Skip to content

fix(heartbeat): fold cleanup into canonical exit trap - #101

Merged
joelteply merged 1 commit into
canaryfrom
fix/heartbeat-trap-clobber
Apr 25, 2026
Merged

joelteply merged 1 commit into
canaryfrom
fix/heartbeat-trap-clobber

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Summary

Trap-clobber fix discovered after PR #100 landed. Bash traps are last-set-wins per shell; my heartbeat-cleanup trap (set at gist-publish) was being silently overwritten by the canonical cmd_connect exit trap (set later). Effect: graceful Ctrl-C left host gist orphaned, forcing next joiner to wait 90s for heartbeat staleness.

Fix: single canonical trap reads heartbeat PID + gist id from scope-local files and reaps everything in one place.

Test plan

  • scenario_heartbeat 8/8 (kill -9 path unaffected)
  • airc.pid records all 3 PIDs (host + acceptor + heartbeat)
  • Joel's macOS canary tab — graceful Ctrl-C should now leave a clean slate

🤖 Generated with Claude Code

The heartbeat-cleanup trap I set inside the gist-publish block was being
clobbered by the canonical cmd_connect exit trap set later in the same
shell — bash traps are last-set-wins per shell. Effect: graceful Ctrl-C
killed the acceptor + children but left the host gist orphaned, forcing
the next joiner to wait ~90s for heartbeat staleness before taking over.

Fix: stash heartbeat PID + gist id to scope-local files
(heartbeat.pid, host_gist_id) at gist-publish time. The canonical exit
trap reads those files and reaps everything in one place — accept loop,
heartbeat loop, gist deletion, pidfile cleanup. cmd_part still does its
own gist delete + room_gist_id cleanup as before; the trap is the
defense-in-depth path for SIGTERM / Ctrl-C / Monitor-tab-close.

Verification: scenario_heartbeat 8/8 still passes after restructure.
airc.pid now correctly records host + acceptor + heartbeat PIDs.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings April 25, 2026 20:47
@joelteply
joelteply merged commit b28ca31 into canary Apr 25, 2026
3 checks passed
@joelteply
joelteply deleted the fix/heartbeat-trap-clobber branch April 25, 2026 20:48

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 bash trap-clobbering issue in cmd_connect host mode where the heartbeat/gist cleanup trap was being overwritten, leaving room gists orphaned on graceful exit (Ctrl‑C / SIGTERM). The PR centralizes heartbeat + gist cleanup into the canonical cmd_connect exit trap by persisting the heartbeat PID and gist id into scope-local state files.

Changes:

  • Replace the heartbeat-site EXIT/INT/TERM trap with persisted state files (heartbeat.pid, host_gist_id) for later cleanup.
  • Extend the canonical cmd_connect trap to kill the heartbeat loop and delete the hosted gist on graceful exit.
  • Include the heartbeat PID (when present) in airc.pid so airc teardown can reap it.

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

Comment thread airc
Comment on lines +2500 to 2511
_exit_hb_pid=""
_exit_gist_id=""
[ -f "$AIRC_WRITE_DIR/heartbeat.pid" ] && _exit_hb_pid=$(cat "$AIRC_WRITE_DIR/heartbeat.pid" 2>/dev/null)
[ -f "$AIRC_WRITE_DIR/host_gist_id" ] && _exit_gist_id=$(cat "$AIRC_WRITE_DIR/host_gist_id" 2>/dev/null)
[ -n "$_exit_hb_pid" ] && kill $_exit_hb_pid 2>/dev/null
if [ -n "$_exit_gist_id" ] && command -v gh >/dev/null 2>&1; then
gh gist delete "$_exit_gist_id" --yes >/dev/null 2>&1
fi
rm -f "$AIRC_WRITE_DIR/airc.pid" "$AIRC_WRITE_DIR/heartbeat.pid" "$AIRC_WRITE_DIR/host_gist_id" 2>/dev/null
for p in $PAIR_PID $(pgrep -P $PAIR_PID 2>/dev/null) $(pgrep -P $$ 2>/dev/null); do
kill $p 2>/dev/null
done
Comment thread airc
Comment on lines +2489 to +2491
_hb_pid_persisted=""
[ -f "$AIRC_WRITE_DIR/heartbeat.pid" ] && _hb_pid_persisted=$(cat "$AIRC_WRITE_DIR/heartbeat.pid" 2>/dev/null)
echo "$$ $PAIR_PID $_hb_pid_persisted" > "$AIRC_WRITE_DIR/airc.pid"
Comment thread airc
Comment on lines +2489 to +2490
_hb_pid_persisted=""
[ -f "$AIRC_WRITE_DIR/heartbeat.pid" ] && _hb_pid_persisted=$(cat "$AIRC_WRITE_DIR/heartbeat.pid" 2>/dev/null)
Comment thread airc
Comment on lines +2502 to 2510
[ -f "$AIRC_WRITE_DIR/heartbeat.pid" ] && _exit_hb_pid=$(cat "$AIRC_WRITE_DIR/heartbeat.pid" 2>/dev/null)
[ -f "$AIRC_WRITE_DIR/host_gist_id" ] && _exit_gist_id=$(cat "$AIRC_WRITE_DIR/host_gist_id" 2>/dev/null)
[ -n "$_exit_hb_pid" ] && kill $_exit_hb_pid 2>/dev/null
if [ -n "$_exit_gist_id" ] && command -v gh >/dev/null 2>&1; then
gh gist delete "$_exit_gist_id" --yes >/dev/null 2>&1
fi
rm -f "$AIRC_WRITE_DIR/airc.pid" "$AIRC_WRITE_DIR/heartbeat.pid" "$AIRC_WRITE_DIR/host_gist_id" 2>/dev/null
for p in $PAIR_PID $(pgrep -P $PAIR_PID 2>/dev/null) $(pgrep -P $$ 2>/dev/null); do
kill $p 2>/dev/null
joelteply added a commit that referenced this pull request Apr 25, 2026
I dropped the airc: prefix in PR #100 thinking it was a broken
plugin-namespace string. Wrong diagnosis. The prefix is namespacing —
without it, /update, /list, /join, /version, /teardown collide with
any other skill in the user's ~/.claude/skills/ that uses the same
generic verb.

Reverts only the skill name change. Heartbeat work (#100, #101) stays.

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.

2 participants