Skip to content

Commit 5a17080

Browse files
committed
fix(toolstream): remove stale socket file on restart
When the process exits abnormally the Unix socket file is left behind, causing the next startup to fail with 'socket path already exists'. Probe for a live listener before refusing: if Dial fails the file is stale and gets removed so the new process can bind successfully.
1 parent 7fc5f83 commit 5a17080

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

  • internal/toolstream/transport

internal/toolstream/transport/uds.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,18 @@ func (l *udsListener) Close() error {
3030
return l.Listener.Close()
3131
}
3232

33-
// ListenUDS binds a Unix socket at path; returns an error if path already exists.
33+
// ListenUDS binds a Unix socket at path. If the socket file already exists it
34+
// probes for a live listener; when none is found the stale file is removed so
35+
// the new process can start successfully.
3436
func ListenUDS(path string) (net.Listener, error) {
3537
if _, err := os.Stat(path); err == nil {
36-
return nil, fmt.Errorf("transport: socket path already exists: %s", path)
38+
conn, dialErr := net.Dial("unix", path)
39+
if dialErr != nil {
40+
_ = os.Remove(path)
41+
} else {
42+
_ = conn.Close()
43+
return nil, fmt.Errorf("transport: socket path already in use: %s", path)
44+
}
3745
}
3846

3947
l, err := net.Listen("unix", path)

0 commit comments

Comments
 (0)