Skip to content

fix(connect): _join_restart_scope_processes must not kill self - #546

Merged
joelteply merged 1 commit into
canaryfrom
fix/restart-scope-no-self-kill
May 7, 2026
Merged

joelteply merged 1 commit into
canaryfrom
fix/restart-scope-no-self-kill

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Summary

_join_restart_scope_processes (lib/airc_bash/cmd_connect.sh:167) read PIDs from airc.pid and helpers and killed every one without filtering out $$/$PPID. After _reexec_into host, the new airc inherits the same pid as the pre-exec instance, so airc.pid (written by the pre-exec airc with $$) now names us. The kill loop SIGTERM'd its own shell and the running airc died exit 1, right after printing:

airc join: AIRC process exists but transport is degraded;
restarting this scope's AIRC process.

How it surfaced

Observed live on Windows Monitor once #545's polyglot shim correctly routed Monitor's airc join to the canonical clone (same SHA airc version already reported, but Monitor was running stale code via Git Bash's PATH-resolved full-script copy). Fresh airc detected stale prior heartbeat from a crashed predecessor, called this function — and exit 1'd because airc.pid contained $$.

Note: _join_scope_transport_pids (line ~199) already excludes $$/$PPID (line 211-212). The airc.pid contents path simply wasn't filtered in the same way until now.

Fix

Add a self-filter pass before the kill loop:

+  local _self_filtered_pids="" _candidate
+  for _candidate in $_pids; do
+    case "$_candidate" in
+      ''|*[!0-9]*) continue ;;
+    esac
+    [ "$_candidate" = "$$" ] && continue
+    [ "$_candidate" = "$PPID" ] && continue
+    _self_filtered_pids="$_self_filtered_pids $_candidate"
+  done
+  _pids="$_self_filtered_pids"
   local _p _c
   for _p in $_pids; do
     ...

Applied at the consolidated entry point so all four PID sources (airc.pid, _airc_scope_monitor_formatter_pids, bearer_gist.*.pid, _join_scope_transport_pids) are protected uniformly.

Verification

After deploying both #545 (polyglot shim) and this fix locally:

  • Monitor airc join reaches the supervisor's restart-degraded-transport branch
  • Restart correctly kills only OLD bearer/transport PIDs (e.g. 80413, 38943) — not the running airc parent
  • Host setup proceeds through "Hosting #cambriantech (gh-account substrate)" without exit 1

Related — Windows Monitor cold-start surface trail

PR Fix
#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 supervisor restart no longer kills its own parent

After all of these, the Windows Monitor airc join flow finally completes: shim → wsl.exe → canonical airc → join → discover → take over → re-exec to host → detect stale prior state → restart scope (kills OLD pids, not us) → host setup → heartbeat loop → Monitor stays alive.

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings May 6, 2026 21:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a join/self-heal regression where _join_restart_scope_processes could SIGTERM the currently running airc process after _reexec_into host (because airc.pid may contain the current PID).

Changes:

  • Add a PID self-filtering pass in _join_restart_scope_processes to exclude $$ and $PPID before issuing kill signals.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +192 to +196
case "$_candidate" in
''|*[!0-9]*) continue ;;
esac
[ "$_candidate" = "$$" ] && continue
[ "$_candidate" = "$PPID" ] && continue
Comment thread lib/airc_bash/cmd_connect.sh Outdated
Comment on lines +179 to +188
# Self-kill guard. After `_reexec_into host` the new airc inherits
# the same PID — `airc.pid` written by the pre-exec instance now
# names US. Without this filter, the kill loop below SIGTERM's its
# own shell and the running airc dies right after printing
# "AIRC process exists but transport is degraded; restarting this
# scope's AIRC process." Observed live during Windows Monitor
# post-takeover host-setup window: shim correctly routed to the
# canonical clone, airc detected stale prior heartbeat from a
# crashed predecessor, called this function — and exit 1'd because
# airc.pid contained $$. _join_scope_transport_pids already excludes
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]>
@joelteply
joelteply force-pushed the fix/restart-scope-no-self-kill branch from 890f73c to 8c48de3 Compare May 6, 2026 22:22
@joelteply
joelteply merged commit ead6d6f into canary May 7, 2026
6 checks passed
@joelteply
joelteply deleted the fix/restart-scope-no-self-kill branch May 7, 2026 02:22
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.

2 participants