Skip to content

fix(streamable): bind port 0 and read Address() instead of racy port probing - #6796

Closed
suantea wants to merge 1 commit into
stacklok:mainfrom
suantea:fix/streamable-freeport-race
Closed

suantea wants to merge 1 commit into
stacklok:mainfrom
suantea:fix/streamable-freeport-race

Conversation

@suantea

@suantea suantea commented Oct 11, 2026

Copy link
Copy Markdown

Fixes #6045 — the second getFreePort bind-close-rebind (TOCTOU) race in the streamable proxy tests, same shape as the one #6034 documented and #6044 fixed in the vMCP copy.

What changed

NewHTTPProxy(host, 0, ...) + Start() now binds synchronously on an OS-assigned port, and the proxy exposes the real bound address via a new Address() accessor — the same shape #6044 used for vMCP. The tests stop probing ports entirely:

  • Start() binds the listener synchronously when the port is 0 and sets server.Addr from the listener, so Address() is valid as soon as Start returns. Configured ports keep their previous ListenAndServe behaviour.
  • streamable_proxy_integration_test.go: deleted getFreePort (the helper Second getFreePort bind-close-rebind race in streamable proxy tests #6045 names) and streamable_proxy_spec_test.go: deleted pickFreePort.
  • The port-typed test helpers are gone: startProxyWithBackend/startProxyOnly take no port and bind 0; establishSession/openGETStream/postSSE/postJSON/startInitTestProxy take *HTTPProxy and build URLs from Address().
  • All ~35 call sites across the package now bind port 0 and read the address back instead of probe-then-rebind.
  • A comment at each former helper site explains why the probe-listener pattern must not be reintroduced (per the Bind the vMCP test server's port directly #6044 precedent).

This removes the race for every test in the package at once — fixing just the two helpers would have left the same TOCTOU shape in the other call sites.

Verification

  • go test -race -count=1 ./pkg/transport/proxy/streamable/ — pass
  • go build ./... — clean
  • gofmt / go vet — clean

…probing

The streamable proxy tests probed a free port with bind-close-rebind
(getFreePort/pickFreePort) and then bound the proxy to it later, leaving a
TOCTOU window (documented in stacklok#6034/stacklok#6044, ~0.08% collisions under churn).
Eliminate the pattern at the source:

- Start() binds synchronously when the port is 0 and exposes the real
  address via a new Address() accessor, so callers can hand the proxy port 0
  and read the bound address back.
- Tests now construct proxies with port 0 and build URLs from
  proxy.Address(); the getFreePort/pickFreePort helpers and the port-typed
  test helpers (startProxyWithBackend, startProxyOnly, establishSession,
  openGETStream, postSSE, postJSON, startInitTestProxy) are gone.
- A comment at each former helper site explains why the probe-listener
  pattern must not be reintroduced (per stacklok#6044 precedent).
@jhrozek

jhrozek commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Hi @suantea, I'm closing this PR along with the rest of this batch. Nothing personal, it's just not how we're set up to take contributions.

Our contributing guide asks you to comment on an issue and wait for someone on the team to assign it to you before you start. We do that so two people don't end up fixing the same thing, and so we can agree on the approach first. A few of these PRs landed on issues that already had someone assigned, and I can't review 20+ PRs across unrelated parts of the codebase from one person anyway.

If you'd like to contribute, please read CONTRIBUTING.md, pick one issue, and ask to be assigned. A good-first-issue is a fine place to start. Happy to take a proper look at that one.

@jhrozek jhrozek closed this Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Second getFreePort bind-close-rebind race in streamable proxy tests

2 participants