Skip to content

fix(airc): pair-listener parent-watch — orphan-process port-hold (#132) - #185

Merged
joelteply merged 1 commit into
canaryfrom
fix/airc-orphan-listener-parent-watch
Apr 28, 2026
Merged

joelteply merged 1 commit into
canaryfrom
fix/airc-orphan-listener-parent-watch

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Summary

Fixes #132. When airc parent bash dies (terminal close, kill, Monitor tool teardown), the accept-loop subshell reparents to init but stays alive — re-spawning fresh python listeners every iteration. Existing `getppid()==1` check never fires because each listener's parent is the still-alive accept-loop, not init. Orphan listeners hold the host port, accept incoming pair handshakes, write peer records, and stuff joiner SSH keys into `authorized_keys` — pointing at a dead host.

Two-layer fix

  • Bash accept loop: `while kill -0 PARENT` instead of `while true`. Captures airc bash's PID at startup; loop exits when that PID disappears.
  • Python listener: new `--watch-pid` flag wires the same airc bash PID to a daemon thread that polls `os.kill(pid, 0)` every second. When the parent dies, `os._exit(0)` breaks out of any in-flight `accept()`/`recv()`.

Both layers watch the SAME PID (airc bash), not their immediate parent, because the immediate parent (accept-loop subshell) outlives airc bash by one iteration.

Test plan

  • Orphan repro: SIGKILL airc bash → python exits via parent-watch within 1s, port freed
  • `airc teardown` still works (the existing teardown path is unchanged)
  • Integration suite: 183 passing (vs 180 baseline). Two long-standing flakes resolved: `port 7549 still held after teardown` + `alpha still listening after teardown`. One remaining flake (`beta did NOT successfully pair`) is unrelated — different scenario, will triage separately.

🤖 Generated with Claude Code

When the airc parent bash dies (terminal close, kill, Monitor tool
teardown), the accept-loop subshell reparents to init but stays alive,
re-spawning fresh python listeners every iteration. Each listener's
own getppid() points at the orphaned bash subshell — never at init —
so the existing `getppid()==1` socket-timeout check never fires.

Result: orphan listeners hold the host port, accept incoming pair
handshakes, write peer records, and stuff joiner SSH keys into
authorized_keys — pointing at a dead host with no relay behind it.
This is the cause of the integration suite's "port still held after
teardown" + "alpha still listening" flakes.

Two-layer fix:

1. Bash accept loop: `while kill -0 PARENT` instead of `while true`.
   Captures airc bash's PID at startup; loop exits the moment that
   PID disappears, no fresh python is spawned past that point.

2. Python listener: --watch-pid flag wires the same airc bash PID
   into a daemon thread that polls os.kill(pid, 0) every second.
   When the parent dies, os._exit(0) breaks out of any in-flight
   accept()/recv() — covers the in-handshake case the bash check
   misses while a python is mid-iteration.

Both layers watch the SAME PID (airc bash), not their immediate
parent, because the immediate parent (accept-loop subshell) outlives
airc bash by one iteration in the orphan scenario.

Verified:
- Orphan repro: SIGKILL airc bash → python exits via parent-watch
  within 1s, port freed (was: ghost listener + held port forever).
- airc teardown still works (watch-pid is opt-in via --watch-pid 0).
- Integration suite: 183 passing (vs 180 baseline on canary). Two
  long-standing flakes resolved: "port 7549 still held after teardown"
  + "alpha still listening after teardown". One remaining flake
  ("beta did NOT successfully pair") is unrelated — different scenario.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings April 28, 2026 02:05
@joelteply
joelteply merged commit e9f3f05 into canary Apr 28, 2026
2 checks passed
@joelteply
joelteply deleted the fix/airc-orphan-listener-parent-watch branch April 28, 2026 02:05

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 #132 by ensuring the host-side pair-listener cannot outlive the original airc parent bash, preventing orphaned listeners from holding the port and continuing to accept/authorize joiners after the controlling terminal/process is gone.

Changes:

  • Add --watch-pid to airc_core.handshake accept_one and start a watcher thread that terminates the process when the watched PID disappears.
  • Update the airc bash accept-loop to run only while the captured parent PID is still alive, and pass that PID down to Python.
  • Expand inline comments documenting the orphan-process/ghost-handshake failure mode and the two-layer mitigation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
lib/airc_core/handshake.py Introduces --watch-pid and a parent-watch thread to terminate accept_one during blocking I/O when the parent PID dies.
airc Wraps the accept-loop in a kill -0-guarded loop tied to the original airc bash PID and forwards that PID to Python.

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

Comment thread airc
Comment on lines +2784 to +2785
# Parent-watch (#132): the loop exits when its own parent disappears
# (PPID=1 = reparented to init = airc parent bash died). Without

Copilot AI Apr 28, 2026

Copy link

Choose a reason for hiding this comment

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

The comment says the loop exits when its own parent disappears ("PPID=1 = reparented to init"), but the code actually loops while kill -0 "$_orphan_parent_pid" succeeds (i.e., while the captured airc bash PID exists). Consider updating the comment to match the implementation to avoid confusion during future debugging.

Suggested change
# Parent-watch (#132): the loop exits when its own parent disappears
# (PPID=1 = reparented to init = airc parent bash died). Without
# Parent-watch (#132): the loop keeps running only while the captured
# airc parent bash PID is still alive (checked via kill -0). Without

Copilot uses AI. Check for mistakes.
Comment thread airc
# 1s parent-watch thread (see airc_core.handshake._start_parent_watch)
# to catch the in-flight-handshake case; this loop check covers the
# between-iterations case before the next python is spawned.
_orphan_parent_pid=$$

Copilot AI Apr 28, 2026

Copy link

Choose a reason for hiding this comment

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

cmd_connect generally uses local for function-scoped variables, but _orphan_parent_pid=$$ is introduced without local, making it a global shell variable. For consistency and to avoid accidental cross-function interference, declare it as local _orphan_parent_pid=$$ (and similarly keep it scoped to this block).

Suggested change
_orphan_parent_pid=$$
local _orphan_parent_pid=$$

Copilot uses AI. Check for mistakes.
Comment on lines +125 to +132
def _watch():
while True:
try:
os.kill(watch_pid, 0)
except (OSError, ProcessLookupError):
# airc bash gone — break out of any blocking syscall.
os._exit(0)
time.sleep(1)

Copilot AI Apr 28, 2026

Copy link

Choose a reason for hiding this comment

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

In _start_parent_watch, the except (OSError, ProcessLookupError) branch will treat any OSError from os.kill(watch_pid, 0) (including PermissionError / EPERM or other unexpected errors) as “PID is gone” and immediately os._exit(0). That can cause the listener to exit even when the watched process is still alive but not signalable. Consider only exiting on “no such process” (e.g., ProcessLookupError or e.errno == errno.ESRCH) and letting other errors either disable the watch or log to stderr.

Copilot uses AI. Check for mistakes.
Comment on lines +292 to 296
# --watch-pid: airc parent bash PID. The listener spawns a daemon
# thread that os._exit()s the moment this PID disappears (#132).
# 0 disables the watch (legacy callers / direct invocations).
a.add_argument("--watch-pid", type=int, default=0)
a.set_defaults(func=cmd_accept_one)

Copilot AI Apr 28, 2026

Copy link

Choose a reason for hiding this comment

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

--watch-pid is documented as “0 disables the watch”, but negative values are currently accepted and will be treated as truthy in if args.watch_pid:; on POSIX, os.kill(-pid, 0) targets a process group, which is not intended here. Recommend validating --watch-pid as >= 0 (or > 0 to enable) in argparse (custom type) and/or changing the check to if args.watch_pid > 0:.

Copilot uses AI. Check for mistakes.
Comment on lines +143 to +145
if args.watch_pid:
_start_parent_watch(args.watch_pid)

Copilot AI Apr 28, 2026

Copy link

Choose a reason for hiding this comment

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

This PR introduces new orphan/parent-death behavior (--watch-pid + os._exit() from a watcher thread), but the integration suite doesn’t appear to assert the specific failure mode in #132 (kill the launching parent shell and ensure the accept loop + listener exit and the port is freed quickly). Adding a dedicated integration scenario would help prevent regressions in this cross-process lifecycle behavior.

Copilot uses AI. Check for mistakes.
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