Skip to main content

drop_exited_shell

Function drop_exited_shell 

Source
fn drop_exited_shell(state: &AppState, agent_block_id: &str, id: &str)
Expand description

Treating an exited shell like a stale pointer (the caller returns None) reuses the existing “create a fresh one” fallback in both callers instead of inventing a new response shape.

Also clear the now-stale pointer on the agent’s own block: without this, two concurrent PtyShellCreate calls against the same exited pane would both fall through to handle_pty_shell_create’s atomic-claim transaction, both read the SAME still-set dead pointer, both take the “lost the race, reuse THEIRS” branch pointing at that same dead id, both find it exited too, and both independently execute the non-transactional “vanished, create for real” fallback — leaving one orphaned live controller and two callers holding different shell_ids for what’s supposed to be one pane’s one shell (Codex P2 on PR #3225). Clearing the pointer first makes the already-correct atomic claim transaction see “unclaimed” instead of “claimed by a dead id,” so its existing single-writer serialization (documented on that transaction) resolves concurrent callers to one winner the normal way.

GUARDED, not unconditional (ReAgent P1 on this PR, round 2): resync_controller’s STATUS_DONE read happens outside any lock — a concurrent resync with respawn_if_done=true (e.g. the WS ControllerResyncCommand/TermResyncHandler path, which always revives a STATUS_DONE controller in place) can respawn THIS SAME shell in the gap between that read and this clear. Nulling the pointer unconditionally would then wipe a pointer that’s valid again, orphaning a live, correctly-pointed shell — exactly the one-shell-per- pane invariant this PR exists to protect. Re-read the parent fresh and only clear if its pointer still names id, the same compare-before-clear pattern this file already uses a few hundred lines down for the identical class of race (see the parent.meta.get(META_KEY_SHELL_SUBBLOCK_ID) == Some(child_id.as_str()) guard in the spawn-failure rollback path). Not fully transactional (same as that existing pattern) — a failed/skipped clear just costs one more round trip through the fallback chain, not correctness.