Skip to content

Allow proxy to auto-assign free ports for sessions#1

Open
paddybyers wants to merge 1 commit into
mainfrom
proxy-allocated-ports
Open

Allow proxy to auto-assign free ports for sessions#1
paddybyers wants to merge 1 commit into
mainfrom
proxy-allocated-ports

Conversation

@paddybyers
Copy link
Copy Markdown
Member

Summary

  • Make the port field optional in POST /sessions. When omitted or set to 0, the proxy binds to an OS-assigned free port via net.Listen("tcp", ":0").
  • Eliminates the TOCTOU race where callers had to guess which ports were available — the proxy now assigns a guaranteed-free port.
  • Fully backwards-compatible: callers that pass an explicit positive port see identical behavior.

Changes

  • listener.go: StartSessionListener returns (int, error), extracts actual port from listener address, sets session.Port under mutex.
  • server.go: Validation changed from port <= 0 to port < 0. Uses returned actualPort for response and logging.
  • session.go: Added omitempty to CreateSessionRequest.Port json tag.
  • docs/API.md: port documented as optional with auto-assign behavior.
  • proxy_test.go: Updated validation test for negative port; added TestSessionAutoPort.

Test plan

  • go build ./... compiles cleanly
  • go test ./... — all 27 tests pass
  • New TestSessionAutoPort verifies auto-assigned port is valid and listening
  • Existing tests with explicit ports still pass (backwards compatibility)
  • Manual smoke test: create session without port, verify response contains auto-assigned port

🤖 Generated with Claude Code

Make the `port` field optional in POST /sessions. When omitted or set
to 0, the proxy binds to an OS-assigned free port, eliminating the
TOCTOU race where callers had to guess available ports. Existing
callers that pass an explicit port are unaffected.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@paddybyers paddybyers requested review from lmars and ttypic May 10, 2026 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant