fix: eliminate test port allocation race by running uvicorn in-thread#2263
Draft
fix: eliminate test port allocation race by running uvicorn in-thread#2263
Conversation
The previous pattern picked a free port via socket.bind(0), released it, then started a uvicorn subprocess hoping to rebind — a TOCTOU race that caused intermittent CI failures when pytest-xdist workers stole the port between release and rebind (ConnectError, 404 against wrong server). Added run_uvicorn_in_thread() which pre-binds the listening socket with port=0 and passes it to uvicorn via server.run(sockets=[sock]). The port is held atomically from bind until shutdown and is known before the server thread even starts — no polling, no race. The kernel's listen queue buffers any connections that arrive during uvicorn startup. Migrated the four test_streamable_http.py fixtures (basic_server, event_server, json_response_server, context_aware_server) that share create_app(). These include the SSE auto-reconnect tests that genuinely need real TCP to exercise connection lifecycle. Running the server in-process means coverage now tracks transport code that was previously subprocess-invisible; adjusted pragmas accordingly (targeted no-cover on unreached error paths, lax no-cover on timing-dependent branches). wait_for_server() is kept for files not touched by this PR.
43777d8 to
21a3979
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces the subprocess + port-race fixture pattern in
tests/shared/test_streamable_http.pywith an in-thread uvicorn helper that pre-binds the listening socket, eliminating the TOCTOU race that caused intermittent CI failures under pytest-xdist.The Problem
Between
getsockname()returning and the subprocess rebinding, another pytest-xdist worker can claim the port — causinghttpx.ConnectError: All connection attempts failed(job 63508538456) on the SSE auto-reconnect tests.PR #1528 attempted worker-specific port ranges but was closed as only reducing (not eliminating) collision probability.
The Fix
tests/test_helpers.pynow providesrun_uvicorn_in_thread(app)which:port=0and callslisten()before starting the server threadserver.run(sockets=[sock])No polling, no race window. The socket is held atomically from
bind()untilsock.close()at fixture teardown.Scope
Migrated the four
test_streamable_http.pyfixtures (basic_server,event_server,json_response_server,context_aware_server) that sharecreate_app(). These include the SSE auto-reconnect tests (test_server_close_sse_stream_via_context,test_streamable_http_client_auto_reconnects,test_streamable_http_multiple_reconnections,test_standalone_get_stream_reconnection,test_streamable_http_client_respects_retry_interval) which genuinely need real TCP to exercise connection lifecycle.Other files with the same pattern (
test_sse.py,test_ws.py,test_http_unicode.py,test_integration.py, security tests) are deliberately left alone here — they don't need real TCP and are being converted to in-memory/ASGITransport in sibling PRs.wait_for_server()is kept for them.Coverage
Running the server in-process means coverage now tracks transport code that was previously subprocess-invisible. Adjusted
# pragma: no cover→# pragma: lax no coveron a handful of branches instreamable_http.pyandtransport_security.pythat are now partially covered depending on which tests exercise them.AI Disclaimer