Thanks to visit codestin.com
Credit goes to github.com

Skip to content

shell: clean up stale ssh.sock on --reconnect when the master is already dead#5165

Merged
AkihiroSuda merged 1 commit into
lima-vm:masterfrom
gaurav0107:fix/4913-limactl-shell-reconnect-fails-when-the-p
Jul 2, 2026
Merged

shell: clean up stale ssh.sock on --reconnect when the master is already dead#5165
AkihiroSuda merged 1 commit into
lima-vm:masterfrom
gaurav0107:fix/4913-limactl-shell-reconnect-fails-when-the-p

Conversation

@gaurav0107

Copy link
Copy Markdown
Contributor

Summary

limactl shell --reconnect <inst> is the documented escape hatch (added in #3125) for resetting a wedged SSH multiplexed session. Today it fails with exit status 255 when the SSH ControlMaster socket file (<instDir>/ssh.sock) still exists but the master process is already dead — which is exactly the wedged state --reconnect is meant to recover from.

sshutil.IsControlMasterExisting is a pure os.Stat; it does not verify the master is alive. After an unclean hostagent exit (kill -9, OOM, host crash, or the Cygwin/msys2 emulation file outliving its process) the socket survives. ssh -O exit then fails and the error was returned directly, so --reconnect exited non-zero with no recovery.

Changes

  • Add sshutil.IsControlMasterRunning(instDir) — dials the control socket to check whether a master is actually listening (a stale socket has no listener).
  • Add sshutil.RemoveStaleControlMaster(instDir) — removes the control socket only when no master is listening; a live master's socket is never touched, and a missing socket is a no-op.
  • In cmd/limactl/shell.go, on ssh.ExitMaster failure during --reconnect, remove the socket only if it is confirmed stale; if a master is still alive the original error is still returned.

This keeps the behavior unchanged for a live master (a genuine ssh -O exit failure is still surfaced) and recovers automatically when the master is already dead.

Test plan

  • New unit tests in pkg/sshutil/sshutil_test.go (TestControlMasterStale):
    • stale socket (no listener) is detected not-running and removed
    • absent socket is a no-op
    • a live unix listener is detected running and is preserved
  • go test ./pkg/sshutil/...
  • go vet ./...
  • gofmt -l (clean)
  • go build ./...

Fixes #4913

@gaurav0107 gaurav0107 marked this pull request as ready for review June 28, 2026 21:17
@gaurav0107 gaurav0107 force-pushed the fix/4913-limactl-shell-reconnect-fails-when-the-p branch from 50f47cc to 4af98f1 Compare June 28, 2026 21:23
Comment thread pkg/sshutil/sshutil.go Outdated
Comment thread pkg/sshutil/sshutil.go Outdated
…ady dead

`limactl shell --reconnect` failed with `exit status 255` when the SSH ControlMaster socket existed but its master process had already exited (unclean hostagent shutdown: kill -9, OOM, host crash) -- the exact wedged state --reconnect is meant to recover from.

Gate the control-socket cleanup on a liveness probe: only remove the socket when no master is listening on it, so a genuine `ssh -O exit` failure against a live master is still surfaced as an error.

Add sshutil.IsControlMasterRunning and sshutil.RemoveStaleControlMaster with unit tests, and use the latter in the shell --reconnect path.

Fixes lima-vm#4913

Signed-off-by: gaurav0107 <[email protected]>
@gaurav0107 gaurav0107 force-pushed the fix/4913-limactl-shell-reconnect-fails-when-the-p branch from 4af98f1 to 7e8cdda Compare June 29, 2026 20:13
@AkihiroSuda AkihiroSuda added this to the v2.2.0 milestone Jul 2, 2026

@AkihiroSuda AkihiroSuda left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@AkihiroSuda AkihiroSuda merged commit bd961b9 into lima-vm:master Jul 2, 2026
35 checks passed
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.

limactl shell --reconnect fails when the previous ssh master crashed (stale ssh.sock not cleaned up)

2 participants