Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix(server): the port-in-use remedy never printed on Windows

Darshan Jain5568c00b1f95
diff --git a/internal/server/addrinuse_other.go b/internal/server/addrinuse_other.go
new file mode 100644
index 00000000..e3a543cc
--- /dev/null
+++ b/internal/server/addrinuse_other.go
@@ -0,0 +1,14 @@
+//go:build !windows
+
+package server
+
+import (
+ "errors"
+ "syscall"
+)
+
+// isAddrInUse reports whether err is the kernel refusing a bind because
+// something already holds the address.
+func isAddrInUse(err error) bool {
+ return errors.Is(err, syscall.EADDRINUSE)
+}
diff --git a/internal/server/addrinuse_test.go b/internal/server/addrinuse_test.go
new file mode 100644
index 00000000..e6189470
--- /dev/null
+++ b/internal/server/addrinuse_test.go
@@ -0,0 +1,72 @@
+package server
+
+import (
+ "errors"
+ "fmt"
+ "net"
+ "syscall"
+ "testing"
+)
+
+// TestIsAddrInUseRecognizesARealBindConflict pins the platform's own answer
+// rather than a constant this test picked.
+//
+// The check used to be errors.Is(err, syscall.EADDRINUSE) inline. That is
+// correct on unix and always false on Windows, where a taken address comes back
+// as winsock's WSAEADDRINUSE (10048) and syscall.EADDRINUSE names an
+// APPLICATION_ERROR placeholder (536870914) that nothing returns. The call
+// compiled and ran on both platforms and answered "no" on one of them, so
+// `pinchtab server` printed the raw winsock sentence with none of the guidance
+// that explains it — on what is the most ordinary startup failure there is,
+// starting a server while the daemon already holds the port.
+func TestIsAddrInUseRecognizesARealBindConflict(t *testing.T) {
+ held, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ t.Fatalf("could not take a port to hold: %v", err)
+ }
+ defer func() { _ = held.Close() }()
+
+ _, err = net.Listen("tcp", held.Addr().String())
+ if err == nil {
+ t.Fatal("second listen on a held address succeeded; no conflict to classify")
+ }
+ if !isAddrInUse(err) {
+ var errno syscall.Errno
+ errors.As(err, &errno)
+ t.Fatalf("isAddrInUse did not recognize a real bind conflict: %v (errno %d)", err, uintptr(errno))
+ }
+}
+
+// Unrelated failures must not collect the port-in-use advice, which would send
+// the reader looking for a process that is not there.
+func TestIsAddrInUseIgnoresOtherErrors(t *testing.T) {
+ for _, err := range []error{
+ fmt.Errorf("some other failure"),
+ syscall.ENOENT,
+ fmt.Errorf("wrapped: %w", syscall.EACCES),
+ } {
+ if isAddrInUse(err) {
+ t.Errorf("isAddrInUse(%v) = true, want false", err)
+ }
+ }
+}
+
+// TestStartupFatalHintsFireForARealBindConflict is the positive half that was
+// missing. TestStartupFatalHintsOnlyForAddressInUse already pins that an
+// unrelated error produces nothing — which stayed true on Windows for the worst
+// possible reason, because the classifier said "not in use" to everything.
+func TestStartupFatalHintsFireForARealBindConflict(t *testing.T) {
+ held, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ t.Fatalf("could not take a port to hold: %v", err)
+ }
+ defer func() { _ = held.Close() }()
+ _, bindErr := net.Listen("tcp", held.Addr().String())
+ if bindErr == nil {
+ t.Fatal("second listen on a held address succeeded")
+ }
+
+ if got := startupFatalHints(bindErr); len(got) == 0 {
+ t.Error("a real bind conflict produced no hints")
+ }
+}
diff --git a/internal/server/addrinuse_windows.go b/internal/server/addrinuse_windows.go
new file mode 100644
index 00000000..7be15f8b
--- /dev/null
+++ b/internal/server/addrinuse_windows.go
@@ -0,0 +1,30 @@
+//go:build windows
+
+package server
+
+import (
+ "errors"
+ "syscall"
+)
+
+// wsaAddrInUse is winsock's WSAEADDRINUSE, which is what Windows actually
+// returns for a taken address.
+//
+// It is not syscall.EADDRINUSE. Go's Windows errno table defines that name as a
+// placeholder in the APPLICATION_ERROR range — 536870914 — which no syscall
+// ever returns, so errors.Is(err, syscall.EADDRINUSE) is false for every real
+// bind conflict on this platform. A plain check against it therefore compiled,
+// ran, and silently answered "no" every time.
+//
+// Declared inline for the same reason internal/bridge/state_lock_windows.go
+// declares its ERROR_* codes: it keeps golang.org/x/sys/windows out of the
+// import graph for the sake of one constant.
+const wsaAddrInUse syscall.Errno = 10048
+
+func isAddrInUse(err error) bool {
+ if errors.Is(err, syscall.EADDRINUSE) {
+ return true
+ }
+ var errno syscall.Errno
+ return errors.As(err, &errno) && errno == wsaAddrInUse
+}
diff --git a/internal/server/server.go b/internal/server/server.go
index 5fef181b..9bc6eda1 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -2,7 +2,6 @@ package server
import (
"context"
- "errors"
"fmt"
"log/slog"
"net"
@@ -49,7 +48,7 @@ func fatalStartup(stage string, err error) {
}
func startupFatalHints(err error) []string {
- if !errors.Is(err, syscall.EADDRINUSE) {
+ if !isAddrInUse(err) {
return nil
}
return []string{