@@ -430,6 +430,28 @@ func ensureServer(cmd *cobra.Command, hostURL *url.URL) error {
430 _, statErr := os.Stat(hostURL.Host)
430 _, statErr := os.Stat(hostURL.Host)
432 case statErr == nil:
432 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,
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)
433 restarted, err := restartIfStale(cmd, hostURL)
455 restarted, err := restartIfStale(cmd, hostURL)
434 if err != nil {
456 if err != nil {
435 slog.Warn("Failed to check server version", "error", err)
457 slog.Warn("Failed to check server version", "error", err)