Skip to content

MCP HTTP integration test: let the server bind its own port - #11777

Merged
danielhanchen merged 1 commit into
mainfrom
fix/mcp-test-port-race
Sep 24, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
fix/mcp-test-port-race

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

tests/test_mcp_http_integration.py picked a port with _free_port() (bind port 0, read the number, close the socket) and passed that number to the server process to bind later. Under xdist another worker can take the port in that window. The (Python 3.13, l-r) shard on #11766 hit exactly that:

ERROR tests/test_mcp_http_integration.py::test_credentials_reach_the_server_and_stay_apart - RuntimeError: server died:
ERROR:    [Errno 98] error while attempting to bind on address ('127.0.0.1', 42883): [errno 98] address already in use

The race has a quieter failure mode as well: the readiness probe only checks that something accepts connections on the port, so it can connect to whichever process now holds it and hand the tests a URL pointing at the wrong server.

Change

  • The server script binds port 0 itself, keeps the socket, prints PORT <n> and serves on that socket via uvicorn.Server.run(sockets=[sock]). The harness reads the port from that first line; an early exit ends the pipe and is reported as before.
  • _free_port is removed; no port number crosses a process boundary before it is bound.

This is a test harness fix only. No product code changes.

Test

  • New test_the_server_comes_up_when_a_port_the_harness_could_pick_is_taken: holds a listening socket and makes every port this process is handed by the OS that one. On the old harness it fails (the returned URL points at the held port); here it passes.
  • tests/test_mcp_http_integration.py: 11 passed, also under -n 4.

_free_port bound port 0, closed the socket and passed the number to the
server process to bind later. Another xdist worker could take the port in
between, and the server died with address already in use (seen on the l-r
shard of #11766). Worse, the readiness probe could then connect to whichever
process now held the port. The server now binds port 0 itself, keeps the
socket and reports the port before serving.

The new test makes every port this process is handed by the OS one that is
already listening; it fails on the old harness and passes here.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-24T06:45:43.909404Z 5863aa0 Manual request
🔒 Security Review ⚠️ Failed 2026-09-24T06:43:37.325507Z 5863aa0 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 5863aa0b91

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit fb9dc1e into main Sep 24, 2026
33 of 35 checks passed
@danielhanchen
danielhanchen deleted the fix/mcp-test-port-race branch September 24, 2026 06:47
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…i#11777)

_free_port bound port 0, closed the socket and passed the number to the
server process to bind later. Another xdist worker could take the port in
between, and the server died with address already in use (seen on the l-r
shard of unslothai#11766). Worse, the readiness probe could then connect to whichever
process now held the port. The server now binds port 0 itself, keeps the
socket and reports the port before serving.

The new test makes every port this process is handed by the OS one that is
already listening; it fails on the old harness and passes here.
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.

1 participant