Skip to content

fix(ssh): stop a request that never left the client from tombstoning its operation id - #17928

Merged
nwparker merged 4 commits into
fix/ssh-link-wedge-and-probe-verdictfrom
fix/undispatched-request-fence
Sep 2, 2026
Merged

nwparker merged 4 commits into
fix/ssh-link-wedge-and-probe-verdictfrom
fix/undispatched-request-fence

Conversation

@nwparker

@nwparker nwparker commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​161 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​160
Prod 5 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​60 $\color{#cf222e}{\Huge{\mathbf{−}}}$​11 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​49

Stacked on #17817. Review that first; retarget to main once it merges. The two must land together — #17817 widens the bug this PR fixes.

Found while auditing whether #17817/#17838/#17871 make reconnect lossy: they make three transports declare death faster, so anything fragile about in-flight RPCs gets hit more often.

First, a correction to the premise I was working from

"Before #17817 a wedged link hung forever" is not true at the RPC layer. ssh-channel-multiplexer.ts gives every request a 30s client-local deadline that fires regardless of link state, so callers already got a definite-looking failure. What #17817 actually changes is 30s → 20s, a different error code, and all in-flight requests torn down at once instead of one at a time. A real widening, not a new class.

The bug

SshChannelMultiplexer.request rejects a disposed mux and an already-aborted signal synchronously — before nextRequestId++, before sendMessage. Nothing is ever framed.

requestSshAgentSessionCreate tagged those agentSessionOperationOutcome: 'unknown' anyway. Its comment says "after request dispatch", but the catch covers pre-dispatch too. That sets retainReplayFence and tombstones the operation id for 24 hours, so a create that provably never left the client becomes permanently unissuable under its own id — every mobile reconnect replay rethrows SSH connection lost, reconnecting... for a day.

Reachable because SshAgentSessionCapabilities.supportsCreateOperations memoizes its probe per-mux (ssh-agent-session-capabilities.ts:34-49): once primed, SshPtyProvider.spawn goes straight to the dispatch seam with no round trip, so a dispose landing during createAgentSession's async preflight arrives at an already-disposed mux. #17817 widens this directly.

What ships here

1. The fence fix. Mark the two pre-dispatch rejections in the multiplexer — the only place that knows whether a request was framed — and have the caller read that fact rather than infer it. Mid-flight losses carry an identical code and message and keep the fence, because a PTY may genuinely be live on the host.

Two regression tests against a real mux and transport. Reverting the fix makes the first fail (expected … to not have property "agentSessionOperationOutcome") while keeps the fence after dispatch still passes — so it is not just disabling the fence.

A note on shape: my first attempt pre-checked mux.isDisposed() at the call site, and it broke ssh-pty-provider-claim-incarnation.test.ts against five partial mux stubs. That was the design saying the caller should not be the one asking.

2. The undispatched carve-out on the unverifiable verdict. #17817 introduces isSshRequestOutcomeUnverifiable, used by the three call sites that phrase a lost-link verdict to a user. Once the multiplexer can say a request was never framed, that predicate should say false for it — the peer provably never saw it, so it is not unverifiable, it is un-run. One test.

3. A reaper guard for a pause the relay caused itself. While the frame decoder holds reads paused for backpressure no frame can decode, so lastReceivedAt freezes. #17817's silence reaper would then reap a client the relay itself stopped listening to. The client-side multiplexer already guards the mirror case with decoderReadPaused; this is the relay half, and the flag is set inside createClient's decoder wiring rather than in the reaper, because the decoder is what owns the pause.

The tests drive a real pause — a chunk carrying more frames than the decoder delivers in one turn, with setImmediate left unfaked so the continuation cannot run inside advanceTimersByTime — rather than poking the private flag. That is what makes the createClient wiring itself covered: swallowing either the pauseReads or the resumeReads forward fails a test, as does deleting the reaper's guard.

Verification

$ pnpm tc                                                        # clean
$ pnpm test src/main/ssh src/relay src/main/text-generation src/main/ai-vault src/main/providers
 Test Files  471 passed | 3 skipped (474)
      Tests  5031 passed | 23 skipped (5054)
$ pnpm run check:code-quality:changed                            # 0 new findings

One finding this audit produced that is NOT fixed here

pty.spawn can leak an agent process unboundedly. Filed separately — the host holds an idempotency ledger open for 24h while the client memoizes the rejection for 24h and never re-issues, so an agent CLI keeps running on the user's machine, invisible to the client. Closing it is the general replay layer, which was explicitly out of scope.

(An earlier revision of this body listed the three unverifiable verdict call sites here as unfixed. They are fixed — in #17817, where the regression originates. This PR only adds the undispatched carve-out on top.)

Also audited: the socket-takeover TOCTOU — verdict is do not convert

An architectural review flagged relay-socket-ownership.ts:100-113 as probe → unlink → listen with a race, and recommended bind-temp + link(). The cited race is already closed — the review stopped before the fence at :191-205: blockedIdentity is captured at :151 before the probe, and unlinkIfStillStale re-stats at :192 and compares dev + ino + ctimeNs immediately before the unlinkSync at :200, refusing on any mismatch. For a third party to bind there it must first unlink the stale inode and create a fresh one — new ctimeNs — so unlinking its socket cannot happen.

Three reasons not to convert anyway:

  1. bind() on an AF_UNIX path already has link()'s atomicity — create-or-EADDRINUSE — so the retry listen() at :72 already refuses a racer.
  2. Refuse-on-EEXIST cannot be our policy. That reference can disable multiplexing because multiplexing is an optimization; the relay socket is the only ingress and its path is stable across restarts, so a SIGKILLed relay's leftover inode would brick the host permanently. Stale reclamation has to stay — which means keeping the probe and the fenced unlink, i.e. the only part with a window.
  3. In Node the bind→listen gap is two back-to-back synchronous libuv calls in one tick; a temp-socket lifecycle would add leaked .tmp sockets on crash and spend sun_path budget on a path already ~83 of 104 bytes.

Nothing in #17821 becomes unnecessary — it answers "is the incumbent process live and does it hold PTYs" via lsof + argv re-verification, which EEXIST cannot answer.

One thing for whoever owns #17821: RelaySocketOwnership has no direct test file. Since that PR elevates this class to the authority, its "refuse to steal a socket that accepts" and "refuse when the inode changed" invariants deserve pinning.

@pullfrog

pullfrog Bot commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

the model provider had a temporary upstream failure.

The provider refused or dropped the request mid-run — nothing in your repo or your credentials is at fault. Re-trigger Pullfrog; if it keeps happening, check the provider's status page.

provider error: {"code":502,"message":"Network connection lost.","metadata":{"error_type":"provider_unavailable"}}

Pullfrog  | Rerun failed job ➔ | View workflow run | via Pullfrog | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No critical issues — the fence fix and the verdict carve-out are correct, with one minor robustness concern on the relay pause wrapper inline.

Reviewed changes

  • Fenced undispatched spawns — SshChannelMultiplexer.request now marks its two pre-dispatch rejections (disposed mux, pre-aborted signal) with an sshRequestUndispatched flag, and requestSshAgentSessionCreate skips the 24h replay tombstone when that flag is present, so a spawn that provably never left the client is no longer fenced.
  • Undispatched carve-out on the unverifiable verdict — isSshRequestOutcomeUnverifiable returns false for marked-undispatched errors, so the three lost-link verdict call sites report "not reached" rather than "may still be running" when the peer provably never saw the request.
  • Relay reaper backpressure guard — RelayClient gains a readsPaused flag set inside createClient's decoder wiring, and reapSilentClients skips a client whose reads the relay itself paused, mirroring the client-side decoderReadPaused guard.

The regression tests drive a real mux/transport and a real FrameDecoder backpressure pause (with setImmediate left unfaked) rather than poking private state, so both guards are meaningfully covered.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

// Why the flag is set here rather than inside the reaper: the decoder owns the pause, and
// the reaper must not count silence it caused itself.
pause: () => {
client.readsPaused = true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A pauseReads() throw leaves readsPaused stuck true, silently exempting that client from the reaper forever. The wrapper sets the flag before calling sourceOptions?.pauseReads?.(), but FrameDecoder.acquirePause reverts its own paused to false and rethrows on that throw, so the decoder recovers while this flag does not. The resume side is fine — releasePause clears the decoder's paused before invoking it — but pause has the opposite ordering. The client-side mirror (pauseDecoderReads) routes these throws to handleProtocolError rather than trusting the flag.

Technical details
# readsPaused can diverge from FrameDecoder.paused on a pause throw

## Affected sites
- src/relay/dispatcher-client-lifecycle.ts:151 — `pause: () => { client.readsPaused = true; sourceOptions?.pauseReads?.() }`
- src/shared/relay-frame-decoder.ts:243 — `acquirePause` sets `paused = true`, calls `pause()`, and on throw reverts `paused = false` and rethrows

## Required outcome
- `readsPaused` must track `FrameDecoder.paused` (a throw leaves both `false`), or the throw must be routed somewhere that disposes the client.

## Suggested approach
- Set `client.readsPaused = true` only after `sourceOptions?.pauseReads?.()` returns, or wrap the call in try/finally that reverts the flag on throw; alternatively adopt the client-side pattern (catch and treat as a protocol error).

@nwparker
nwparker force-pushed the fix/ssh-link-wedge-and-probe-verdict branch from c54be7e to c58e8bb Compare September 2, 2026 21:04
@nwparker
nwparker force-pushed the fix/undispatched-request-fence branch from 2f62120 to 36859d0 Compare September 2, 2026 21:04
@nwparker
nwparker force-pushed the fix/ssh-link-wedge-and-probe-verdict branch from c58e8bb to 0632910 Compare September 2, 2026 21:13
@nwparker
nwparker force-pushed the fix/undispatched-request-fence branch from 36859d0 to 4545e6c Compare September 2, 2026 21:14
@nwparker
nwparker merged commit 30918e1 into fix/ssh-link-wedge-and-probe-verdict Sep 2, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant