Skip to content

fix(attach): detach transport via setsid -f so Monitor exit does not kill it - #522

Merged
joelteply merged 2 commits into
canaryfrom
ce/detach-attach-transport-setsid
May 5, 2026
Merged

joelteply merged 2 commits into
canaryfrom
ce/detach-attach-transport-setsid

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Closes the third Windows blocker on #511

After #518 cleared the empty-value argparse crash and #521 fixed the --attach identity leak, Windows + Claude Code Monitor + airc join --attach still produced no live transport. airc status reported airc process: not running, airc msg refused with monitor down, and the transport log mtime froze at "Found canonical gist" — exactly when SIGHUP would cascade.

Root cause

_join_spawn_transport_for_attach spawned via ( ... ) >>"$_log" 2>&1 & — same session + pgroup as the launcher bash. On Windows, the Monitor's wsl bash -lc 'airc join --attach' is the controlling-terminal session leader; when it returns, the kernel SIGHUPs the entire pgroup. Every child in the join tree (handshake accept_one, bearer_cli recv loops, reminder_timer_loop, _mesh_rediscover_loop) dies before airc.pid is ever written. Foreground bash on Mac doesn't hit this because the user's interactive shell stays alive holding children open.

Fix

Detach the transport into a new session + pgroup with setsid -f. Kernel-cascade SIGHUP from the launcher's pgroup no longer reaches the transport. Watchdog reworked: setsid -f returns 0 immediately after fork so $! is no longer useful — airc.pid written by airc itself is the canonical liveness signal.

if [ "${AIRC_NO_DETACH:-0}" != "1" ] && command -v setsid >/dev/null 2>&1; then
  setsid -f env AIRC_NO_ATTACH=1 AIRC_BACKGROUND_OK=1 \
    "$AIRC_SELF" join \
    ${_spawn_args[@]+"${_spawn_args[@]}"} \
    >>"$_log" 2>&1
else
  # macOS / minimal envs: keep the existing background-subshell form
  ( trap '' HUP
    AIRC_NO_ATTACH=1 AIRC_BACKGROUND_OK=1 exec "$AIRC_SELF" join \
      ${_spawn_args[@]+"${_spawn_args[@]}"}
  ) >>"$_log" 2>&1 &
fi

Verified end-to-end on Windows 11 + WSL2 + Claude Code Monitor

$ airc join --attach   (via Monitor)
$ pgrep -af airc
30211 /bin/bash /home/joel/.airc-src/airc join
30584 ...airc_core.handshake accept_one ...
30591 ...airc_core.bearer_cli recv ...
$ airc status
airc process: AIRC background process running for scope (PID 30211)
transport health: starting; bearers spawning per-channel
$ airc msg --channel general "windows ops-ok ..."
→ #general (broadcast)

Pre-fix: same sequence produced zero airc processes after Monitor's bash returned.

Test plan

  • Windows + Claude Code Monitor: transport survives, msg accepted (verified above)
  • macOS: existing inline-spawn form still active when setsid is absent or AIRC_NO_DETACH=1; should be unchanged behavior
  • Linux non-Monitor: foreground airc join --attach keeps working — setsid path used (transport still survives correctly), or AIRC_NO_DETACH=1 reverts
  • CI: bash -n already passes; existing integration tests don't simulate launcher exit so they should be unaffected. A test for "transport survives parent exit" would need a separate harness — happy to add if codex points at where to plug in.

🤖 Generated with Claude Code

…ill it

Closes the third Windows blocker tracked on #511.

After #518 (empty-value argparse) and #521 (--attach identity leak),
Windows + Claude Code Monitor + `airc join --attach` still produced no
working transport. `airc status` reported `airc process: not running`
and `airc msg` refused with `monitor down` even though the canonical
events ("Hosting as 'continuum-b69f'", "Found canonical gist") landed
in the transport log. The transport-log mtime stops within a couple of
seconds of the Monitor's `wsl bash -lc 'airc join --attach'` returning,
exactly when SIGHUP would cascade through the launcher's pgroup.

Root cause: `_join_spawn_transport_for_attach` started the transport
as `( ... ) >>"$_log" 2>&1 &` — same session and pgroup as the
launcher bash. On Windows, the launcher is the controlling-terminal
session leader; when it returns, the kernel SIGHUPs the entire pgroup.
The transport bash + airc_core.handshake accept_one + bearer_cli recv
loops + reminder_timer_loop + _mesh_rediscover_loop all die before
they ever write `airc.pid`. Foreground bash on Linux/Mac doesn't hit
this because the user's interactive shell stays alive holding children
open. (See the post-65adceb retest in #511 — transport-log frozen at
"Found canonical gist", no fresh writes for 12+ hours.)

Fix: detach the transport into a new session+pgroup with `setsid -f`
on systems where it's available. The kernel-cascade SIGHUP from
launcher exit no longer reaches the transport. Watchdog reworked to
rely solely on the `airc.pid` file (written by airc itself once the
transport reaches the bearer-spawn point); `setsid -f` returns 0
immediately after fork, so `$!` is no longer a useful liveness signal.

