@@ -430,6 +430,28 @@ func ensureServer(cmd *cobra.Command, hostURL *url.URL) error {
430430 _, statErr := os.Stat(hostURL.Host)
431431 switch {
432432 case statErr == nil:
433+ // Probe the socket explicitly before the version-check
434+ // path. A stale unix socket file (the previous server
435+ // exited without cleaning up) would otherwise make
436+ // restartIfStale spin on a non-responsive endpoint; here
437+ // we detect it with a short DialTimeout and remove the
438+ // orphaned file so the normal spawn path can run.
439+ if hostURL.Scheme == "unix" {
440+ conn, dialErr := net.DialTimeout(
441+ hostURL.Scheme, hostURL.Host, 200*time.Millisecond,
442+ )
443+ if dialErr == nil {
444+ conn.Close()
445+ } else if server.IsStaleSocketErr(dialErr) {
446+ slog.Warn("Stale socket detected, removing",
447+ "path", hostURL.Host, "error", dialErr)
448+ if err := os.Remove(hostURL.Host); err != nil && !errors.Is(err, fs.ErrNotExist) {
449+ return fmt.Errorf("failed to remove stale server socket %q: %v", hostURL.Host, err)
450+ }
451+ needsStart = true
452+ break
453+ }
454+ }
433455 restarted, err := restartIfStale(cmd, hostURL)
434456 if err != nil {
435457 slog.Warn("Failed to check server version", "error", err)