Skip to content

fix(terminal): never sweep descendants of an already-exited PTY root (hardening, #9191 investigation) - #10069

Merged
nwparker merged 1 commit into
mainfrom
nwparker/fix-9191-subprocess-sigterm
Jul 23, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/fix-9191-subprocess-sigterm

Conversation

@nwparker

@nwparker nwparker commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

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 walking ppid links from rootPid. Previously, if the root's own row was absent from the ps snapshot (the root had already exited), it still walked childrenByPpid.get(rootPid) and returned any rows that happened to point at that vacated PID as "descendants" — which terminateDescendantSnapshot then 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.

if (!rootRow) {
  return { rootPgid: null, descendants: [], capturedAtMs }
}

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):

WITHOUT the guard (current main):
 ❯ pty-descendant-termination.test.ts (24 tests | 3 failed)
   × sweeps nothing when the root row is already gone (a vacated PID has no findable tree)
   × never collects sibling utilities when a recycled root PID resolves to a live parent
   × captures no descendants and signals nothing when the root PID was already recycled

WITH the guard:
   Test Files  1 passed (1)
        Tests  24 passed (24)

The end-to-end test drives the real captureDescendantSnapshot → terminateDescendantSnapshot path with a ps table where the captured root PID (500) is gone and only unrelated live processes reference it, and asserts the SignalSender is 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 (killWithDescendantSweep snapshots, then calls killRoot()), 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 own killRoot() still runs when descendants is empty, so the root is never leaked.

Full teardown/kill surface, all green:

Suite Result
pty-descendant-termination.test.ts 24 passed
providers/local-pty-provider.test.ts 88 passed
daemon/session.test.ts 70 passed
daemon/terminal-session-teardown.test.ts + pty/posix-pty-process-groups.integration.test.ts 95 passed (combined run)
tsc --noEmit -p config/tsconfig.node.json exit 0
oxlint (changed files) clean

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 🐋

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]>
@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

PTY 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)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is on-topic and detailed, but it omits required template sections like Summary, Screenshots, AI Review Report, Security Audit, and Notes. Rewrite the PR body to match the template and add the missing sections, including explicit screenshot/no-visual-change, testing checklist, review report, security audit, and notes.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: preventing descendant sweeps for already-exited PTY roots.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

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

📥 Commits

Reviewing files that changed from the base of the PR and between ffe54a5 and 514915c.

📒 Files selected for processing (2)
  • src/main/pty-descendant-termination.test.ts
  • src/main/pty-descendant-termination.ts

Comment on lines +133 to +136
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.

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.

📐 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.

@nwparker
nwparker merged commit d152039 into main Jul 23, 2026
2 checks passed
@nwparker
nwparker deleted the nwparker/fix-9191-subprocess-sigterm branch July 23, 2026 03:31
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
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.

[Bug]: Electron Utility subprocesses receive unexpected SIGTERM — app restarts without user action (~10-20 min after startup, escalated with v1.4.144)

1 participant