VSCode-Perplexity-MCP icon indicating copy to clipboard operation
VSCode-Perplexity-MCP copied to clipboard

VS Code stays pinned to a dead daemon port after respawn (ephemeral port + definitions never re-fired; Perplexity.daemonPort is dead code)

Open ARHAEEM opened this issue 4 months ago • 1 comments

Split out of #10, where @ITFinesse hit:

Connection state: Error Error sending message to http://127.0.0.1:63596/mcp:
TypeError: fetch failed: connect ECONNREFUSED 127.0.0.1:63596

The cause of the daemon death in that report is fixed (see #10 / commit f05938a — a rejecting reinit was hard-killing the daemon via an unhandled rejection). This issue is the second half: once the daemon dies for any reason, VS Code never recovers.

The bug

  1. The daemon binds an OS-assigned ephemeral port on every start — const requestedPort = options.port ?? 0 (daemon/server.ts:100). The extension never passes a port (daemon/runtime.ts:122-127), so every respawn lands on a different port.
  2. provideMcpServerDefinitions bakes that port into the McpHttpServerDefinition (extension.ts:1095, from launcher.ts:911).
  3. serverDefinitionsChanged.fire() is only called on user-initiated auth/profile actions — login, logout, switchAccount, createProfile (extension.ts:568, 873, 883, 901 + the DashboardProvider sites). Nothing fires it on daemon death or respawn.

So VS Code holds a concrete-but-dead URL indefinitely, and even a healthy respawn lands on a port it never learns about. To the user it looks permanently broken with no way out.

Related wrinkle: postDaemonState({ ensure: true }) (DashboardProvider.ts:2026-2070) calls ensureBundledDaemon() and never fires the change event — so if the daemon is down, opening the dashboard can move the port out from under VS Code.

Perplexity.daemonPort is dead code

This is what makes it un-workaroundable. The setting is defined (settings.ts:29) and read (settings.ts:75) — and then never used. Every real consumer reads the observed port (status.health?.port ?? status.record?.port), never the configured one.

Which means extension.ts:262-269 is a no-op. It tells the user, verbatim:

http-loopback configs embed the port; this config for ${ideTag} will break when the daemon restarts on a new port. Pin Perplexity.daemonPort?

…and on accept writes daemonPort: 49217. That value goes nowhere. We prompt users to apply a fix that does nothing.

Note the repo already understood this failure mode — but only guarded external IDE configs with that nudge. VS Code's own definition embeds the port identically and got no protection at all.

Also worth fixing while here

isStale() (daemon/lockfile.ts:152-170) checks pid liveness only (isProcessAlive, :255-265) — never whether the port is accepting connections. A live pid with a dead socket yields healthy=false, stale=false → running: true (launcher.ts:185), and ensureDaemon's spawn is gated on if (!status.running && !launched) (:212) — so it never spawns a replacement, never reclaims the lock, and throws "Timed out" after 15s (:225). provideMcpServerDefinitions catches that and returns []. Unrecoverable without manual intervention.

Same class in the startup window: the provisional lock is written with port: 0 (launcher.ts:263) before the real port is synced (:487), and probeHealth returns null for port <= 0 (:844) — so a daemon that hangs in between pins the lock at port 0 + live pid permanently.

Options

  1. Honor Perplexity.daemonPort — thread settings.daemonPort into ensureBundledDaemon(). Makes the existing nudge truthful and gives a stable URL. Doesn't help users who leave it at the default 0.
  2. Fire serverDefinitionsChanged when the observed port changes — VS Code re-resolves and picks up the new port. Fixes the default case; needs a place that notices the change.
  3. Both — pin when configured, re-fire when not. Probably right.

Plus: make isStale() reclaim a lock whose port refuses connections, so a wedged daemon can be replaced rather than timing out forever.

I'd take (3) + the isStale fix. Flagging rather than just doing it because option 1 changes what a documented setting does, and the port-pinning default (49217?) is a product decision.

ARHAEEM avatar Jul 16 '26 17:07 ARHAEEM

Fixed in main (679c9ba) — all three parts, taking option 3 from the writeup:

  1. Perplexity.daemonPort is honored. ensureBundledDaemon validates the setting (1024–65535) and passes it into the spawn path. The extension's "Pin a port now?" nudge now does what it says. Pins take effect at the next daemon start; 0 keeps today's ephemeral behavior.
  2. VS Code re-resolves when the port moves. Every daemon-ensure funnels through ensureBundledDaemon, which tracks the observed port and fires a change listener; the extension re-fires serverDefinitionsChanged on it, so a respawn on a new port propagates instead of leaving VS Code hammering a dead socket with ECONNREFUSED. No re-resolve loop: the notifier only fires on an actual change, and the re-resolve lands on the now-current port.
  3. Wedged daemons are reclaimed. ensureDaemon gets a second pass: when the full timeout elapses with a live pid holding the lock whose port never once answered health — the hung-between-lock-acquire-and-port-sync shape (lock pinned at port 0), or a dead socket under a live process — it SIGTERMs (then SIGKILLs) the pid, releases the lock, and retries once. Guarded against ever signalling its own pid. Previously this state was unrecoverable without hand-deleting daemon.lock, because isStale() is pid-only and the spawn gate never opened.

Tests: a port-change/pin suite for the runtime, and a launcher test that wedges the lock with a real live process at port 0 and asserts the reclaim kills it and a healthy replacement comes up — the exact shape that used to dead-end every ensure.

Combined with the reinit crash fix (f05938a), the full ECONNREFUSED chain from #10 is closed: the daemon no longer dies on background reinit failures, and if it dies for any other reason, VS Code follows it to the new port. Closing.

ARHAEEM avatar Jul 16 '26 19:07 ARHAEEM