Daemon lifecycle: stale-reclaim releases the lockfile but never kills the orphan process → multiple daemons pile up and fight over the shared browser profile
Separate from #12 (the Cloudflare/headless root cause), the daemon lifecycle has an independent flaw that lets multiple daemons pile up and fight, which on my machine escalated a transient browser failure into a full outage plus stuck external MCP clients.
What I observed
At one point there were 5 server.mjs daemon start processes alive simultaneously — one orphan ~4 days old (survived a VS Code close) plus four spawned within a single hour. The logs showed a stream of:
Browser launch hit a profile-lock collision (attempt 1/4); retrying after backoff.
They were all opening Chrome against the same persistent profile dir (profiles/wynn/browser-data) and killing each other's browser, so every in-flight call died with page.evaluate: Target page, context or browser has been closed, and any stdio client attached via start.mjs got MCP error -32000: Connection closed and lost its session permanently (had to restart the host).
Root cause in the code (0.8.53/0.8.54 bundle)
Two design gaps combine:
1. Stale-reclaim releases the lock but never terminates the losing process. In getDaemonStatus(..., { reclaimStale: true }), when a daemon is judged stale it does:
if (stale && options.reclaimStale) {
release({ lockPath, expectedUuid: record3.uuid }); // <-- only deletes the lockfile
return { running: false, ... };
}
release() just removes/rewrites the lockfile. The stale daemon's process keeps running, still holding its Chrome instance and the browser-data profile lock. So the next ensureDaemon() spawns a fresh daemon that immediately collides on the profile with the orphan it "reclaimed."
2. Ephemeral port (daemonPort: 0, the default) means nothing stops a second daemon from starting. On EADDRINUSE the ephemeral path just retries with a new random port (isAddressInUseError → delay + continue), so N daemons happily coexist on N different ports, all pointing at one shared profile. (Interestingly the pinned-port path already does the right thing — it throws "Port X is in use; daemon cannot start" instead of spawning a duplicate. Pinning Perplexity.daemonPort is a decent user-side mitigation, but the default shouldn't allow the pile-up.)
The orphan itself is born from a third gap: a daemon whose parent (extension host / VS Code window) is force-closed or crashes gets reparented by the OS and never exits on its own.
Trigger
Normally this stays latent — the orphan holds the lock and serves fine. It blew up here because #12 (CF now blocks the headless persistent phase) made every freshly-spawned daemon unhealthy at bootstrap, so the watchdog kept declaring stale → reclaim (lock only) → respawn, in a loop, each new daemon colliding with the last on the profile. #12 was the spark; this lifecycle gap was the accelerant.
Suggested fixes
- Reclaim should fence the loser, not just drop the lock: on stale-reclaim,
SIGTERM→SIGKILLthe recorded PID (there's already exactly this logic instopDaemon's force path) before releasing, so orphans can't linger holding the profile. - Singleton on the profile, not just the lockfile: treat a live profile
Singleton*lock / a still-listening old daemon as "attach or refuse," never "spawn alongside." - Parent-death cleanup: best-effort kill of a prior daemon on extension activation, or a PID-liveness sweep, so a force-quit doesn't leave a multi-day orphan.
- Consider defaulting
daemonPortto a pinned value (or at least documenting the pinning mitigation), since the pinned path already prevents duplicates.
Environment
- Extension: nskha.perplexity-vscode 0.8.54 (daemon reports 0.8.53)
- OS: macOS (darwin arm64, Darwin 25.5.0)
- Node: v24.17.0
Related: #12 (the CF/headless outage that triggered this pile-up).
Excellent report again — the five-process inventory with the four-day orphan is exactly the evidence that was missing, and it materially changed our read of #12 (more on that below). Status of your four suggestions, as of main today:
| Suggestion | Status |
|---|---|
| Reclaim should fence the loser, not just drop the lock | Fixed (94b094c). Your reading of the code was exact: getDaemonStatus(reclaimStale: true) only called release(). "Stale" usually means a dead pid, but isStale() also condemns a live holder whose health responder echoes a different uuid — and that path dropped the lock while the process lived on, holding Chromium and the profile singleton. A live, foreign, stale holder is now SIGTERM→SIGKILLed before release, using the same fence as two other reclaims that landed this week: the wedge reclaim (live pid whose port never answers health — your "unhealthy at bootstrap" loop shape) and the code-graph version gate (a daemon from a previous build answers health fine but can't import the current hashed chunks; ensureDaemon/plain attach now stop-and-replace it instead of attaching). Covered by a test that stands up a real live process answering /daemon/health with a mismatched uuid and asserts it is killed before the lock is released. |
| Singleton on the profile, not just the lockfile | Open — tracked as a follow-up. Agreed in principle; it needs design care to not collide with the Doctor probe's existing liveDaemonOwnsProfile() skip and the login runners' separate login-browser-data. |
| Parent-death cleanup | Partially covered, rest open. The extension's activation path already reaps stale-versioned daemons by pid, and the new fences kill wedged/mismatched/stale-live holders on every ensure. A generic PID-liveness sweep for a healthy orphan is deliberately not done — a daemon surviving VS Code close is the intended shared-daemon design; it only became a problem when #12 made every newcomer unhealthy. With the fences, the loop that turned one orphan into five is closed. |
Pinned daemonPort default |
Open, and half-done: Perplexity.daemonPort was dead code until this week — it's now actually honored (validated 1024–65535, passed to the spawn), so pinning is a real mitigation today. Changing the default is a product decision we're leaving on the table for now; noted here as the tracking item. |
Also relevant to your "stuck external MCP clients" symptom: the biggest single cause of daemon death is fixed — a rejecting background reinit (.reinit sentinel) used to become an unhandled rejection and hard-exit the daemon (the watcher wrapped an async callback in a synchronous try/catch). And when a daemon dies or respawns on a new port now, VS Code re-fires its MCP definitions instead of hammering the dead port.
On #12 — your pile-up changes the picture
Five daemons fighting over one browser-data produces exactly "Target page, context or browser has been closed" — our own probe documents that string as the profile singleton-lock signature (#8), and on POSIX the collision exits silently, which matches your page.evaluate failure shape precisely. Your own framing ("#12 was the spark; this lifecycle gap was the accelerant") may even understate it: the pile-up alone fully explains the outage, and the CF-blocks-headless-reuse claim remains unproven (your repro showed headless can't solve a fresh challenge, which Phase 1 exists for).
Accordingly we've reverted the headed-offscreen Phase 2 default — it caused a confirmed regression (a permanently visible Chrome window on Windows; the off-screen trick was designed for the ~20s bootstrap, not a long-lived window with a taskbar presence) while its benefit was unconfirmed. PERPLEXITY_PERSISTENT_HEADED=1 remains as the opt-in if you're genuinely re-challenged on the reuse path.
The ask, refreshed: with the fences on main, your machine can't pile up daemons anymore. If you can rebuild and re-run your original repro on a profile a headed session just cleared (the reuse path) with exactly one daemon alive, that would finally settle whether CF blocks headless reuse at all — and whether the headed opt-in needs to get louder. The two disambiguating log lines from the earlier comment still apply.
The pile-up is fixed and released in extension v0.8.60 / mcp-server v0.8.59. Closing — with one item split out rather than dropped.
Your four suggestions, final status:
| Suggestion | Status |
|---|---|
| Reclaim should fence the loser, not just drop the lock | Fixed. Your reading was exact: getDaemonStatus(reclaimStale:true) only called release(). A live holder judged stale by uuid mismatch now gets terminated before the lock is released — same fence as the wedge reclaim and the code-graph version gate |
| Singleton on the profile, not just the lockfile | Fixed. On a real ProcessSingleton collision the client now hunts processes carrying --user-data-dir=<browser-data> and evicts them before retrying — ownership-guarded, so if a live foreign daemon holds daemon.lock nothing is killed (we'd be the trespasser) |
| Parent-death cleanup | Fixed where it mattered. The orphan's consequence is gone: every daemon-kill path now tree-kills on Windows (taskkill /T /F), so a reclaim can no longer strand a Chrome at all. A generic sweep for a healthy orphan stays deliberately undone — a daemon outliving its editor window is the intended shared-daemon design; it only became a problem when unhealthy newcomers piled up behind it |
Pinned daemonPort default |
Split out → #16. The setting is now honored (it was dead code until this week), so pinning is a real mitigation today. Changing the default is a product decision, so it gets its own issue rather than being quietly dropped here |
The root cause you couldn't see from the logs, and the reason this kept happening: on Windows process.kill(pid, "SIGTERM") is TerminateProcess — the daemon's cleanup handler never runs, so every kill path we had (wedge reclaim, version gate, your stale-fence, force-stop, and the extension's activation reaper) left the daemon's Chrome alive holding the profile singleton. The orphan then forwarded every later launch to itself — "Opening in existing browser session" — and each retry opened another about:blank tab in it. Our own fences were manufacturing the orphans they then tripped over. That's now tree-killed at all five sites.
Two more things landed alongside, both from your log: reinit coalescing (your machine ran 138 reinit cycles — N requests during an in-flight run now collapse to at most one follow-up, newest passphrase winning so a profile switch is never stranded), and idle browser parking (Chromium closes after 30 idle minutes and the next tool call resurrects it lazily — a daemon nobody is using now costs ~nothing; PERPLEXITY_BROWSER_IDLE_PARK_MS=0 disables).
Your framing — "#12 was the spark; this lifecycle gap was the accelerant" — turned out to understate it: the pile-up alone fully explains the "Target page, context or browser has been closed" outage, which is why the headed-offscreen Phase 2 default from #12 has been reverted to an opt-in. #12 stays open pending the clean single-daemon repro.
Reopen if you still see more than one server.mjs daemon start on 0.8.60.