Skip to content

Single Instance

Most desktop apps should only ever run one copy per user. When a user double-clicks the app icon while it’s already open, you want the existing window to focus — not a second window.

Install the required plugin:

Terminal window
tynd add single-instance
import { app } from "@tynd/core/client";
import { singleInstance } from "@tynd/plugin-single-instance/client";
const { acquired } = await singleInstance.acquire("com.example.myapp");
if (!acquired) {
// We're the second instance. The primary has already auto-focused itself
// and will receive {argv, cwd} via onSecondLaunch. Exit silently.
await app.exit(0);
}
// Only the primary reaches this point. Set up second-launch handling:
singleInstance.onSecondLaunch(({ argv, cwd }) => {
console.log("user tried to launch again with", argv, "from", cwd);
// typical: focus the main window + interpret argv as a file-open request
});
  • acquire(id) uses a cross-OS exclusive lock + a per-user local socket for forwarding argv/cwd. The primary delivers app:second-launch to app-local webviews only.
  • Hold the lock for the process lifetime — don’t release and re-acquire. The lock releases on process exit.
  • id doubles as the OS lock name and the socket name. Use a stable reverse-DNS identifier.

When acquire() returns { acquired: false }, the host has already:

  1. Connected to the primary instance’s local endpoint (an owner-only named pipe on Windows, a socket in a per-user 0700 directory on Linux and macOS) and checked that it belongs to the current user.
  2. Sent the forwarded payload as a single JSON line { argv, cwd }.
  3. Triggered the primary window’s setFocus() + un-minimize via the host’s native event loop — no IPC round-trip needed.

Register a custom scheme in tynd.config.ts:

export default {
backend: "backend/main.ts",
frontendDir: "dist",
protocols: ["myapp"], // myapp:// links now launch this app
bundle: { identifier: "com.example.myapp" },
} satisfies TyndConfig;

Handle both cold-start and duplicate-launch cases with onOpenUrl:

singleInstance.onOpenUrl((url) => {
// e.g. "myapp://invite/abc123"
const parsed = new URL(url);
router.navigate(parsed.pathname);
});

onOpenUrl fires for:

  • Cold start — argv contains the URL.
  • Duplicate launch — the primary receives the forwarded URL.

See Deep Linking for scheme registration details per OS.

Users drag a file onto your app icon while it’s running. The OS relaunches the app with the file path as argv[1]:

singleInstance.onSecondLaunch(({ argv }) => {
const file = argv.find((a) => a.endsWith(".myapp"));
if (file) openFile(file);
});

Declare file type associations with bundle.fileAssociations in tynd.config.ts. The bundler writes the required .desktop, .app, and installer metadata.

  • Windows — owner-only named pipe, named from a hash of your user SID, the id and a per-user, per-endpoint random secret (%LOCALAPPDATA%\tynd\pipe-nonce-<hash of the id>). Windows frees the pipe when the owning process dies; no stale-lock cleanup needed.
  • Linux and macOS — socket file in a per-user 0700 directory. A file left by a crash is removed by the next primary instance, which holds the lock.
  • A second launch waits at most 2 s to connect to the primary; on timeout acquire() rejects with a connect: error and nothing is forwarded.
  • If the primary cannot create its forwarding endpoint, the app still starts but single-instance is not enforced (see the API page).

Stale lock. Usually self-heals on process exit (see above). If it doesn’t:

  • Windows — nothing to do; Windows frees named pipes aggressively.
  • Linux and macOS — the lock is a file lock, released by the OS when the process exits. If acquire() still returns false, another instance of the app is running for this user (killall MyApp to stop a hung one).

You called singleInstance.onSecondLaunch(...) inside a handler that itself re-fires (React useEffect rerender, component re-mount). The API is idempotent for a given handler function, but registering two different handler functions means both fire.

// Register once, not in a render function
const unsub = singleInstance.onSecondLaunch(onRelaunch);
// call unsub() if you want to stop handling

Use a stable reverse-DNS identifier that matches bundle.identifier:

singleInstance.acquire("com.example.myapp");

Don’t include version numbers — a user upgrading from 1.0.0 to 1.1.0 expects the upgrade to auto-focus the already-running old version (so the old one can shut down cleanly, then the new one starts). Different ids break that.

  • Multi-user isolation on Windows (service / tenancy setups) — single-instance is user-scoped, so two users on the same box each get their own primary.
  • Cross-machine coordination — no network-wide lock. If you want a license server, build your own.