Skip to content

fix: replace ATCH_SESSION env check with PID ancestry walk#9

Open
DonaldoDes wants to merge 1 commit intomobydeck:mainfrom
DonaldoDes:fix/atch-session-ancestry
Open

fix: replace ATCH_SESSION env check with PID ancestry walk#9
DonaldoDes wants to merge 1 commit intomobydeck:mainfrom
DonaldoDes:fix/atch-session-ancestry

Conversation

@DonaldoDes
Copy link

Summary

  • Replace the ATCH_SESSION environment variable check with a proper PID ancestry verification
  • Master writes shell PID to <socket>.ppid file
  • Attach walks the process tree upward to confirm actual ancestry before blocking

Closes #5

Test plan

  • Stale ATCH_SESSION no longer blocks attach
  • Self-attach from within a session is still correctly blocked
  • 3 regression tests added (no ppid file, dead PID, self PID)

🤖 Generated with Claude Code

…estry check

The anti-recursion guard in attach_main relied solely on ATCH_SESSION to
detect self-attach attempts.  Because ATCH_SESSION is an inherited env var
it could be stale (e.g. the variable set inside a session that has since
been detached-from, or a shell-config export), causing every subsequent
attach to be incorrectly blocked with "cannot attach from within itself".

Fix: the master now writes the pty-child PID to <sockname>.ppid on session
start and removes it on cleanup.  attach_main reads this file and walks the
process-ancestry tree (via libproc on macOS, /proc on Linux) to verify that
the calling process is genuinely a descendant of the session shell before
blocking the attach.  If the .ppid file is absent or the recorded PID is
not an ancestor, ATCH_SESSION is treated as stale and the attach proceeds.

The ancestry check is also hoisted before require_tty() in cmd_attach and
the legacy -a/-A/-c routes so the correct diagnostic fires even without a
controlling terminal.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

ATCH_SESSION env var blocks all attach after detach

1 participant