René's URL Explorer Experiment


Title: fix: prevent session + task leak on GET/DELETE without session-id by raphaelOhana · Pull Request #3059 · modelcontextprotocol/python-sdk · GitHub

Open Graph Title: fix: prevent session + task leak on GET/DELETE without session-id by raphaelOhana · Pull Request #3059 · modelcontextprotocol/python-sdk

X Title: fix: prevent session + task leak on GET/DELETE without session-id by raphaelOhana · Pull Request #3059 · modelcontextprotocol/python-sdk

Description: Summary In stateful mode, StreamableHTTPSessionManager currently allocates a StreamableHTTPServerTransport and spawns a background task for every request without an MCP-Session-Id header — including GET and DELETE, which the MCP spec does not allow to initialize a session. The transport rejects the request downstream (400/406), but the allocated session and task are never cleaned up. Only session_idle_timeout (opt-in, off by default) reaps them later. This PR moves the "must have a session-id" check up into the manager for GET and DELETE, so those requests are rejected with 400 Bad Request: Missing session ID before any session is allocated. Repro (before the patch) Fire 100 GET /mcp requests with an SDK-only in-process server (no FastMCP, no external transport), then inspect _server_instances: [SNAPSHOT baseline ] sessions= 0 tasks= 0 [SNAPSHOT after 100x GET /mcp/ (no header) ] sessions= 100 tasks= 100 [SNAPSHOT after 100x GET /mcp/ (Accept hdr) ] sessions= 200 tasks= 200 [SNAPSHOT after 100x DELETE /mcp/ ] sessions= 300 tasks= 300 Every one of those 300 sessions is still in _server_instances, and every one has a corresponding suspended anyio task in the task group. In production this shows up as Created new transport with session ID: … in the server logs with no matching Session terminated / closed / ended / deleted events. We caught this via a Docker healthcheck polling GET /mcp every 30s on a taylorwilsdon/google_workspace_mcp container (which uses this SDK via FastMCP) running for 10 days: 28 800 sessions accumulated, RAM climbed from ~100 MiB to ~1.3 GiB (~46 KiB / leaked session), 0 restarts, container reported healthy the whole time. Repro after the patch Same script, same requests: [SNAPSHOT after 100x GET /mcp/ (no header) ] sessions= 0 tasks= 0 [SNAPSHOT after 100x GET /mcp/ (Accept hdr) ] sessions= 0 tasks= 0 [SNAPSHOT after 100x DELETE /mcp/ ] sessions= 0 tasks= 0 Requests are rejected at the manager layer with a well-formed JSON-RPC error: { "jsonrpc": "2.0", "id": "server-error", "error": { "code": -32600, "message": "Bad Request: Missing session ID" } } Why the manager and not the transport The transport-level rejection happens after the manager has already allocated a session and spawned the background task. That task waits on serve_loop / app.run(read_stream, write_stream, …) which never returns because read_stream never receives anything, so the finally cleanup at the bottom of the task never fires. Moving the check up-stack means we never enter the code path that leaks. What the change touches src/mcp/server/streamable_http_manager.py: pre-instantiates a TransportSecurityMiddleware on the manager so the DNS-rebinding / bad-Host check runs before session allocation (previously that check lived only in the transport, so a bad-Host request would first allocate a session, then get rejected — same leak), rejects GET and DELETE without a session-id with 400 "Missing session ID" (same message the transport uses for the equivalent POST case at streamable_http.py:844, kept identical to avoid diverging error surfaces), leaves PUT/PATCH/OPTIONS alone — they continue to reach the transport's _handle_unsupported_request and get the existing 405 Method Not Allowed response. tests/server/test_streamable_http_manager.py: adds a parametrized regression test test_non_post_without_session_id_does_not_allocate_session[GET, DELETE] that asserts _server_instances and the task group both stay empty after a bad request, and that the response is the JSON-RPC 400 shape. Full suite passes locally on v1.x: tests/server/test_streamable_http_manager.py — all pass (existing + 2 new). tests/server/test_streamable_http_security.py — passes (test_streamable_http_security_get_request still returns 421 for bad Host, verifying the reorder didn't break DNS-rebinding). tests/shared/test_streamable_http.py — passes (test_method_not_allowed still returns 405 for PUT, verifying the fix only intercepts GET/DELETE). tests/issues/test_1363_race_condition_streamable_http.py — passes. Total: 82 passed, 0 failed on v1.x. Behavioural notes for downstream users POST /mcp without a session-id: unchanged — still initializes a new session. GET /mcp without a session-id: was 406 Not Acceptable (or 400 if the client sent the right Accept header); now consistently 400 "Bad Request: Missing session ID". DELETE /mcp without a session-id: was 400 "Missing session ID" from the transport (with a leaked session); now 400 "Missing session ID" from the manager (no leak). PUT/PATCH/OPTIONS without a session-id: unchanged — still 405 Method Not Allowed. The response body shape is the same JSON-RPC error the manager already uses for unknown-session cases, so clients that already parse that shape need no changes.

Open Graph Description: Summary In stateful mode, StreamableHTTPSessionManager currently allocates a StreamableHTTPServerTransport and spawns a background task for every request without an MCP-Session-Id header — includin...

X Description: Summary In stateful mode, StreamableHTTPSessionManager currently allocates a StreamableHTTPServerTransport and spawns a background task for every request without an MCP-Session-Id header — includin...

Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/3059

X: @github

direct link

Domain: github.com

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:077d1e11-904d-6ade-e2b6-25963cd2e249
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-id98B4:882E2:273429:354E9D:6B00C136
html-safe-nonce5997013caced26e7218680743db58ea65c4863b2e450e5c079f27681ead381c7
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OEI0Ojg4MkUyOjI3MzQyOTozNTRFOUQ6NkIwMEMxMzYiLCJ2aXNpdG9yX2lkIjoiODE5NjIyNTIzNjgwNzUwMDA4NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac2ce20a242c8bb45b67de7d4725b86918329f9ceeb92b36a1e93c83d85451c4a0
hovercard-subject-tagpull_request:3986424851
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/modelcontextprotocol/python-sdk/pull/3059/files
twitter:imagehttps://avatars.githubusercontent.com/u/57364944?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/57364944?s=400&v=4
og:image:altSummary In stateful mode, StreamableHTTPSessionManager currently allocates a StreamableHTTPServerTransport and spawns a background task for every request without an MCP-Session-Id header — includin...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None93edd3bebc4afb7ce711ee74cb5d2a95927850b082a4d43e212222389cb1c415
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git
octolytics-dimension-user_id182288589
octolytics-dimension-user_loginmodelcontextprotocol
octolytics-dimension-repository_id862584018
octolytics-dimension-repository_nwomodelcontextprotocol/python-sdk
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id862584018
octolytics-dimension-repository_network_root_nwomodelcontextprotocol/python-sdk
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release1fee81a280ea86312b69448e261b418189bfd5b5
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/modelcontextprotocol/python-sdk/pull/3059/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fpull%2F3059%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fpull%2F3059%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=modelcontextprotocol%2Fpython-sdk
Reloadhttps://github.com/modelcontextprotocol/python-sdk/pull/3059/files
Reloadhttps://github.com/modelcontextprotocol/python-sdk/pull/3059/files
Reloadhttps://github.com/modelcontextprotocol/python-sdk/pull/3059/files
modelcontextprotocolhttps://github.com/modelcontextprotocol
python-sdkhttps://github.com/modelcontextprotocol/python-sdk
Codehttps://github.com/modelcontextprotocol/python-sdk
Issues308 (308)https://github.com/modelcontextprotocol/python-sdk/issues
Pull requests112 (112)https://github.com/modelcontextprotocol/python-sdk/pulls
Actionshttps://github.com/modelcontextprotocol/python-sdk/actions
Projectshttps://github.com/modelcontextprotocol/python-sdk/projects
Security and quality12 (12)https://github.com/modelcontextprotocol/python-sdk/security
Insightshttps://github.com/modelcontextprotocol/python-sdk/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2Fnew%2Fchoose
raphaelOhanahttps://github.com/raphaelOhana
modelcontextprotocol:v1.xhttps://github.com/modelcontextprotocol/python-sdk/tree/v1.x
raphaelOhana:fix/streamable-http-session-leakhttps://github.com/raphaelOhana/python-sdk/tree/fix/streamable-http-session-leak
Conversation 7 https://github.com/modelcontextprotocol/python-sdk/pull/3059
Commits 5 https://github.com/modelcontextprotocol/python-sdk/pull/3059/commits
Checks 27 https://github.com/modelcontextprotocol/python-sdk/pull/3059/checks
Files changed https://github.com/modelcontextprotocol/python-sdk/pull/3059/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/3059/files
fix: prevent session + task leak on GET/DELETE without session-id https://github.com/modelcontextprotocol/python-sdk/pull/3059/files#top
Show all changes 5 commits https://github.com/modelcontextprotocol/python-sdk/pull/3059/files
0f12dae fix(streamable-http): reject GET/DELETE without session-id before all… raphaelOhana Jul 3, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/3059/commits/0f12dae419b0a1623643968ad2e61a75c1d3cb2e
dbadb12 test: cover the pre-session security check + fix pyright annotation raphaelOhana Jul 3, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/3059/commits/dbadb129995269e11487377fded8ea95a2f98c4c
cf87495 fix(streamable-http): also reject PUT/PATCH/OPTIONS/HEAD without sess… raphaelOhana Jul 3, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/3059/commits/cf874959c572a1f7db351770d205777e97377201
be9c118 fix(streamable-http): mirror the transport's 405 shape (Allow header … raphaelOhana Jul 3, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/3059/commits/be9c118888d4a15bbcfe09afd5f61d619082cde2
f47b003 test: parametrize the Allow header check to keep 100% branch coverage raphaelOhana Jul 3, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/3059/commits/f47b003efdb93c643308a5c5b6d00a77d930f3e7
Clear filters https://github.com/modelcontextprotocol/python-sdk/pull/3059/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/3059/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/3059/files
streamable_http_manager.py https://github.com/modelcontextprotocol/python-sdk/pull/3059/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
test_streamable_http_manager.py https://github.com/modelcontextprotocol/python-sdk/pull/3059/files#diff-ee0bac8ccf39135f1347002539cc4af38ee7e89b85595884e94fb4ab304a111d
src/mcp/server/streamable_http_manager.pyhttps://github.com/modelcontextprotocol/python-sdk/pull/3059/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
View file https://github.com/raphaelOhana/python-sdk/blob/f47b003efdb93c643308a5c5b6d00a77d930f3e7/src/mcp/server/streamable_http_manager.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/modelcontextprotocol/python-sdk/pull/3059/{{ revealButtonHref }}
https://github.com/modelcontextprotocol/python-sdk/pull/3059/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
https://github.com/modelcontextprotocol/python-sdk/pull/3059/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
https://github.com/modelcontextprotocol/python-sdk/pull/3059/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
https://github.com/modelcontextprotocol/python-sdk/pull/3059/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
https://github.com/modelcontextprotocol/python-sdk/pull/3059/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
https://github.com/modelcontextprotocol/python-sdk/pull/3059/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
https://github.com/modelcontextprotocol/python-sdk/pull/3059/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
https://github.com/modelcontextprotocol/python-sdk/pull/3059/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
tests/server/test_streamable_http_manager.pyhttps://github.com/modelcontextprotocol/python-sdk/pull/3059/files#diff-ee0bac8ccf39135f1347002539cc4af38ee7e89b85595884e94fb4ab304a111d
View file https://github.com/raphaelOhana/python-sdk/blob/f47b003efdb93c643308a5c5b6d00a77d930f3e7/tests/server/test_streamable_http_manager.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/modelcontextprotocol/python-sdk/pull/3059/{{ revealButtonHref }}
https://github.com/modelcontextprotocol/python-sdk/pull/3059/files#diff-ee0bac8ccf39135f1347002539cc4af38ee7e89b85595884e94fb4ab304a111d
https://github.com/modelcontextprotocol/python-sdk/pull/3059/files#diff-ee0bac8ccf39135f1347002539cc4af38ee7e89b85595884e94fb4ab304a111d
https://github.com/modelcontextprotocol/python-sdk/pull/3059/files#diff-ee0bac8ccf39135f1347002539cc4af38ee7e89b85595884e94fb4ab304a111d
https://github.com/modelcontextprotocol/python-sdk/pull/3059/files#diff-ee0bac8ccf39135f1347002539cc4af38ee7e89b85595884e94fb4ab304a111d
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/3059/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.