Title: Run transport security tests in process instead of over sockets by maxisbey · Pull Request #2764 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Run transport security tests in process instead of over sockets by maxisbey · Pull Request #2764 · modelcontextprotocol/python-sdk
X Title: Run transport security tests in process instead of over sockets by maxisbey · Pull Request #2764 · modelcontextprotocol/python-sdk
Description: The SSE and StreamableHTTP security test files have been flaking in CI. Two recent examples: test_sse_security_custom_allowed_hosts failing with 421 for an explicitly allowed host, and test_idle_session_is_reaped failing with 406 instead of 404 on Windows. Motivation and Context The security tests had a port-allocation race. Each test picked an ephemeral port by bind-then-close, spawned a uvicorn subprocess to re-bind it, and polled until the port accepted connections. Under pytest-xdist, two workers can pick the same port in that window: the second server fails to bind (silently — log_level="error" in a subprocess), the readiness poll succeeds against the other worker's server, and the test asserts against a server configured with different security settings. That's how a test that allows custom.host observes a 421. The fix removes the network entirely: both files now drive the same Starlette apps in process through the interaction suite's StreamingASGITransport (re-exported from tests.interaction.transports as the sanctioned import point for code outside that suite). No sockets, no subprocesses, no ports to race over. Assertions are unchanged — the diff is a harness swap. Each file also drops from seconds of subprocess churn to ~0.25s. The idle-reap test had a timing race. It slept 0.1s after a 0.05s idle timeout and asserted the session was gone — on a slow runner the reaper hasn't fired yet, the request routes into the still-live transport, and the missing Accept header yields the 406. Polling for the 404 instead would never converge: each request to a live session pushes its idle deadline forward. The test now waits (bounded by anyio.fail_after(5)) on the manager's own "idle timeout" log record, which is emitted synchronously with the session being unregistered — once observed, the 404 is guaranteed. One src change: the new in-process GET test covers the validation-failure return in _handle_get_request, so the now-stale # pragma: no cover on that line is removed (the success path was already driven in process by the interaction suite). Two subtleties worth knowing as a reviewer: The SSE rejection path sends a second http.response.start through the bridge (connect_sse sends the 421/403 itself, then the handler's trailing Response() is sent by Starlette). This is deterministic because anyio's MemoryObjectSendStream.send() checkpoints on the non-empty error body before the overwrite can happen (holds down to the anyio>=4.9 floor); there's a comment at the spot documenting the assumption. The SSE client uses cancel_on_close=False so the bridge drains the app's disconnect handling instead of cancelling it, matching how the interaction suite's own SSE leg uses the bridge. tests/shared/test_sse.py, tests/shared/test_streamable_http.py, and tests/client/test_http_unicode.py still use the subprocess pattern and are follow-up candidates. How Has This Been Tested? ./scripts/test green: 1526 passed, 100% line+branch coverage, strict-no-cover clean The three changed files stress-tested heavily: thousands of executions across repeated sequential runs, pytest-xdist at several -n values, shuffled test orders, and under sustained CPU load — zero failures The deflaked idle-reap test passed 50/50 consecutive solo runs Breaking Changes None — test-only, plus one pragma removal in src. Types of changes Bug fix (non-breaking change which fixes an issue) Checklist I have read the MCP Documentation My code follows the repository's style guidelines New and existing tests pass locally I have added appropriate error handling I have added or updated documentation as needed Additional context Test names keep their existing feature-label style (rather than the interaction suite's behaviour-sentence names) so the "assertions unchanged" claim stays auditable in the diff; happy to rename in a follow-up if preferred. AI Disclaimer
Open Graph Description: The SSE and StreamableHTTP security test files have been flaking in CI. Two recent examples: test_sse_security_custom_allowed_hosts failing with 421 for an explicitly allowed host, and test_idle_se...
X Description: The SSE and StreamableHTTP security test files have been flaking in CI. Two recent examples: test_sse_security_custom_allowed_hosts failing with 421 for an explicitly allowed host, and test_idle_se...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/2764
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:51a1c6b1-5d8e-6fe7-26ca-3aab1745183d |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 9D0E:3C3370:3B01FE:4C2BCB:6A5B237F |
| html-safe-nonce | 35443a66fcb73fa5194397dab65a6510045c72609a388184ba3f742788deb01f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RDBFOjNDMzM3MDozQjAxRkU6NEMyQkNCOjZBNUIyMzdGIiwidmlzaXRvcl9pZCI6Ijc1MDc1MjE5ODI0MzMwMTA1NTkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 0c078d8965442f49ff65325ac311de1d4cf3761570bc20073e1b82dd43da7c46 |
| hovercard-subject-tag | pull_request:3790417850 |
| 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/2764/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 | The SSE and StreamableHTTP security test files have been flaking in CI. Two recent examples: test_sse_security_custom_allowed_hosts failing with 421 for an explicitly allowed host, and test_idle_se... |
| 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