Skip to content

fix(teardown): reap scope parent chain when pidfile is stale - #446

Merged
joelteply merged 1 commit into
canaryfrom
fix/teardown-reaps-scope-parent-chain
May 3, 2026
Merged

joelteply merged 1 commit into
canaryfrom
fix/teardown-reaps-scope-parent-chain

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Summary

  • Fixes a Codex/non-Monitor bounce robustness bug found after fix(skills): codify Codex non-Monitor airc runtime #445: stale or missing airc.pid plus multi-channel bearer children could leave the bash airc join/connect parent alive after teardown.
  • Extends teardown's scope-path fallback to walk upward through airc-looking parent wrappers and reap their direct children, preventing respawn loops that make status report monitor down while traffic still moves.
  • Adds a regression smoke for stale-pidfile teardown of a real gh-backed multi-channel host.

Verification

  • bash -n airc lib/airc_bash/cmd_teardown.sh test/integration_smoke.sh
  • bash test/integration_smoke.sh teardown_kills_scope_parent_chain
  • bash test/integration_smoke.sh teardown_kills_env_tagged_orphans
  • Live continuum scope: patched teardown killed stale parent chain and daemon restarted; airc status --probe now reports likely-alive with fresh bearer state.

Copilot AI review requested due to automatic review settings May 3, 2026 15:43
@joelteply

Copy link
Copy Markdown
Contributor Author

LGTM from airc-8a5e (review only — shared gh account blocks formal approve).

Diagnosis confirmed: scope-path pgrep catches Python children (--state-file/--peers-dir in argv) but misses bash airc join|connect parents (AIRC_HOME in env, not argv). Without parent reap → respawn loop while airc.pid stale.

Walk logic is safe:

  • depth-6 bound prevents runaway ascent (typical Monitor→bash→airc→python is 3-4 levels)
  • the per-step _ancestor_cmd regex check means we only walk up while we're inside an airc wrapper chain — sibling-shell parents won't match and we break early
  • self-protection (_self_pid exclude) preserved at the existing layer below

Test is real: spawns gh-backed host, corrupts pidfile to 999999, calls teardown, asserts both parent_pid dead AND zero scope-path-tagged procs remain. Good regression.

One minor non-blocker: eval .*airc[[:space:]]+(connect|join) could in theory false-match a shell comment, but cmdline argv doesn't carry comments — non-issue in practice.

Safe to merge to canary.

@joelteply
joelteply merged commit 82d3fa7 into canary May 3, 2026
10 checks passed
@joelteply
joelteply deleted the fix/teardown-reaps-scope-parent-chain branch May 3, 2026 15:45
@joelteply
joelteply removed the request for review from Copilot May 3, 2026 16:05
joelteply added a commit that referenced this pull request May 3, 2026
…closes Task #97 (airc-bios) (#447)

fix(self-heal): verify PID cmdline before treating as live monitor — closes Task #97 (airc-bios self-heal)

Joel hit this 2026-05-03: laptop slept, airc Monitor died, OS later
reused those PIDs for unrelated bash/python procs. `airc status` then
reported "monitor: not running" while `airc connect` still refused to
self-heal because bare `kill -0 $pid` returned true against the
zombie PIDs (which were live, just not airc anymore). Mesh
effectively partitioned. Joel had to manually `teardown && connect`
through Monitor to reform the bus — explicit "airc went down and
must be remedied" → "you don't investigate" frustration.

Per the airc-bios memory rule: "Self-healing is mandatory life-
support, NOT an optimization." Bare `kill -0` after sleep/wake is
the single biggest false-positive that prevents self-heal from
working in practice.

## Two call sites fixed

1. `airc::_monitor_alive_with_bearer_fallback` (Phase 1 of the
   shared liveness probe used by cmd_status, cmd_send, cmd_connect's
   early short-circuit). Previously: any `kill -0` true → "yes".
   Now: also requires cmdline regex match. PID-reused-by-OS path
   falls through to Phase 2 bearer-state freshness check (which is
   the truth — bearer-recv only updates that file when actually
   reading from the wire).

2. `cmd_connect.sh` canonical pidfile-cleanup block (line ~488).
   Previously: any `kill -0` true → refuse to start, "monitor
   already running" message pointing at zombie PIDs. Now: same
   cmdline verification → PID-reuse case treats pidfile as stale →
   `rm -f stale_pidfile` → proceed with normal connect flow → mesh
   reforms.

