René's URL Explorer Experiment


Title: Fix stateless HTTP task accumulation causing memory leak by wiggzz · Pull Request #2145 · modelcontextprotocol/python-sdk · GitHub

Open Graph Title: Fix stateless HTTP task accumulation causing memory leak by wiggzz · Pull Request #2145 · modelcontextprotocol/python-sdk

X Title: Fix stateless HTTP task accumulation causing memory leak by wiggzz · Pull Request #2145 · modelcontextprotocol/python-sdk

Description: Summary Fix memory leak where run_stateless_server tasks accumulate indefinitely in the manager's global task group after client disconnects Use request-scoped task groups instead of the global _task_group for stateless requests, ensuring tasks are cancelled when requests complete Add graceful SSE drain on shutdown: terminate all active transports before cancelling the task group, so SSE streams close cleanly instead of being abruptly reset Add regression tests for both the leak and the graceful drain Motivation and Context Related to #1764 (the underlying task accumulation issue in stateless mode). Fixes #1272 (graceful shutdown / SSE drain). Root cause — memory leak When a client disconnects while a tool call is in progress, EventSourceResponse detects the disconnect and tears down the SSE stream pipeline. However, Server.run() — which is processing the tool call in a separate task spawned into the manager's global _task_group — continues running. After the tool handler completes, the response has nowhere to go (the request streams were cleaned up), and Server.run() blocks indefinitely on async for message in session.incoming_messages. These zombie tasks accumulate one per disconnected request. In production, we observed 449 leaked ServerSession objects, 66,086 orphaned coroutines, 4,463 blocked stream receive operations, and ~150 MB of leaked heap — all from this pattern. Root cause — broken SSE close on shutdown The memory leak fix moves stateless server tasks from the manager's global task group to request-scoped task groups. This fixes the leak, but it also means the manager's tg.cancel_scope.cancel() during shutdown no longer reaches stateless transports — nothing ever closes their streams, so SSE responses hang indefinitely. A reverse proxy like nginx would log "upstream prematurely closed connection while reading upstream". The graceful drain fix adds explicit terminate() calls in run()'s finally block (for both stateful and stateless transports) before cancelling the task group. This closes the in-memory streams, letting EventSourceResponse send a final more_body=False chunk — a clean HTTP close. Why the existing test didn't catch the leak The existing test (test_stateless_requests_memory_cleanup, added in PR #1140) mocks app.run to return immediately, so the spawned tasks complete naturally. It never exercises the real-world scenario where client disconnects leave app.run() blocked. The fixes Memory leak: Replace the global task group spawn with a request-scoped anyio.create_task_group(). Both the server task and the request handler run inside this scoped group. When the request handler finishes (or is cancelled due to client disconnect), the scope is cancelled, which automatically cancels the server task. No tasks leak into the manager's long-lived task group. Graceful drain: Track in-flight stateless transports in _stateless_transports. In run()'s finally block, call terminate() on all active transports (stateful via _server_instances, stateless via _stateless_transports) before tg.cancel_scope.cancel(). Testing test_stateless_requests_task_leak_on_client_disconnect: Real Server.run() with a real tool handler, real SSE streaming, simulated client disconnect — verifies zero leaked tasks after 3 disconnected requests test_graceful_shutdown_closes_sse_streams_cleanly: Uses an ASGI send() interceptor to verify that SSE responses receive more_body=False (the ASGI equivalent of a proper HTTP chunked transfer close) during manager shutdown. Without the fix, the test times out because nothing ever closes the stateless transport's streams. All 13 tests pass. Breaking changes None. This is an internal implementation change. The public API and behavior are unchanged. 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

Open Graph Description: Summary Fix memory leak where run_stateless_server tasks accumulate indefinitely in the manager's global task group after client disconnects Use request-scoped task groups instead of the globa...

X Description: Summary Fix memory leak where run_stateless_server tasks accumulate indefinitely in the manager's global task group after client disconnects Use request-scoped task groups instead of the g...

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

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:22d34917-1a41-36ac-f470-bd13d95aac08
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idA556:3D7871:181B90:213D55:6AFD0401
html-safe-noncec0d47e6636ee165363abae29f15803f40aa45c32f9bffd75c2ba961aa2c5e087
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNTU2OjNENzg3MToxODFCOTA6MjEzRDU1OjZBRkQwNDAxIiwidmlzaXRvcl9pZCI6IjgwNzE4MTM4NDI1NjE5OTE2ODEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac44adb28328e7cb093d674a4b43d16f39bc48055100786a1908d377d9b311442f
hovercard-subject-tagpull_request:3326408465
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/2145/files
twitter:imagehttps://avatars.githubusercontent.com/u/7453026?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/7453026?s=400&v=4
og:image:altSummary Fix memory leak where run_stateless_server tasks accumulate indefinitely in the manager's global task group after client disconnects Use request-scoped task groups instead of the globa...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None8fd5a24ae29aa78df114eaaf1f9a839e8c3b7c0d27549e4f54678bb7cf3166d4
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
releasedd0990b833f227378ab84f1d9519db1a04660550
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/modelcontextprotocol/python-sdk/pull/2145/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fpull%2F2145%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%2F2145%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/2145/files
Reloadhttps://github.com/modelcontextprotocol/python-sdk/pull/2145/files
Reloadhttps://github.com/modelcontextprotocol/python-sdk/pull/2145/files
modelcontextprotocolhttps://github.com/modelcontextprotocol
python-sdkhttps://github.com/modelcontextprotocol/python-sdk
Codehttps://github.com/modelcontextprotocol/python-sdk
Issues305 (305)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
wiggzzhttps://github.com/wiggzz
modelcontextprotocol:mainhttps://github.com/modelcontextprotocol/python-sdk/tree/main
wiggzz:fix/stateless-task-group-leakhttps://github.com/wiggzz/python-sdk/tree/fix/stateless-task-group-leak
Conversation 1 https://github.com/modelcontextprotocol/python-sdk/pull/2145
Commits 17 https://github.com/modelcontextprotocol/python-sdk/pull/2145/commits
Checks 25 https://github.com/modelcontextprotocol/python-sdk/pull/2145/checks
Files changed https://github.com/modelcontextprotocol/python-sdk/pull/2145/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/2145/files
Fix stateless HTTP task accumulation causing memory leak https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#top
Show all changes 17 commits https://github.com/modelcontextprotocol/python-sdk/pull/2145/files
6bd4531 Fix stateless HTTP task accumulation causing memory leak (#756) wiggzz Feb 25, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2145/commits/6bd4531c7b387279d1e6919d82c7bc0985693fa4
8bfcca2 Fix issue reference from #756 (closed) to #1764 (open) wiggzz Feb 25, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2145/commits/8bfcca2fbc5cf35601e4d06163d87f01ffcbef20
bc59032 Fix CI: coverage, ruff format, and remove unused handle_list_tools wiggzz Feb 25, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2145/commits/bc590325188df7f80450d87a4262be6c197a6cec
4116e02 Remove over-conservative pragma: no cover annotations wiggzz Feb 25, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2145/commits/4116e0294e2a74872819ab13d64e84b9a5b57849
1095767 Revert sleep(0) approach — coverage was racy wiggzz Feb 25, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2145/commits/10957676eaef87722193c2fab468692265e67eb9
f6909a1 Move pragma: no cover to logger.debug (the unreachable line) wiggzz Feb 25, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2145/commits/f6909a10b29ea50bb31c05792eef5f6e63defb94
2e0e00b Move terminate() after task group exit for deterministic cancellation wiggzz Feb 25, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2145/commits/2e0e00b1f926f7e21ece29dfa15be3760db46255
1af03f8 Use lax no cover for timing-dependent exception paths; fix ruff format wiggzz Feb 25, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2145/commits/1af03f876b090d189512b40a13be53be34d51d34
bf8cf43 Fix ruff format and Python 3.11 coverage false negatives in test wiggzz Feb 25, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2145/commits/bf8cf4326642207609b01d2c1f7fe0601cc15f79
24da35a Remove unused Tool import flagged by ruff wiggzz Feb 25, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2145/commits/24da35a945c84b52d7151a0d17c31d0e76c72e42
35f08c7 Fix pyright errors for private task group access in test wiggzz Feb 25, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2145/commits/35f08c78570d46a54e49480ca5cc3c00b5689778
a7907d0 Add assertion for session_manager not None (pyright) wiggzz Feb 26, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2145/commits/a7907d069343b98f9edbb51cc8f5fd21fd95a751
02547cb Run handle_request in caller context, not as child task wiggzz Feb 26, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2145/commits/02547cbc146aef4989925a3cb8a6b26bd22e87ce
3179143 Add graceful SSE drain on session manager shutdown wiggzz Mar 9, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2145/commits/3179143b9ef2bb243dcaf93b2e31e797f7c793e4
0d513a6 Fix CI: pyright type errors in test and strict-no-cover false positive wiggzz Mar 9, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2145/commits/0d513a699e297b4ab11118aa163293a96af5c558
bb1a218 Merge branch 'main' into fix/stateless-task-group-leak wiggzz Mar 23, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2145/commits/bb1a2184674ef7cfcfdd661bf24878af12c53d66
6237bc3 Merge branch 'main' into fix/stateless-task-group-leak wiggzz Apr 1, 2026 https://github.com/modelcontextprotocol/python-sdk/pull/2145/commits/6237bc3fcee581c456f1281a88173c591c10ae81
Clear filters https://github.com/modelcontextprotocol/python-sdk/pull/2145/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/2145/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/2145/files
streamable_http.py https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-0119e4c1bbf2fde7ba89b9c9f5bc472ccc2332f84e96e47cf9e45cebef801da3
streamable_http_manager.py https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
session.py https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-aaa1596dd4db91f5cd7f743c8afbe58596daf01a2ecec4a0024768f7409fb1dc
test_streamable_http_manager.py https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-ee0bac8ccf39135f1347002539cc4af38ee7e89b85595884e94fb4ab304a111d
src/mcp/server/streamable_http.pyhttps://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-0119e4c1bbf2fde7ba89b9c9f5bc472ccc2332f84e96e47cf9e45cebef801da3
View file https://github.com/modelcontextprotocol/python-sdk/blob/6237bc3fcee581c456f1281a88173c591c10ae81/src/mcp/server/streamable_http.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/modelcontextprotocol/python-sdk/pull/2145/{{ revealButtonHref }}
https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-0119e4c1bbf2fde7ba89b9c9f5bc472ccc2332f84e96e47cf9e45cebef801da3
https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-0119e4c1bbf2fde7ba89b9c9f5bc472ccc2332f84e96e47cf9e45cebef801da3
https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-0119e4c1bbf2fde7ba89b9c9f5bc472ccc2332f84e96e47cf9e45cebef801da3
https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-0119e4c1bbf2fde7ba89b9c9f5bc472ccc2332f84e96e47cf9e45cebef801da3
https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-0119e4c1bbf2fde7ba89b9c9f5bc472ccc2332f84e96e47cf9e45cebef801da3
https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-0119e4c1bbf2fde7ba89b9c9f5bc472ccc2332f84e96e47cf9e45cebef801da3
https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-0119e4c1bbf2fde7ba89b9c9f5bc472ccc2332f84e96e47cf9e45cebef801da3
https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-0119e4c1bbf2fde7ba89b9c9f5bc472ccc2332f84e96e47cf9e45cebef801da3
src/mcp/server/streamable_http_manager.pyhttps://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
View file https://github.com/modelcontextprotocol/python-sdk/blob/6237bc3fcee581c456f1281a88173c591c10ae81/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/2145/{{ revealButtonHref }}
https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-1071ee05567116cb77322c376307b9071c6212871068ccf41911fc337ed473af
src/mcp/shared/session.pyhttps://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-aaa1596dd4db91f5cd7f743c8afbe58596daf01a2ecec4a0024768f7409fb1dc
View file https://github.com/modelcontextprotocol/python-sdk/blob/6237bc3fcee581c456f1281a88173c591c10ae81/src/mcp/shared/session.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/modelcontextprotocol/python-sdk/pull/2145/{{ revealButtonHref }}
https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-aaa1596dd4db91f5cd7f743c8afbe58596daf01a2ecec4a0024768f7409fb1dc
https://github.com/modelcontextprotocol/python-sdk/pull/2145/files#diff-aaa1596dd4db91f5cd7f743c8afbe58596daf01a2ecec4a0024768f7409fb1dc
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/2145/files
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/pull/2145/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.