Repository navigation
fix(runtime): stop a throwing client-event listener from wedging worktree sleep + harden fan-out - #10052
Conversation
…tree sleep A synchronously-throwing client-event subscriber (e.g. a paired-client relay that throws on a broken pipe) propagated out of emitClientEvent and escaped acquireWorktreeTerminalSpawn *after* it had taken the per-worktree terminal mutation but before returning the release. The release was never handed back to the pty spawn caller, so its `finally` no-op'd and the mutation leaked. Every later sleep of that worktree then blocked on the wedged mutation for 12s and threw terminal_worktree_sleep_timeout, surfacing as "The host could not confirm terminal shutdown" — permanently, until app restart, and worse with a remote client attached. Isolate each listener in emitClientEvent so one failing subscriber can never abort the emitting operation. This fixes the class at the single fan-out point all events flow through, not just the wake path. Adds a regression test that fails (leaks the mutation) when the guard is reverted.
📝 WalkthroughWalkthroughUpdated runtime listener fan-out to catch and log individual subscriber errors while continuing delivery across client events, notifications, PTY events, and fit overrides. Added regression tests covering terminal wake and subsequent sleeping, plus notification subscriber isolation. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/main/runtime/orca-runtime.test.ts (1)
27960-27998: 🎯 Functional Correctness | 🔵 Trivial | 💤 Low valueConsider asserting the error was actually logged.
The test verifies isolation (second listener still fires) and convergence (subsequent sleep succeeds), but doesn't assert that the thrown error from the first listener was caught and logged, which is part of the stated fix behavior. A spy on the logger (if injectable/mocked in this suite) would tighten the regression coverage.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 7400dde0-4280-4f3e-aa46-7c99d542942b
📒 Files selected for processing (2)
src/main/runtime/orca-runtime.test.tssrc/main/runtime/orca-runtime.ts
| const releaseSpawn = await runtime.acquireWorktreeTerminalSpawn(TEST_WORKTREE_ID) | ||
| releaseSpawn() |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Inspect the signature/behavior of acquireWorktreeTerminalSpawn's release function
ast-grep run --pattern 'acquireWorktreeTerminalSpawn($$$) {
$$$
}' --lang typescript src/main/runtime/orca-runtime.ts
rg -n -A5 -B2 'acquireWorktreeTerminalSpawn' src/main/runtime/orca-runtime.tsRepository: stablyai/orca
Length of output: 1348
🏁 Script executed:
#!/bin/bash
set -euo pipefail
sed -n '22690,22745p' src/main/runtime/orca-runtime.ts
printf '\n--- tests around releaseSpawn ---\n'
sed -n '27950,28005p' src/main/runtime/orca-runtime.test.ts
printf '\n--- all release spawn declarations/usages ---\n'
rg -n 'acquireWorktreeTerminalSpawn|releaseSpawn|release =|Promise<\(\) => void>|Promise<\(.*=>.*\)|\(wait_until.*=>.*\)' src/main/runtime/orca-runtime.tsRepository: stablyai/orca
Length of output: 5004
Await releaseSpawn() before exercising the post-release behavior.
acquireWorktreeTerminalSpawn() returns Promise<() => void>, but acquireWorktreeTerminalMutation() may await waitForWorktreeTerminalMutation(...) before returning the async release callback, so releaseSpawn() without await can let the assertions and next sleepTerminalsForWorktree() run before the mutation tail has actually resolved.
The wedged-sleep bug was one instance of a broader latent footgun: every runtime listener fan-out looped and called subscribers with no error isolation, so any synchronously-throwing subscriber could abort the emitting operation. Only emitClientEvent had a proven critical consequence (a leaked per-worktree mutation), but the pattern was repeated across mobile notifications, pty data, driver-state, resize, and fit-override fan-outs. Introduce notifyRuntimeListeners(listeners, deliver, context) and route the low-frequency fan-outs (client-event, mobile-notification, pty-driver, pty-resize, fit-override) through it so one failing subscriber is caught, logged, and the rest still receive the event. The terminal-output hot path keeps an inlined try/catch to avoid a per-chunk closure allocation. Adds a test that a throwing mobile-notification subscriber no longer breaks dispatch for the others, mirroring the client-event regression test.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/main/runtime/orca-runtime.ts (1)
29697-29713: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick winOther listener fan-outs (
mobileSessionTabListeners,graphSyncCallbacks) remain unisolated.The new
notifyRuntimeListenershelper fixes the exact wedge bug for client events, fit overrides, mobile notifications, PTY driver, and PTY resize, butemitMobileSessionTabsSnapshot/notifyMobileSessionTabsChangedNow/notifyMobileSessionTabSnapshots(around lines 25219-25230, 25164-25179, 25181-25193) and thegraphSyncCallbacksdrain loops (e.g. lines 3950-3952, 22064-22067) still call subscriber callbacks with no per-listener try/catch. A throwing mobile session listener or graph-sync callback could abort the same kind of caller-held-mutation loop this PR was written to fix, just via a different fan-out.Out of scope for this PR's fix, but worth a follow-up pass to route these through
notifyRuntimeListenerstoo for consistency.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 4d865727-3342-4ec1-8869-f8f315373d60
📒 Files selected for processing (2)
src/main/runtime/orca-runtime.test.tssrc/main/runtime/orca-runtime.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- src/main/runtime/orca-runtime.test.ts
…tree sleep + harden fan-out (stablyai#10052)
Problem
Sleeping a workspace failed with "Failed to sleep workspace — The host could not confirm terminal shutdown." Persistent (survived retries), hit both the local host and a paired remote client, and only cleared on app restart.
Root cause — a regression from #9874
The sleep RPC (
sleepResolvedWorktreeTerminals) must first acquire a per-worktree terminal mutation before it can stop any PTY. That mutation was leaked by the sibling acquireracquireWorktreeTerminalSpawn, added by #9874:emitClientEventfanned out to every subscriber with no error isolation (long-standing, since #4715). The only production subscriber is the streaming-RPCemitinruntime.clientEvents.subscribethat forwards each event to a connected client. When thatemitthrows synchronously — a paired/remote client whose stream is broken/closed — the throw escapedacquireWorktreeTerminalSpawnafter it took the mutation but before returning the release. The caller insrc/main/ipc/pty.tsnever received the release, so itsfinally { releaseWorktreeSpawn?.() }was a no-op and the mutation stayed pending forever. Every later sleep then blocked on it forWORKTREE_TERMINAL_SLEEP_TIMEOUT_MS(12s) and threwterminal_worktree_sleep_timeout→ the toast.The unguarded fan-out was a dormant footgun for over a year; #9874 made it exploitable by adding the first caller that holds a lock across the emit. Not platform-specific — it correlates with a client being attached, not with Windows (Windows just surfaced the daemon-log evidence).
Diagnostic evidence:
daemon.logshowed the wedged worktree's PTY sessions were never killed (nosession-killedevents) though the daemon was healthy and killed other worktrees' sessions seconds later — i.e. sleep failed before issuing any stop, exactly at the mutation gate, and worktree-scoped.Fix
emitClientEventso a throwing subscriber can't abort the emitting operation or leak the mutation.notifyRuntimeListeners(listeners, deliver, context)and route the low-frequency fan-outs through it; the terminal-output hot path keeps an inlined try/catch to avoid a per-chunk closure allocation.Tests
releases the worktree terminal mutation when a wake client-event listener throws— reproduces the exact wedge; fails on revert (mutation leaks,relay_send_failedpropagates), passes with the fix.isolates a throwing subscriber across runtime listener fan-out— proves the shared helper keeps sibling fan-outs (mobile notifications) alive past a throwing subscriber.Verification
orca-runtime.test.ts: 852 pass (1 pre-existing, unrelated Windows\tmppath-separator failure inpreserves existing badgeColor on runtime createRepo dedupe, present onmain).oxlint+tsc --noEmit -p config/tsconfig.node.json: clean.