Skip to content

tailcat: hang up PTY sessions when the SSH connection closes - #215

Open
tntrock wants to merge 1 commit into
tailscale:mainfrom
tntrock:ssh-pty-hangup
Open

tntrock wants to merge 1 commit into
tailscale:mainfrom
tntrock:ssh-pty-hangup

Conversation

@tntrock

@tntrock tntrock commented Dec 3, 2026 •

Copy link
Copy Markdown

runWithPTY waited only for the pty to close, which happens when the child exits. If the client's connection closed while the child was still running, nothing told the child, so it and its pty stayed around until the tailcat server exited. OpenSSH hangs up the terminal in that case.

Send SIGHUP to the child's process group when the session's context is canceled. The child is started with Setsid, so its process group ID is its PID. The watcher is stopped before cmd.Wait reaps the child, so it can't signal a process group ID that has since been reused.

This covers connections that are closed. A client that vanishes without closing its connection (killed together with its tailcat ProxyCommand, or a dropped network) is never noticed by the server at all, PTY or not; that needs some kind of keepalive and isn't addressed here. The Windows ConPTY path is unchanged.

The new TestSSHPTYHangupOnDisconnect fails on main (the child is still running 10s after the client closes its connection) and passes with this change.

Tested end-to-end with the tailcat CLI and OpenSSH 9.6: 10 PTY clients whose ssh was killed left 10 processes and 10 ptys behind on main, and none with this change.

Fixes #214

🤖 Generated with Claude Code

runWithPTY waited only for the pty to close, which happens when the
child exits. If the client's connection closed while the child was
still running, nothing told the child, so it and its pty stayed around
until the tailcat server exited. OpenSSH hangs up the terminal in that
case.

Send SIGHUP to the child's process group when the session's context
is canceled. The child is started with Setsid, so its process group ID
is its PID. The watcher is stopped before cmd.Wait reaps the child, so
it can't signal a process group ID that has since been reused.

This covers connections that are closed. A client that vanishes
without closing its connection (killed together with its tailcat
ProxyCommand, or a dropped network) is never noticed by the server at
all, PTY or not; that needs some kind of keepalive and isn't addressed
here. The Windows ConPTY path is unchanged.

The new TestSSHPTYHangupOnDisconnect fails on main (the child is
still running 10s after the client closes its connection) and passes
with this change.

Fixes tailscale#214

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: AllenYen <20695184+tntrock@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

ssh: PTY session process keeps running after the client disconnects

1 participant