Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(desktop): installing an update closes local SSH forwards before relaunching #34171

ClosedUmais-Adeed(GitHub profile, opens in a new tab)wants to merge 0 commits intomain fromrefs/pull/34171/headopened View on GitHub

Problem

After the desktop app installs an update and relaunches, each local SSH forward from the old process keeps running, reparented to PID 1, and the new process opens a second forward to the same host. In #14268, the reporter found 12 orphaned ssh forwards and tied one (PID 60751, port 62377) to an update install.

Julius confirmed the cause in triage on the issue:

  • installDownloadedUpdate stops pooled backends before quitAndInstall, because app.quit() exits before the runtime scope can run finalizers.
  • The updater's quit path then skips the normal desktop shutdown on purpose, so the macOS relaunch isn't cancelled.
  • Local forwards are ssh -N -L children that only the SshEnvironmentManager scope finalizer signals. A parent exit doesn't stop them, and -n means a closed stdin doesn't either.

His triage also says the fix has to reap the local ssh children before quitAndInstall, the same way backends are stopped, and that only the local listener needs to die.

Change

  • SshEnvironmentManager gets releaseLocalTunnels. It takes every tunnel out of the manager's map, then closes each entry's scope. With the entry already removed, the existing entry finalizer kills only the local ssh process (SIGTERM, then the existing force-kill timeout) and skips stopping the remote server. disconnectEnvironment already relies on this ordering. Remote servers keep running, so the relaunched app reconnects to them.
  • A forward joins the map only after its readiness check, so the manager also tracks the entry scopes of forwards still starting, and the release closes those. The tunnel child is spawned into that scope, so closing it kills the child, and the waiting connect ends without registering. A release counter covers a connect that started before the release but had not created its scope yet: it closes its fresh scope at once, so its forward dies as it spawns.
  • It takes no per-target lock, so a connect waiting on a password prompt can't hold up the quit. It runs uninterruptibly.
  • DesktopSshEnvironment exposes it, and DesktopUpdates calls it right after stopping backends and before quitAndInstall.
  • main.ts provides the same layerDesktopSsh layer to DesktopUpdates. Layers are memoized by reference, so there is still one SSH manager, the same pattern main.ts already uses for DesktopManagedPolicy.

If quitAndInstall fails, backends restart as before. The released forwards reopen the next time each environment connects.

Scope and approval

Fixes #14268, which is labelled bug and via-triage, and which a maintainer confirmed and described the fix for in the triage comment. This is one problem: the update quit path leaks local SSH forwards. The reconnect race in #4144 is a separate leak and is left alone.

Verification

  • New test in packages/ssh/src/tunnel.test.ts, releases local forwards without stopping their remote servers. It connects to a target, then releases local tunnels, and asserts that the local -N process was killed, that no remote stop command ran, and that the next connect opens a new forward. On main it fails because the manager has no such operation:
    × releases local forwards without stopping their remote servers
    TypeError: yield* (intermediate value)… is not iterable
  • A second new test, kills a forward that is still starting, and its connect fails, releases while a forward waits for readiness. It fails without the starting-scope tracking (expected +0 to equal 1 kills).
  • DesktopUpdates.test.ts: the update harness records a releaseSshTunnels step. The three existing install tests that assert the step order now expect it before quitAndInstall. Without the DesktopUpdates change, those three fail (3 failed | 37 passed); with it, all 40 pass.
  • vp run check:changed: format, lint, knip, typecheck (5 packages) and tests pass.
  • concurrent launches on one remote home record the server that owns it, an existing it.live test in the same file, failed in one local run with an InterruptError. It fails the same way on unmodified main here, and it passed in the check:changed run.
  • I did not run a real signed update and relaunch on macOS. The update quit path is covered through the update harness.

Written by Claude Opus 5.5 in Claude Code.

    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    2. @@ -1902,4 +1907,6 @@
      19021907 );
      19031908 });
      19041909
      1905- return SshEnvironmentManager.of({ ensureEnvironment, disconnectEnvironment });
      1910+ // No target lock: a connect waiting on a password prompt must not hold up the quit.
      1911+ const releaseLocalTunnels = Effect.suspend(() => {
      1912+ const entries = [...tunnels.values()];
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟠 High src/tunnel.ts:1912

      releaseLocalTunnels can return while a local SSH forward is still running, so update cleanup leaves that child process alive. It snapshots only tunnels, but createTunnelEntry registers an entry after startSshTunnel finishes readiness checks; an in-flight creation is therefore omitted and can register after tunnels.clear(). Track and close in-flight creation scopes, and prevent pending creations from registering after release, without waiting on password prompts.

      🤖 Copy this AI Prompt to have your agent fix this:
      In file @packages/ssh/src/tunnel.ts around line 1912:
      
      `releaseLocalTunnels` can return while a local SSH forward is still running, so update cleanup leaves that child process alive. It snapshots only `tunnels`, but `createTunnelEntry` registers an entry after `startSshTunnel` finishes readiness checks; an in-flight creation is therefore omitted and can register after `tunnels.clear()`. Track and close in-flight creation scopes, and prevent pending creations from registering after release, without waiting on password prompts.

      Confirmed and fixed in 518d9cd709. The manager now tracks the entry scope of every forward that is still starting (from Scope.make until startSshTunnel settles), and releaseLocalTunnels closes those too. The tunnel child is spawned into that scope, so closing it kills the child, and the waiting connect ends without registering. A release counter also covers a connect that started before the release but had not created its scope yet: it closes its fresh scope at once, and the forward dies as it spawns. Adding a finalizer to a closed scope runs it immediately. There is still no target lock, so a pending password prompt can't hold up the quit. New test: kills a forward that is still starting, and its connect fails. It fails on the previous commit (expected +0 to equal 1 kills) and passes now.

      Sorry, I'm unable to act on this request because you do not have permissions within this repository.

    3. Approvability

      Verdict: Would Approve

      Macroscope's review found this PR approvable — Update installation now closes registered local SSH forwards before relaunching while preserving remote servers. The reported race involving in-flight connections remains subject to the separate correctness gate.

      Needs human approval because:

      • 1 blocking correctness issue found at or above your repo's Minimum Blocking Severity
      Adjust the Minimum Blocking Severity for this repo — including turning it Off — in [Settings](https://app.macroscope.com/pingdotgg/settings#Repos). You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Explain Change →
      Pre-merge checks: Inconclusive 2

      Failed Failed checks: 2 inconclusive
      Check nameStatusExplanationResolution
      Title checkPendingThis assessment has not completed. The check will be retried on the next pre-merge check run.
      Description checkPendingThis assessment has not completed. The check will be retried on the next pre-merge check run.
      Finishing Touches

      Generate unit tests (beta)

      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
    5. Thanks for this. https://git.cafe/pingdotgg/t3code/pulls/14276 makes the same change and is the one being kept.

      #14276 fixes the same update-related SSH leak and also handles connections still being created when cleanup starts. This version only snapshots registered tunnels, so a forward that finishes connecting during cleanup can still escape and survive relaunch.

      Your separation of local forward cleanup from remote server shutdown is the right approach. The existing PR carries that approach through concurrent connection handling and failed-install recovery, so we should consolidate the fix there.

      Closing as a duplicate.

    6. juliusmarminge closed this

Sign in to join this conversation.