From 7b05505db7e964e400b779f76e8a2023bda93fe1 Mon Sep 17 00:00:00 2001 From: Clawd Date: Wed, 18 Feb 2026 20:46:53 -0800 Subject: fix: port allocator scans existing ports to avoid collisions - Collapse multi-line allocScript to single line (fixes SSH+sudo mangling) - Scan /etc/ship/ports/* to find highest port in use - Use max(next_port, highest_used + 1) to prevent collisions - Fixes issue where stale/missing next_port caused port conflicts --- cmd/ship/deploy_impl_v2.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'cmd/ship') diff --git a/cmd/ship/deploy_impl_v2.go b/cmd/ship/deploy_impl_v2.go index 5b68dc3..ec5c4a3 100644 --- a/cmd/ship/deploy_impl_v2.go +++ b/cmd/ship/deploy_impl_v2.go @@ -308,14 +308,8 @@ func allocatePort(client *ssh.Client, name string) (int, error) { } // Allocate new port atomically using flock - // This reads next_port, increments it, and writes back while holding a lock - allocScript := ` -flock -x /etc/ship/.port.lock sh -c ' - mkdir -p /etc/ship/ports - PORT=$(cat /etc/ship/next_port 2>/dev/null || echo 9000) - echo $((PORT + 1)) > /etc/ship/next_port - echo $PORT -'` + // Scans existing port files to avoid collisions even if next_port is stale + allocScript := `flock -x /etc/ship/.port.lock sh -c 'mkdir -p /etc/ship/ports; NEXT=$(cat /etc/ship/next_port 2>/dev/null || echo 9000); MAX=8999; for f in /etc/ship/ports/*; do [ -f "$f" ] && P=$(cat "$f" 2>/dev/null) && [ "$P" -gt "$MAX" ] 2>/dev/null && MAX=$P; done; PORT=$(( NEXT > MAX ? NEXT : MAX + 1 )); echo $((PORT + 1)) > /etc/ship/next_port; echo $PORT'` out, err = client.RunSudo(allocScript) if err != nil { return 0, fmt.Errorf("failed to allocate port: %w", err) -- cgit v1.2.3