Skip to content

Session teardown does not wait for the session's process tree — stragglers race the worktree removal (#139 bug 1) #183

Description

@pbean

Follow-up to #139 (fixed by #182). That PR addressed bug 2 — a failed teardown no longer crashes the run post-merge, degrading to a worktree-teardown-degraded journal event instead. This issue tracks the underlying bug 1: the race itself. A process spawned inside a session can outlive the session and keep writing into the worktree while git is deleting it.

Mechanism

_integrate_unit runs ~2s after session-end. Nothing on that path verifies the session's process tree is dead — only its tmux window:

Consequence (post-#182)

Tolerable but real: a worktree-teardown-degraded warning, a leftover dir under the gitignored run dir (reclaimed by trim/clean), and in the worst case a straggler still consuming resources or writing into a reclaimed path long after the story completed. On the #139 report this fired on 2 of 2 backend stories in one sweep, so it is not rare under test-heavy workloads.

Proposed direction

Extend the #157 verified-kill seam rather than adding a new mechanism:

  1. Harvest window_pane_pids plus their descendant tree before the first kill strike, while the window is provably alive (same pid-reuse safety argument session-end journaled 2h19 after session_timeout_min fires when the session is wedged inside a tool call #157 already established — harvesting after death risks signalling reused pids, and orphaned descendants reparent away from the pane and become unfindable).
  2. After the window dies, poll the harvested tree (kill-0 style, as in the session-end journaled 2h19 after session_timeout_min fires when the session is wedged inside a tool call #157 zombie-poll probe) within teardown_grace_s; force-kill stragglers through the process host.
  3. Only then let the engine proceed to merge + teardown.

Platform notes: descendant walk needs win32 parity through the process-host seam (pgrep -P has no direct equivalent; #157's fake-child/kill-0 test pattern applies). The opencode_http adapter keeps its own teardown copy and needs the same treatment separately — its sessions have no tmux window at all, making the process-tree wait the only teardown signal there.

Acceptance sketch

With a session that backgrounds a writer outliving its turn (the #139 repro), teardown blocks up to teardown_grace_s until the writer is dead, the worktree removes cleanly, and no worktree-teardown-degraded event fires.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions