Files
Max Isbey 36991d8bc1 fix: eliminate port allocation race in test_streamable_http fixtures
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.
2026-03-18 16:03:02 +00:00
..