Repository navigation
fix(session): reach process groups after leader reap - #10431
satyam-thakur wants to merge 1 commit into
Conversation
|
Intent: Keep agent process groups reachable through teardown after their leader exits and is reaped, without signaling a recycled group. |
Design Review (Fable 5, fork) — ✅ PASSDesign-level review of Design-Verdict: PASS Verify the new real-child fixtures in test_pid_lifecycle stay flake-free on macOS/CI runners, per testing-conventions' real-spawn and reap rows. [DESIGN-REVIEWED] e417445 |
GPT 5.6 Review (fork) — ✅ no blocking findingsReviewed Review detailsFINDING -- src/kiro_crew/acp/client.py:3813 -- function-local |
First Principles Review (Fable 5, fork) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Confirm the new teardown-time witness scan (unbounded /proc reads inside shutdown) cannot push pooled discards past Not justified as shipped
What this change shipsInventory (10 items) — 9 justifiedIntent: FIX — a child that ignores SIGTERM survives cleanup once its group leader is reaped first (linked issue #10245, real failing-on-base process test).
[FIRST-PRINCIPLES-REVIEWED] e417445 |
Opus 4.8 Review (fork) — ✅ no blocking findingsReviewed Review detailsI've now examined the candidate against the actual code and the platform helpers. Analysis of Candidate 1 ( The candidate's leak requires the incarnation gate to refuse while our child is genuinely alive and ours. Tracing the gate (
The only path to a refusal for a still-alive leader is The withholding is also a deliberate, spec-documented invariant ("A captured group never degrades to the leader PID"), and even under a hypothetical transient refusal No new Step-2 finding grounds to 80+: the reaped-leader path, the non-force SIGKILL escalation, and the group-None fallback preservation all check out against the diff. No findings. [OPUS-REVIEWED] e417445 |
Maintainer decision: accept this platform residual to retain group-complete cleanup, or require a larger design that keeps a dedicated leader or supervisor alive through teardown? |
9f49739 to
9382a91
Compare
9382a91 to
b0ee622
Compare
Keep the process-group identity captured at spawn so cleanup can reach descendants after the group leader has been reaped. Deriving the group from the leader pid at signal time fails exactly when it matters: os.getpgid raises once the leader is gone, so a descendant that ignores SIGTERM survives teardown unsignalled. Both spawn sites record the group while the leader is provably alive and expose it through one public LLMProvider capability, bound to the leader's start identity so a recycled pgid is refused rather than signalled. Preserve that group across pooled shutdown, and resolve it before the pid guards in _sync_kill_provider: shutdown clears the client pid while the captured group remains, so a pid-first return left the group unsignalled and leaked the resistant descendant. A cleared pid over a live group now escalates group-only, with the incarnation rechecked before every signal and no pid fallback. A leaderless group needs an original member to authorize it. Liveness alone cannot separate our own surviving tree from a stranger that inherited the pgid and then lost its own leader, because both reach the same populated-but-leaderless shape. Carry ProcessGroupMember witnesses recorded while the leader is still alive to vouch for the group, and authorize a reaped-leader group only when one witness is still alive, still the same incarnation and still in this group. An unreadable identity, a witness that left the group, and a group carrying no witnesses all refuse. Cover the reaped-leader, leaderless-witness and cleared-pid paths with deterministic lifecycle tests.
d109970 to
e417445
Compare
|
Superseded by #11309, which landed the same fix: teardown now signals the process group authorized by group ownership ( One residual remained — a leader already reaped when teardown starts leaves no readable group id, so no group signal is sent at all. That is now #11456, at 51 production lines instead of this PR's 3,188. Closing in favour of those two. |
Problem / Motivation
Agent cleanup keeps only the leader PID. If that leader exits and is reaped first,
getpgid(pid)fails. A child process that ignores the graceful stop signal (SIGTERM) can then survive cleanup.Why it matters
The surviving process tree keeps its memory, file descriptors, and background work. Repeated sessions can leave several trees behind until the host slows down or the user kills them by hand.
What changed (motivation → approach → change)
Linux/macOS process groups are now captured when the agent starts. The capture records the leader's start identity and every group member seen while that leader is still proven current. ACP clients refresh these member witnesses before teardown so late child processes are included.
Cleanup signals the saved group only when it can prove that the group is still ours. A live leader must match its saved identity. After the leader is reaped, one saved member must still be the same process and still belong to that group. Recycled, moved, missing, malformed, or unreadable identities are refused. A refused group never falls back to the same number as a PID.
Direct ACP teardown uses the same saved-group check for
SIGTERMandSIGKILL. This includes a leader that exits during the graceful stop wait and a leader that was already reaped before cleanup starts. Already-reaped cleanup also runs the saved escaped-child sweep after group signaling, so a child that moved out of the group is still removed.LLMProvider.spawned_process_group()exposes the saved group through one public capability. Pool and dashboard cleanup preserve that group after the leader PID is cleared. Potentially long process scans run outside the async event loop.flowchart LR subgraph Before A1[spawn leader]:::ctx --> B1[save leader PID]:::removed --> C1[leader reaped]:::ctx --> D1[child survives]:::removed end subgraph After A2[spawn leader]:::ctx --> B2[save group witnesses]:::added --> C2[leader reaped]:::ctx --> D2[check surviving member]:::changed --> E2[signal saved group]:::added end classDef added fill:#DCFCE7,stroke:#16A34A,color:#14532D,stroke-width:2px classDef changed fill:#FEF3C7,stroke:#D97706,color:#78350F,stroke-width:2px classDef removed fill:#FEE2E2,stroke:#DC2626,color:#7F1D1D,stroke-dasharray:4 3 classDef ctx fill:#E0F2FE,stroke:#0284C7,color:#0C4A6E linkStyle 0,1,2 stroke:#DC2626,stroke-dasharray:4 3 linkStyle 3,4,5,6 stroke:#16A34A,stroke-width:2px🟩 added · 🟨 changed · 🟥 removed · 🟦 unchanged
Cleanup now keeps proof of the original process group, so it can remove child processes after the leader is reaped.
Tests
SIGTERM). Its fixture now holds the leader until the child witness is captured, then releases and reaps it.AsyncMockcapability test; direct call and await counts now prove the capability was not invoked.Manual verification
A controlled Linux process tree confirmed the failure mode and fix. The leader exited and was reaped. The child ignored
SIGTERM. Saved-group escalation removed it. Windows behavior has deterministic unit coverage.Related Issues
Fixes #10245
Pattern harvest
Rule candidate: review-prompt
Pattern: A saved PID or process group used after a reap must carry process-incarnation proof, and failed proof must not fall back to the same numeric ID.
Checklist
feat|fix|docs|refactor|perf|test|chore|ci|build|revert: ...)