feat(server): add helper to detect stale unix sockets

Christian Rocha and Charm Crush created

Adds a helper that recognizes when a unix socket file is left on disk
but no process is listening on it, so later code can clean it up safely.

Co-Authored-By: Charm Crush <crush@charm.land>

Change summary

internal/server/socket.go | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)

Detailed changes

internal/server/socket.go 🔗

@@ -0,0 +1,24 @@
+//go:build !windows
+
+package server
+
+import (
+	"errors"
+	"io/fs"
+	"net"
+	"syscall"
+)
+
+// isStaleSocketErr reports whether err indicates a Unix-domain socket file
+// exists on disk but no process is listening on it (a stale or orphaned
+// socket). It returns false for nil and for timeout errors.
+func isStaleSocketErr(err error) bool {
+	if err == nil {
+		return false
+	}
+	var netErr net.Error
+	if errors.As(err, &netErr) && netErr.Timeout() {
+		return false
+	}
+	return errors.Is(err, syscall.ECONNREFUSED) || errors.Is(err, fs.ErrNotExist)
+}