Repository navigation
fix(terminal): never sweep descendants of an already-exited PTY root (hardening, #9191 investigation) - #10069
Conversation
collectDescendantRows walked children of rootPid even when the root row was absent from the ps snapshot (root already exited). During a teardown race the PID can be recycled, so rows still pointing at the vacated PID are a PID-reuse coincidence — and terminateDescendantSnapshot would SIGTERM them, hitting an unrelated process. Bail with an empty sweep when the root row is absent; the caller's own root kill still runs, so nothing legitimate is leaked. Hardening related to #9191 (does not resolve its multi-utility symptom, which points to an external process-group signal). Co-authored-by: Orca <[email protected]>
📝 WalkthroughWalkthroughPTY descendant collection now stops when the captured root process row is missing, returning no process group or descendants. Tests cover missing and recycled root PIDs, including cases where other processes reference the vacated PID, and verify that termination sends no signals or schedules escalation timers. 🚥 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
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 463b4bd4-27ef-416b-9e84-2fb87857b699
📒 Files selected for processing (2)
src/main/pty-descendant-termination.test.tssrc/main/pty-descendant-termination.ts
| it('captures no descendants and signals nothing when the root PID was already recycled', async () => { | ||
| // End-to-end proof for the #9191-class hazard: the captured PTY root (500) is | ||
| // gone; only unrelated live processes reference its vacated PID. Neither the | ||
| // snapshot nor the terminator may touch them. |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Rename the test to match the fixture.
This fixture models a missing/vacated root with stale ppid references, not a root PID recycled to a live process. Rename the test to avoid claiming coverage for the explicitly out-of-scope scenario.
…(hardening, stablyai#9191 investigation) (stablyai#10069) Co-authored-by: Orca <[email protected]>
Description
Hardens Orca's POSIX PTY descendant-termination sweep so it can never signal an unrelated process whose PID coincides with an already-exited PTY root.
collectDescendantRows(rootPid, table)builds a terminal's descendant tree by walkingppidlinks fromrootPid. Previously, if the root's own row was absent from thepssnapshot (the root had already exited), it still walkedchildrenByPpid.get(rootPid)and returned any rows that happened to point at that vacated PID as "descendants" — whichterminateDescendantSnapshotthen SIGTERMs. Because PIDs are recycled, during a teardown race those "descendants" can belong to a completely unrelated, newly-spawned process.The fix is one guard: if the root row is absent from the snapshot, sweep nothing.
Scope / honesty
This is defense-in-depth for the recycled-PID class, not a claimed fix for a specific field crash. It closes the case where the swept root PID has been vacated (no live occupant). It deliberately does not try to verify identity when a PID was recycled to a live process — that needs birth-time identity threaded from spawn, a larger change. Filed in the context of investigating #9191 (unexpected Utility/GPU SIGTERM), but that crash's multi-utility signature points to an external process-group signal, not this path — so this PR does not claim to resolve #9191.
ELI5
When Orca closes a terminal, it first lists the terminal's "child" programs so it can shut them down too. It finds them by asking the OS "who are process 12345's children?" But if process 12345 already quit, the operating system quickly hands that number to a brand-new, unrelated program. The old code would then shut down that stranger's children by mistake. The fix: if process 12345 isn't actually alive in the snapshot, don't shut anything down — there's nothing of ours left to clean up.
Proof of fix
The 3 added/updated tests fail on the current code and pass with the one-line guard (verified by reverting only the source guard and keeping the tests):
The end-to-end test drives the real
captureDescendantSnapshot→terminateDescendantSnapshotpath with apstable where the captured root PID (500) is gone and only unrelated live processes reference it, and asserts theSignalSenderis never called.Proof of zero regression
The guard changes behavior in exactly one case — an absent root — which is never legitimate at capture time. The design contract requires capturing before the root is killed (
killWithDescendantSweepsnapshots, then callskillRoot()), so the root is alive/present in every real sweep. And when a root exits naturally, its real descendants have already reparented to pid 1 (ppid ≠ rootPid), so the old code collected nothing legitimate there either — the only rows it could have collected were PID-reuse coincidences. The caller's ownkillRoot()still runs whendescendantsis empty, so the root is never leaked.Full teardown/kill surface, all green:
pty-descendant-termination.test.tsproviders/local-pty-provider.test.tsdaemon/session.test.tsdaemon/terminal-session-teardown.test.ts+pty/posix-pty-process-groups.integration.test.tstsc --noEmit -p config/tsconfig.node.jsonoxlint(changed files)Adversarial review
Reviewed by an independent skeptic sub-agent focused on regression-hunting and honest scoping (see result summary in PR thread).
Made with Orca 🐋