diff options
| author | Clawd <ai@clawd.bot> | 2026-02-18 20:46:53 -0800 |
|---|---|---|
| committer | Clawd <ai@clawd.bot> | 2026-02-18 20:46:53 -0800 |
| commit | 7b05505db7e964e400b779f76e8a2023bda93fe1 (patch) | |
| tree | c1786f2c906bab8d595268fa3e7d1a9b73210529 | |
| parent | 805541df0f22f1200692ddfae7b2f199a89a8f7b (diff) | |
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
| -rw-r--r-- | cmd/ship/deploy_impl_v2.go | 10 |
1 files changed, 2 insertions, 8 deletions
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) { | |||
| 308 | } | 308 | } |
| 309 | 309 | ||
| 310 | // Allocate new port atomically using flock | 310 | // Allocate new port atomically using flock |
| 311 | // This reads next_port, increments it, and writes back while holding a lock | 311 | // Scans existing port files to avoid collisions even if next_port is stale |
| 312 | allocScript := ` | 312 | 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'` |
| 313 | flock -x /etc/ship/.port.lock sh -c ' | ||
| 314 | mkdir -p /etc/ship/ports | ||
| 315 | PORT=$(cat /etc/ship/next_port 2>/dev/null || echo 9000) | ||
| 316 | echo $((PORT + 1)) > /etc/ship/next_port | ||
| 317 | echo $PORT | ||
| 318 | '` | ||
| 319 | out, err = client.RunSudo(allocScript) | 313 | out, err = client.RunSudo(allocScript) |
| 320 | if err != nil { | 314 | if err != nil { |
| 321 | return 0, fmt.Errorf("failed to allocate port: %w", err) | 315 | return 0, fmt.Errorf("failed to allocate port: %w", err) |
