Skip to content

Tear down SSH tunnel on abnormal client disconnect#3897

Merged
bbatsov merged 1 commit intomasterfrom
tunnel-cleanup-on-disconnect
Apr 29, 2026
Merged

Tear down SSH tunnel on abnormal client disconnect#3897
bbatsov merged 1 commit intomasterfrom
tunnel-cleanup-on-disconnect

Conversation

@bbatsov
Copy link
Copy Markdown
Member

@bbatsov bbatsov commented Apr 29, 2026

Found during a wider audit of connection handling.

nrepl--ssh-tunnel-connect spawns a separate ssh subprocess for port forwarding and stashes it on the client buffer's nrepl-tunnel-buffer. The orderly teardown path -- cider-quitcider--close-connection -- already kills the tunnel buffer cleanly.

But the nrepl-client-sentinel path, which fires on abnormal client-process death (server crash, network drop, the client process getting killed), only ran the disconnect hooks and the nrepl--maybe-kill-server-buffer cleanup. Neither of those touches the tunnel, so the ssh subprocess kept running until Emacs itself exited. On macOS / Linux you'd see it via ps; on Windows it's worse since taskkill cleanup also doesn't reach it.

Add the tunnel cleanup right next to the existing nrepl--maybe-kill-server-buffer call in the sentinel, symmetric with how the server gets handled. Same layering: the transport sentinel owns transport-infrastructure cleanup.

`nrepl--ssh-tunnel-connect' spawns a separate `ssh' subprocess for port
forwarding and stashes it on the client buffer's `nrepl-tunnel-buffer'.
The orderly teardown path -- `cider-quit' -> `cider--close-connection'
-- already kills the tunnel buffer.

But the `nrepl-client-sentinel' path, which fires on abnormal client-
process death (server crash, network drop, kill -9), only ran the
disconnect hooks and the maybe-kill-server-buffer cleanup.  Neither
touches the tunnel, so the `ssh' subprocess kept running until Emacs
itself exited.

Add the tunnel cleanup to the sentinel, symmetric with the existing
`nrepl--maybe-kill-server-buffer' call.  Same layering: the transport
sentinel owns transport-infrastructure cleanup.
@bbatsov bbatsov force-pushed the tunnel-cleanup-on-disconnect branch from ecc7303 to 3fa8f2d Compare April 29, 2026 19:51
@bbatsov bbatsov merged commit fd2dfce into master Apr 29, 2026
13 checks passed
@bbatsov bbatsov deleted the tunnel-cleanup-on-disconnect branch April 29, 2026 21:39
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.

1 participant