Skip to content

Commit ee78c9c

Browse files
wesmclaude
andcommitted
fix: bind to correct host when probing for available ports
FindAvailablePort was checking :port (all interfaces) but the server binds to host:port (127.0.0.1). If 127.0.0.1:8080 was already in use, the probe could pass on 0.0.0.0:8080 and the actual bind would fail. Now probes the same host:port the server will use. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7ffa99e commit ee78c9c

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

cmd/agentsview/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func runServe(args []string) {
115115

116116
go startPeriodicSync(engine)
117117

118-
port := server.FindAvailablePort(cfg.Port)
118+
port := server.FindAvailablePort(cfg.Host, cfg.Port)
119119
if port != cfg.Port {
120120
fmt.Printf("Port %d in use, using %d\n", cfg.Port, port)
121121
}

internal/server/server.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"log"
88
"net"
99
"net/http"
10+
"strconv"
1011
"strings"
1112
gosync "sync"
1213
"time"
@@ -200,10 +201,12 @@ func (s *Server) Shutdown(ctx context.Context) error {
200201
return srv.Shutdown(ctx)
201202
}
202203

203-
// FindAvailablePort finds an available port starting from the given port.
204-
func FindAvailablePort(start int) int {
204+
// FindAvailablePort finds an available port starting from the
205+
// given port, binding to the specified host.
206+
func FindAvailablePort(host string, start int) int {
205207
for port := start; port < start+100; port++ {
206-
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
208+
addr := net.JoinHostPort(host, strconv.Itoa(port))
209+
ln, err := net.Listen("tcp", addr)
207210
if err == nil {
208211
ln.Close()
209212
return port

internal/server/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (te *testEnv) writeSessionFile(
133133
// base URL. The server is shut down when the test finishes.
134134
func (te *testEnv) listenAndServe(t *testing.T) string {
135135
t.Helper()
136-
port := server.FindAvailablePort(40000)
136+
port := server.FindAvailablePort("127.0.0.1", 40000)
137137
te.srv.SetPort(port)
138138

139139
var serveErr error

0 commit comments

Comments
 (0)