Repository navigation
fix(airc): pair-listener parent-watch — orphan-process port-hold (#132) - #185
Conversation
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]>
There was a problem hiding this comment.
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-pidtoairc_core.handshake accept_oneand start a watcher thread that terminates the process when the watched PID disappears. - Update the
aircbash 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.
| # Parent-watch (#132): the loop exits when its own parent disappears | ||
| # (PPID=1 = reparented to init = airc parent bash died). Without |
There was a problem hiding this comment.
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.
| # 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 |
| # 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=$$ |
There was a problem hiding this comment.
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).
| _orphan_parent_pid=$$ | |
| local _orphan_parent_pid=$$ |
| 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) |
There was a problem hiding this comment.
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.
| # --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) |
There was a problem hiding this comment.
--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:.
| if args.watch_pid: | ||
| _start_parent_watch(args.watch_pid) | ||
|
|
There was a problem hiding this comment.
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.
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
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
🤖 Generated with Claude Code