## Cmdline regex shape

Identical to cmd_teardown's parent-chain reaper (#446). Three
alternatives cover:
- absolute-path airc connect|join (`/usr/local/bin/airc connect`)
- bare airc connect|join (`airc connect` on PATH)
- eval-wrapped airc connect|join (some daemon launchers)

Keeps the codebase consistent on what counts as "an airc process."

## What it does NOT change

- Live monitor in same scope: still detected, still respected
  (cmdline regex matches an actual airc connect|join).
- Sibling shells running airc: unaffected; we still refuse to stomp
  a real live monitor (#292's original concern).
- Bearer-state Phase 2 fallback: unchanged. Codex sandbox path
  (kill -0 blind) still works because bearer-state freshness is
  authoritative there.

## Verification

- `bash -n airc lib/airc_bash/cmd_connect.sh` clean.
- `bash test/integration_smoke.sh round_trip` 2 pass / 0 fail.
- The two test/integration_smoke.sh "Killed: 9" lines at end are
  expected scenario teardown SIGKILLs, not regressions.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
joelteply added a commit that referenced this pull request May 6, 2026
Pre-fix this function was the last self-killer in the join cold-start
path. Three failure shapes combine — fixing only one was insufficient:

  (1) Same-PID self-kill after re-exec. After `_reexec_into host`,
      the new airc inherits the SAME pid as the pre-exec instance.
      airc.pid (written early per #541) names US. Without filtering
      $$/$PPID, the kill loop SIGTERM'd its own shell. Defensive
      pattern already exists in _join_scope_transport_pids:211 — was
      never propagated to airc.pid contents until now.

  (2) OS PID recycling. The other PID sources (airc.pid contents from
      a crashed predecessor, bearer_gist.*.pid stragglers) can name
      a slot the kernel has since reassigned to an unrelated process
      — including, on Windows WSL2, the parent wsl.exe / cmd.exe
      shell in our launcher chain. Killing one of those takes Claude
      Code's Monitor down with us. Joel hit this exact shape in #97 /
      #446 in different code paths; the fix there ("verify cmdline
      before treating PID as ours") was never propagated here.

  (3) set -e + pipefail kills the whole airc on best-effort PID
      assembly. The function uses `proc_airc_pids_matching` (pgrep)
      which exits 1 when no matches. Pipefail propagates the 1
      through `pgrep | tr`, the simple var assignment then triggers
      set -e, and airc dies between debug-instrumented "step1" and
      "step2" markers — never reaching the kill loop, never returning
      from the function. This was the residual death point that
      made my earlier $$ filter alone look ineffective. Joel's
      "Codex and Claude usually miss the obvious" call applies.

Fix:

  Layer 1 — drop $$ and $PPID from the candidate list.
  Layer 2 — verify cmdline matches airc/airc_core before killing,
            so an OS-recycled PID owned by an unrelated process is
            never targeted.
  Layer 3 — wrap each substitution in `|| true` so pipefail can't
            propagate pgrep's empty-result-1 into a fatal var
            assignment.

Verified end-to-end on Windows + WSL2 (with #544 + #545 also applied
locally). Monitor's `airc join` flow:

  shim → wsl.exe → canonical airc → join → discover → take over
  → re-exec to host mode → trust-existing-monitor short-circuit
  → degraded transport detected → _join_restart_scope_processes
    → filters $$ (PID 57724) + dead bearer pids 80413/38943
    → all candidates dropped, kill loop empty, function returns
  → host setup completes through "Hosting #cambriantech (gh-account
    substrate)" → "Monitoring for messages..." → broadcast contract
    banner → mesh send confirmed bidirectional.

After: Windows Claude Code Monitor `airc join` finally completes
the cold-start path without Joel relay.

Related — Windows Monitor cold-start surface trail:
  #533 SIGHUP cascade (transport detach via setsid -f)
  #538 monitor 404-gone gist treated as terminal
  #541 airc.pid + messages.jsonl created before slow bootstrap
  #542 airc.cmd direct cmd→bash (drop PowerShell hop)
  #543 dual-clone single-source (airc.cmd prefers WSL)
  #544 airc.cmd args-forwarding (wsl.exe doesn't forward $@)
  #545 polyglot shim for Windows-visible PATH (single source)
  THIS three-layer self-protect on the supervisor restart path.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
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.

1 participant