Repository navigation
fix(relay): reap owned PTYs when the daemon dies on an uncaught exception (STA-5697) - #16894
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (2)
Included review availability: Your plan provides up to 10 included reviews per hour; 2 remain after this review. 📝 WalkthroughWalkthroughThe relay daemon stores its runtime Merge Risk: ⚪ Minimal · up to The change synchronously cleans up relay-owned terminals when the daemon exits from an uncaught exception while preserving socket cleanup and exit behavior. No actionable merge-blocking risk remains beyond normal checks and review. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Description checkExplanation The description is detailed and on-topic. It explains the problem, fix, rationale, linked issue, testing, platform behavior, and relationship to related changes. Some template sections and checklist items are not explicitly completed, but the required information is mostly present.
✨ Finishing Touches 💡 1📝 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 |
|
Note GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer. |
Review — PR #16894Reviewed at head The core claim holdsI traced the Ablation
Full pre-fix tree (both prod files reverted, test kept) is RED, matching the PR body's claim. Findings (all P2)
Not affected
Composition with #16895The two merge cleanly and compose correctly: #16895's process-wide kill-on-close job nests the per-PTY jobs, which is the documented and intended relationship, and it does not change what this PR's reap observes. See my comment on #16895 for a shared-failure-mode note that applies to both. |
…able The fatal reap had no POSIX coverage at all -- every case forced win32 -- and the daemon discarded the rethrown reap error in an empty catch, so a reap that failed on a remote host left no trace in the only log a crash produces. Collapse the job-terminated branch onto the forceKillSent flag it already sets: the flag is what suppresses the redundant signal, so the separate "continue" was a second expression of one intent, and the two could only be caught together -- reverting either one alone left the suite green.
2950f4f to
8b892bb
Compare
|
Note GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer. |
Review — patched, recommend merge with #16895Rebased onto current Confirmed from the earlier round
New finding: two arms that could only be caught together
Gate-level ablation, each arm reverted ALONE
Every arm is now individually pinned (8/8). Red-before-greenThe new failure-log test fails against the pre-fix tree — Gates
Still open — not fixed here, flagging for a decisionThe reap is bounded per call, not in aggregate. Also worth noting: |
Addendum — the Windows job branch is dead on SSH remotes, so the POSIX path I added coverage for is the one that runsFollow-up to my review above; verified independently.
So on an SSH remote,
A one-line comment at the This does not change my recommendation: the patch stands, CI is green at |
…tion (STA-5697) (stablyai#16894) * fix(relay): reap PTY jobs on fatal exit (STA-5697) * test(relay): cover the POSIX fatal reap and make a failed reap observable The fatal reap had no POSIX coverage at all -- every case forced win32 -- and the daemon discarded the rethrown reap error in an empty catch, so a reap that failed on a remote host left no trace in the only log a crash produces. Collapse the job-terminated branch onto the forceKillSent flag it already sets: the flag is what suppresses the redundant signal, so the separate "continue" was a second expression of one intent, and the two could only be caught together -- reverting either one alone left the suite green.
In plain terms
You have terminals open on a remote machine over SSH. Orca runs a small helper program on that machine to look after them.
If that helper hit an unexpected error, it quit — and left your terminal programs running. Orca could no longer see or control them, but they were still there: still running, still holding your project folder open, still using the machine. You'd have no way to notice except that things gradually got slower and messier.
This makes the helper shut your terminals down on its way out, instead of walking away and leaving them behind.
What you'll notice: nothing, when things are working. That's the point — after a crash, the leftovers you never knew about simply aren't there any more.
How the three PRs fit together
The original report (STA-5698) was: after the remote helper restarts, Orca might start your AI agent a second time while the first one is still running — two agents editing the same files at once.
That needs two ingredients:
These three PRs remove both ingredients rather than trying to detect the collision afterwards:
Each stands alone and they can land in any order.
Why not just detect the collision? That was the first attempt (#16843, now closed). It tried to work out "has the helper been replaced?" by comparing how long it had been running against when Orca last saw the terminal. It couldn't tell a helper restart apart from the whole machine having rebooted — and after a reboot nothing survives, so there's nothing to protect against. It would have refused to restore your agent in a case that was completely safe. A live reproduction also failed to produce the collision at all on Linux: killing the helper took the agent down with it in 0.3 seconds.
So: prevent the leftovers, fix the names, and the problem doesn't arise instead of being guessed at.
Fixes STA-5697.
The bug
src/relay/relay-daemon.tsinstalls anuncaughtExceptionhandler that logs, cleans up the socket, and callsprocess.exit(1). It never disposes the PTYs the relay owns — even though the teardown already exists and is used on every other exit path (PtyHandler.dispose()→disposePtys()).So a relay that crashes leaves every shell it owned behind.
Why it matters, measured
On Windows it orphans. On build 26200, after
taskkill /Fof a process owning a ConPTY, the shell died but a descendant survived. Windows has no SIGHUP-on-master-close semantics, and the per-PTY job is deliberately not kill-on-close (its comment explains why: with that flag, typingexitin a pane also reaped whatever the user had backgrounded).On Linux it usually doesn't — closing the pty master delivers SIGHUP and the shell and its agent die, measured at 0.3s in a live SSH repro. But the crash path must not depend on that being true for every shell, every agent, and every platform.
The fix
A bounded, synchronous, best-effort reap on the fatal path before
process.exit(1).dispose()— which is async and waits for physical exit — is the wrong tool. This uses the existing synchronous primitives instead.RelayRuntimeServicesis constructed so an exception during construction is still caught. A mutable holder is set once the runtime exists; the reap is a no-op until then.unhandledRejectiondeliberately unchanged. It logs and does not exit, so the process keeps running and its PTYs must keep running with it.One correction to the original design
The task described reusing
killPtyProcess(pty, 'SIGKILL'), whose win32 branch callspty.kill(). That is not sufficient:pty.kill()does not terminate the non-kill-on-close per-PTY job, so the descendants that actually orphan on Windows would have survived. The implementation usesterminatePtyJobfirst — which is exact, because job membership is the kernel's answer rather than a pid walk — and falls back to the POSIX group kill path only when that is unavailable.Tests
src/relay/relay-daemon-fatal-reap.test.tsasserts the derived behaviour: that the owned PTYs are actually killed on the fatal path. It was confirmed red before the production change (expected the Windows job kills, received[]) and green after.Verification
pnpm tc,npx oxlinton the changed files, the new test file, andpnpm oxfmtall pass. Integrated with the sibling Windows PR,pnpm build:relaysucceeds and the relay suite shows no new failures — the two that fail (pty-handler-spawn-environment,pty-handler-startup-command-delivery) fail identically onorigin/main.Relationship to the other relay PRs
This removes the orphan on paths that run a handler. Its sibling covers deaths that run nothing at all (
taskkill /F, OOM) by putting the relay in a kill-on-close job. They are independent and can land in either order.