Title: test: fix WS test port race; narrow to single smoke test covering both transport ends by maxisbey · Pull Request #2267 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: test: fix WS test port race; narrow to single smoke test covering both transport ends by maxisbey · Pull Request #2267 · modelcontextprotocol/python-sdk
X Title: test: fix WS test port race; narrow to single smoke test covering both transport ends by maxisbey · Pull Request #2267 · modelcontextprotocol/python-sdk
Description: Problem tests/shared/test_ws.py had 4 tests sharing a subprocess+uvicorn fixture with a TOCTOU port race: bind port 0 → release → subprocess rebinds. Under pytest-xdist another worker claims the port in the gap, and the WS client connects to the wrong server's HTTP endpoint, yielding a 403 Forbidden on upgrade (job 63414508256). Disposition of the 4 tests Only one of the four was actually testing WebSocket. The other three tested MCP session semantics with WS as an incidental pipe — swap in any transport and they pass identically. Test What it tested Outcome test_ws_client_basic_connection Init + ping over WS — subprotocol handshake, JSON-RPC serialization round-trip Kept test_ws_client_happy_request_and_response read_resource() returns the right content Duplicate of test_client.py::test_read_resource → deleted test_ws_client_exception_handling Server-raised error surfaces as MCPError with correct code Moved to test_client.py::test_read_resource_error_propagates (in-memory) test_ws_client_timeout Session survives a client-side timeout, can issue requests after Duplicate of test_88_random_error.py → deleted ~150 of the old 211 lines were subprocess launcher + port-polling + fixture plumbing shared by all four. With one test remaining on a shared helper, the file is 51 lines. The smoke test test_ws_client_basic_connection now uses run_uvicorn_in_thread (tests/test_helpers.py), which pre-binds the socket and calls listen() before the server thread starts. The port is known immediately — the kernel's listen queue buffers any early connection until uvicorn's loop reaches accept(). No polling, no sleep, no gap for another xdist worker to steal the port. Knock-on effects of in-thread Two subprocess-era assumptions stopped holding once uvicorn runs in the test process: Coverage now reaches server/websocket.py. The function-level # pragma: no cover existed because the subprocess couldn't be instrumented; strict-no-cover (#2305) correctly flags it as stale. Narrowed to the three error paths the smoke test doesn't exercise. The one test now covers both client/websocket.py and server/websocket.py at 100%. pytest's warning filters now apply to uvicorn. uvicorn.Config.load() calls asyncio.iscoroutinefunction() during ASGI autodetection; Python 3.14 deprecates it; filterwarnings=error promotes it; the server thread crashes silently before binding. Fixed by passing interface="asgi3" to skip the autodetect — Starlette is asgi3, the detection was always going to land there. Test plan ./scripts/test (3.13) — 100% coverage, strict-no-cover clean ✓ uv run --python 3.14 pytest tests/shared/test_ws.py — 0.46s ✓ pytest tests/shared/test_ws.py -n 4 --flake-finder --flake-runs=5 — 20 parallel runs, 0 flakes ✓ AI Disclaimer
Open Graph Description: Problem tests/shared/test_ws.py had 4 tests sharing a subprocess+uvicorn fixture with a TOCTOU port race: bind port 0 → release → subprocess rebinds. Under pytest-xdist another worker claims the po...
X Description: Problem tests/shared/test_ws.py had 4 tests sharing a subprocess+uvicorn fixture with a TOCTOU port race: bind port 0 → release → subprocess rebinds. Under pytest-xdist another worker claims the po...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/2267
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:3df7e2f3-48ae-7165-3387-f08d147d54c3 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | BDBA:336741:1D41B28:271F1E3:6A5BB176 |
| html-safe-nonce | 4537b228bfc68b34e2f7ea605bfe2387f5b1f95b6f871aa6f7c6ba489ef7396c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCREJBOjMzNjc0MToxRDQxQjI4OjI3MUYxRTM6NkE1QkIxNzYiLCJ2aXNpdG9yX2lkIjoiNTcyOTk1NzUwNjc5MDk2MTUyNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 67d6a7f72ff000e633a533dea261d083ad62471125f494cd8a5cc0ffcd7afdba |
| hovercard-subject-tag | pull_request:3379075559 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/modelcontextprotocol/python-sdk/pull/2267/files |
| twitter:image | https://avatars.githubusercontent.com/u/224885523?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/224885523?s=400&v=4 |
| og:image:alt | Problem tests/shared/test_ws.py had 4 tests sharing a subprocess+uvicorn fixture with a TOCTOU port race: bind port 0 → release → subprocess rebinds. Under pytest-xdist another worker claims the po... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width