Repository navigation
Conversation
…se cycles A process-table capture is not atomic, so PID reuse can produce duplicate or cyclic-looking ppid links -- a descendant whose pid a later row reuses as an ancestor. collectDescendantsFromIndex walked children with no visited set, so such a cycle sent it into an unbounded DFS: it re-pushed the same rows forever, grew the terminal-host heap to the V8 4 GB cap, and aborted the daemon, killing every terminal session it hosted (stablyai#16797). The sibling ppid walk in pty-descendant-termination.ts already guards this exact case ("PID reuse can produce duplicate or cyclic-looking rows, which must not hang the Electron main thread"). This brings the shared walk to parity with a visited set. Well-formed tables have unique pids, so the guard never fires and the existing deepest-last ordering is unchanged. Adds process-table-index.test.ts: a 2-node cycle and a self-parented row now terminate, a descendant that reuses the root pid does not revisit it, and the acyclic result still matches the plain per-call walk exactly. Co-Authored-By: Claude Fable 5.1 <[email protected]>
📝 WalkthroughWalkthrough
Priority: ⬆️ High Severity of issue fixed: High Merge Risk: 🔵 Low · up to The current traversal behavior is bounded, but its regression test can miss a root-PID reuse failure. Add the exact-result assertion before merge to preserve this safeguard. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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: Advanced
Run ID: 388cf678-2591-467b-9e1f-10074ff9190d
📒 Files selected for processing (2)
src/shared/process-table-index.test.tssrc/shared/process-table-index.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
| expect(got.length).toBeLessThanOrEqual(rows.length) | ||
| expect(got.some((r) => r.pid === 60)).toBe(true) |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Assert that the reused root PID is excluded.
The current assertions pass if visited does not start with rootPid. That regression returns PID 50 after PID 60 but still produces a bounded result. Assert the exact PID list, or assert that PID 50 is absent.
Proposed test update
- expect(got.length).toBeLessThanOrEqual(rows.length)
- expect(got.some((r) => r.pid === 60)).toBe(true)
+ expect(got.map((r) => r.pid)).toEqual([60])📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| expect(got.length).toBeLessThanOrEqual(rows.length) | |
| expect(got.some((r) => r.pid === 60)).toBe(true) | |
| expect(got.map((r) => r.pid)).toEqual([60]) |
There was a problem hiding this comment.
ℹ️ No critical issues — the guard itself is correct and well pinned by the new tests. One coverage gap worth a follow-up.
Reviewed changes
- Bound
collectDescendantsFromIndexagainst PID-reuse cycles — a per-callvisitedset (seeded withrootPid) now gates every push onto the walk stack, so a cyclic or duplicatechildrenByPpidlink can no longer grow it without bound. On unique-pid tables the guard never fires, so ordering and depths are unchanged; only the guard is new. - New
src/shared/process-table-index.test.ts— asserts exact acyclic parity against a pre-guard reference walk, plus bounded termination for a 2-node cycle, a self-parented row (pid === ppid), and root-pid reuse. The parity test is an exacttoEqual, and the cycle tests would hang/OOM against the pre-fix walk, so the suite genuinely fails with the bug present.
ℹ️ The relay's private descendant walk still has the unguarded loop
collectDescendants in src/relay/pty-shell-utils.ts:182 is a byte-for-byte copy of the pre-fix walk — same stack.map(...) seed, same unconditional stack.push — and it is live on the relay's foreground RPC (pty-handler.ts:2623 and :2831), which is POSIX-only. The PR body says the shared walk is "also on the POSIX foreground path; verified on Node/Windows" and that local and relay hosts "both fed this walk," but the relay does not call collectDescendantsFromIndex; it feeds its own copy the same index. So a pid-reuse cycle still grows that stack without bound on a relay host. This is pre-existing, not a regression, and the Windows daemon target of #16797 is fully covered by this PR — mentioning it only because the stated relay coverage is inaccurate.
Technical details
# Relay foreground walk is not covered by the shared guard
## Affected sites
- `src/relay/pty-shell-utils.ts:182-196` — private `collectDescendants(index, rootPid)` reproduces the pre-guard stack walk exactly.
- `src/relay/pty-shell-utils.ts:223` — the only caller, via `getForegroundProcessNameFromProcessTable`.
- `src/relay/pty-shell-utils.ts:275, 294` → `getRecognizedForegroundDescendant` → `pty-handler.ts:2623, 2831` — live relay foreground path.
- The file already imports `getProcessTableIndex` / `ProcessTableIndex` from the shared module but not `collectDescendantsFromIndex`.
## Required outcome
The relay's POSIX foreground walk must terminate on a cyclic or duplicate `childrenByPpid` link, matching `collectDescendantsFromIndex`, `collectDescendantRows` (`main/providers/agent-foreground-process-remote-evidence.ts:126`), and the ppid walk in `main/pty-descendant-termination.ts:198`.
## Suggested approach (optional)
Delete the private copy and import `collectDescendantsFromIndex` from `../shared/process-table-index` (same signature: `(index, rootPid) => (Row & { depth })[]`). Alternatively add the same `visited`/`pushUnvisited` guard. Either way, a relay-side regression test mirroring the new cycle test would pin it.
## Open questions for the human (optional)
Is the relay-side copy intentionally retained (e.g. a different row contract) or a leftover from the shared-index extraction?DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

Drafted by Warden (Claude Code) on Brynn's behalf; Brynn reviewed.
ELI5
When Orca lists Windows processes to find what's running in a terminal, it walks the "who is a child of whom" tree. The Windows process list isn't a frozen snapshot, and Windows reuses process IDs aggressively, so once in a while the list says A is B's parent and B is A's parent — a loop. The tree walk had no "already seen this one" check, so it went round that loop forever, piling up objects until the terminal host ran out of memory and died, taking every open terminal with it. This adds the missing "already seen" check.
What Changed
collectDescendantsFromIndexinsrc/shared/process-table-index.tsnow carries avisitedset of pids and never pushes a pid it has already queued. That is the only behavioural change. On a well-formed table every pid is unique, so the guard never fires and the existing deepest-last ordering is byte-for-byte unchanged.Adds
src/shared/process-table-index.test.ts.Why
A process-table capture (Toolhelp32 on Windows,
pson POSIX) is not atomic, and Windows recycles pids quickly. A capture taken during heavy process churn can therefore contain a pid-reuse cycle: a row whoseppidpoints at one of its own descendants, because that descendant's pid was reused by a newly-spawned ancestor between reads.childrenByPpidthen hasA -> BandB -> A, and the walkpushes
{...row, depth}objects without bound. In the daemon this runs on the Windows foreground-process path (windows-foreground-process-rows.ts,agent-foreground-process.ts) on every pane refresh, so the terminal host's V8 heap climbs from ~200 MB to the 4 GB cap in ~20 s and aborts — killing every terminal session the daemon hosts.The sibling ppid walk in
pty-descendant-termination.tsalready guards this exact hazard, with the comment: "ps is not an atomic snapshot. PID reuse can produce duplicate or cyclic-looking rows, which must not hang the Electron main thread." When the shared index machinery was extracted, the descendant walk did not carry that guard. This restores parity.The approach is the minimal one: a visited set is what the sibling walk uses, it is O(1) per node, and it changes nothing for well-formed tables.
Linked Issue
Fixes #16797
This is a confirmed cause of the silent OOM aborts reported there on Windows. It is independent of the missing-native-addon staging in #19522 — the OOM reproduces on both the native process-tree path and the PowerShell CIM fallback, because both feed the same index into this walk.
Visual Proof
N/A— no UI or interaction change; this is an internal process-table traversal fix.Testing
Deterministic reproduction against the real function (Node 24,
--max-old-space-size=128):EXIT=134(V8 heap OOM) in under 8 s.pid === ppid) and a descendant that reuses the root pid both terminate; and for a normal acyclic tree the output is identical (same rows, same order, same depths) to the original per-call walk.New tests in
src/shared/process-table-index.test.tscover: acyclic parity with the plain walk, a 2-node cycle, a self-parented row, and root-pid reuse.Platforms: the trigger is Windows-specific (pid reuse + Toolhelp32/CIM non-atomicity), but the walk is platform-agnostic pure logic and is also on the POSIX foreground path; verified on Node/Windows. I could not run the full
pnpm test/typecheck/buildlocally (partial clone without installed deps) — the new test follows the repo's existing vitest conventions and is syntax-clean; CI should confirm.AI Disclosure
Investigated and written with Claude Code (Anthropic). Models: Claude Fable 5.1, with a fallback to Claude Opus 4.8 during the infrastructure work. Root cause was confirmed by the deterministic repro above, not inferred. Brynn reviewed before filing.
Review
AI code-review summary against the contribution checklist:
Set<number>and an O(1) membership check per node; the walk stays linear in table size instead of unbounded.Agent skill upstream boundary
docs/reference/agent-skill-sharing-upstream-boundary.mdand copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.Notes
Security: bounded traversal only. Cross-platform: pure logic, Windows + POSIX. Remote SSH: covered (same daemon path). Mobile: N/A. Backwards compatibility: output unchanged for well-formed tables. Performance: linear, one set allocation.
Author: Brynn Bendixen (X handle not provided).
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred) — not run locally (partial clone); relying on CI