Verified end-to-end on Windows 11 + WSL2 + Claude Code Monitor:

  $ airc join --attach   (via Monitor)
  $ pgrep -af airc
  30211 /bin/bash /home/joel/.airc-src/airc join
  30584 ...handshake accept_one ...
  30591 ...bearer_cli recv ...
  $ airc status
  airc process: AIRC background process running for scope (PID 30211)
  $ airc msg --channel general "..."
  → #general (broadcast)

Pre-fix: same sequence produced zero airc processes after Monitor's
bash returned.

Fallback: AIRC_NO_DETACH=1 forces the inline `( ... ) &` form for
harness tests that want to reap the transport via the parent process
tree. macOS where setsid may be missing falls back to the inline form
automatically (Mac doesn't hit the SIGHUP cascade because the
launcher shell is foreground and stays alive).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

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

This PR hardens airc join --attach so the scope-local transport can outlive the launcher shell, which targets the Windows/WSL Monitor failure reported in #511.

Changes:

  • Reworks attach startup to prefer a detached setsid -f launch, with a HUP-ignoring fallback path.
  • Switches the attach watchdog to rely on airc.pid readiness instead of the launcher job PID when detaching.
  • Adds extra --attach scrubbing so recursive join calls do not leak the flag into positional target/name parsing.

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

Comment on lines +272 to +276
if [ "${AIRC_NO_DETACH:-0}" != "1" ] && command -v setsid >/dev/null 2>&1; then
setsid -f env AIRC_NO_ATTACH=1 AIRC_BACKGROUND_OK=1 \
"$AIRC_SELF" join \
${_spawn_args[@]+"${_spawn_args[@]}"} \
>>"$_log" 2>&1
Comment on lines +272 to +276
if [ "${AIRC_NO_DETACH:-0}" != "1" ] && command -v setsid >/dev/null 2>&1; then
setsid -f env AIRC_NO_ATTACH=1 AIRC_BACKGROUND_OK=1 \
"$AIRC_SELF" join \
${_spawn_args[@]+"${_spawn_args[@]}"} \
>>"$_log" 2>&1
Comment thread lib/airc_bash/cmd_connect.sh Outdated
Comment on lines +233 to +260
# Approach: ignore SIGHUP in the subshell BEFORE exec'ing airc-self.
# POSIX semantics: a process started with SIGHUP set to SIG_IGN keeps
# SIG_IGN across exec() AND across fork() — every grandchild
# (handshake accept_one, bearer_cli recv loops, reminder timer,
# rediscover loop) inherits the same disposition unless it explicitly
# installs its own handler. Python's runtime resets SIG_DFL for many
# signals but PRESERVES SIG_IGN for SIGHUP unless code overrides it,
# so the python sidecars survive too.
#
# This keeps the watchdog at the bottom of the function intact:
# `kill -0 "$_transport_pid"` still tracks the wrapper subshell, and
# `_monitor_alive_with_bearer_fallback` reads airc.pid which airc
# writes after spawn. Both stay valid because the subshell does
# survive the parent shell's exit now.
#
# Mac's existing path doesn't hit this because Mac's launcher is a
# foreground bash that holds children alive until the user closes the
# tab — the subshell never receives SIGHUP. The trap is harmless
# there.
# Detach the transport into its own session+pgroup so SIGHUP from the
# launcher's session leader exit doesn't cascade. On Windows + Claude
# Code Monitor (`wsl bash -lc 'airc join --attach'`) the launcher
# bash is the controlling-terminal session leader; when it returns,
# kernel SIGHUPs the entire pgroup. `setsid -f` forks the transport
# into a new session AND a new pgroup AND disconnects it from the
# controlling terminal — the kill-all-on-launcher-exit semantics no
# longer apply.
#
@joelteply

Copy link
Copy Markdown
Contributor Author

Codex review/update:

  • Windows success signal is real: Mac continuum received [2026-05-05T15:26:14Z] continuum-8e97: windows, and Mac status reported last remote message from continuum-8e97. That proves this was not just a local Windows island.
  • Reviewed the attach detach path. The setsid -f shape matches the WSL Monitor lifetime failure: detached session/process group, liveness verified by airc.pid/status rather than $!, fallback path for systems without setsid or AIRC_NO_DETACH=1.
  • I removed a stale comment block that still described the earlier trap HUP approach and claimed the watchdog tracked the wrapper PID. Code behavior is unchanged; comments now match the final setsid implementation.

Local validation on Mac after the comment cleanup:

  • bash -n lib/airc_bash/cmd_connect.sh airc test/integration.sh
  • git diff --check
  • bash test/integration.sh attach_starts_background_transport -> 3 passed
  • bash test/integration.sh attach_spawn_strips_attach_flag -> 2 passed
  • ./airc doctor --tests python_units -> 14 passed

Waiting on CI rerun. If green, I think #522 is canary-mergeable. After canary merge, promotion validation should require named markers from Windows, Mac Claude tabs, and Codex, plus status/health output from each real scope.

@joelteply
joelteply merged commit af52dad into canary May 5, 2026
5 checks passed
@joelteply
joelteply deleted the ce/detach-attach-transport-setsid branch May 5, 2026 15:31
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