René's URL Explorer Experiment


Title: Possible ressource leak / race condition in streamable_http_client · Issue #1805 · modelcontextprotocol/python-sdk · GitHub

Open Graph Title: Possible ressource leak / race condition in streamable_http_client · Issue #1805 · modelcontextprotocol/python-sdk

X Title: Possible ressource leak / race condition in streamable_http_client · Issue #1805 · modelcontextprotocol/python-sdk

Description: Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue Description Observed Issue When us...

Open Graph Description: Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this ...

X Description: Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening t...

Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/1805

X: @github

direct link

Domain: patch-diff.githubusercontent.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Possible ressource leak / race condition in streamable_http_client","articleBody":"### Initial Checks\n\n- [x] I confirm that I'm using the latest version of MCP Python SDK\n- [x] I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue\n\n### Description\n\n## Observed Issue\nWhen using the MCP SDK with the `streamable-http` transport, the cpu usage spikes and never goes back down after sending multiple requests and exiting the client context. I observed this bahavior especially in the google adk-python when the mcp-toolset tries to close the client.\n\nWhen running the testcode to reproduce i get:\n\n### Client-Side Testcode shows Thread Leaks\n```\nIteration 1: 1 thread  → 2 threads\nIteration 2: 2 threads → 7 threads\nIteration 3: 7 threads → 7 threads\nFinal: 7 threads (6 leaked asyncio_* threads)\n```\n\n### Server-Side: Exceptions\n```\nSession crashed: unhandled errors in a TaskGroup (1 sub-exception)\n\nClosedResourceError at session.py:349\n  → _write_stream.send() fails - stream already closed\n\nBrokenResourceError at streamable_http.py:638\n  → SSE writer has no receiver\n```\n\n## Possible Cause\n\n1. Client sends multiple requests via `write_stream.send()`\n2. Client exits `async with streamable_http_client` context\n3. `tg.cancel_scope.cancel()` is called during cleanup\n4. Server is still processing requests (e.g., `slow_echo` with delay)\n5. Server tries to send responses via `_write_stream.send()`\n6. Stream is already closed → `ClosedResourceError`\n7. Background asyncio threads handling responses become orphaned\n8. Threads never terminate → memory/resource leak\n\n\n## Expected Behavior\n- No thread leaks after exiting `streamable_http_client` context\n- Graceful handling of client disconnection on server side\n\n## Impact\n- Memory leak from accumulated threads\n- Resource exhaustion in long-running applications\n\n## Affected Code Paths\n- `mcp/client/streamable_http.py` - `streamable_http_client()` cleanup\n- `mcp/shared/session.py:237-238` - `cancel_scope.cancel()` and `__aexit__`\n- `mcp/server/streamable_http.py:638` - SSE response handling\n\n\n\n### Example Code\n\n```Python\n\"\"\"\nThis script demonstrates a possible bug in the MCP SDK where cleanup of\nstreamable_http_client causes BrokenResourceError and leaks threads.\n\nBug?: When tg.cancel_scope.cancel() is called during cleanup, child tasks\nspawned by post_writer (like _handle_json_response) are still trying to\nuse read_stream_writer. The stream gets closed before they finish,\ncausing BrokenResourceError and preventing proper httpx client cleanup.\n\"\"\"\n\nimport asyncio\nimport threading\nimport httpx\nfrom mcp.client.streamable_http import streamable_http_client\nfrom mcp.shared.message import SessionMessage\nfrom mcp.types import JSONRPCRequest, JSONRPCMessage\n\n\nasync def make_mcp_request(url: str, trigger_race: bool = False) -\u003e None:\n    \"\"\"Make an MCP request and observe cleanup behavior.\"\"\"\n\n    print(f\"[Before] Active threads: {threading.active_count()}\")\n    print(f\"[Before] Thread names: {[t.name for t in threading.enumerate()]}\")\n\n    # Create custom httpx client with short timeouts\n    http_client = httpx.AsyncClient(\n        headers={\"Authorization\": \"test\"},\n        timeout=httpx.Timeout(5.0, read=10.0),\n    )\n\n    try:\n        async with streamable_http_client(\n            url=url,\n            http_client=http_client,\n        ) as (read_stream, write_stream, get_session_id):\n            print(f\"[Connected] Session ID: {get_session_id()}\")\n            print(f\"[Connected] Active threads: {threading.active_count()}\")\n\n            # Send an initialize request\n            init_request = JSONRPCRequest(\n                jsonrpc=\"2.0\",\n                id=\"1\",\n                method=\"initialize\",\n                params={\n                    \"protocolVersion\": \"2024-11-05\",\n                    \"capabilities\": {},\n                    \"clientInfo\": {\"name\": \"test-client\", \"version\": \"1.0.0\"},\n                },\n            )\n            await write_stream.send(SessionMessage(JSONRPCMessage(init_request)))\n            print(\"[Sent] Initialize request\")\n\n            # Read the response\n            async for message in read_stream:\n                print(f\"[Received] {type(message).__name__}\")\n                if isinstance(message, Exception):\n                    print(f\"[Error in stream] {message}\")\n                break\n\n            if trigger_race:\n                # Send multiple requests quickly to create race during cleanup\n                for i in range(5):\n                    tool_request = JSONRPCRequest(\n                        jsonrpc=\"2.0\",\n                        id=f\"tool-{i}\",\n                        method=\"tools/call\",\n                        params={\n                            \"name\": \"slow_echo\",\n                            \"arguments\": {\"message\": f\"test-{i}\"},\n                        },\n                    )\n                    await write_stream.send(SessionMessage(JSONRPCMessage(tool_request)))\n                print(\"[Sent] 5 tool requests - exiting immediately to trigger race\")\n                # Exit immediately without reading responses - this should trigger the race\n\n    except Exception as e:\n        import traceback\n        print(f\"[Error] {type(e).__name__}: {e}\")\n        traceback.print_exc()\n\n    print(f\"[After cleanup] Active threads: {threading.active_count()}\")\n    print(f\"[After cleanup] Thread names: {[t.name for t in threading.enumerate()]}\")\n\n    # Wait and check if threads persist\n    await asyncio.sleep(5)\n    print(f\"[After 5s] Active threads: {threading.active_count()}\")\n    print(f\"[After 5s] Thread names: {[t.name for t in threading.enumerate()]}\")\n\n\nasync def main():\n    # Replace with your MCP server URL\n    MCP_URL = \"http://localhost:8000/mcp\"\n\n    print(\"=\" * 60)\n    print(\"MCP SDK Cleanup Race Condition Reproduction\")\n    print(\"=\" * 60)\n\n    # Run multiple iterations to accumulate leaked threads\n    for i in range(3):\n        print(f\"\\n--- Iteration {i + 1} ---\")\n        # Trigger race condition on iterations 2 and 3\n        await make_mcp_request(MCP_URL, trigger_race=(i \u003e 0))\n        await asyncio.sleep(2)\n\n    print(\"\\n\" + \"=\" * 60)\n    print(\"Final State\")\n    print(\"=\" * 60)\n    print(f\"Active threads: {threading.active_count()}\")\n    for t in threading.enumerate():\n        print(f\"  - {t.name} (daemon={t.daemon})\")\n\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n\n\n-----\n\n\"\"\"Minimal MCP server for race condition testing.\"\"\"\n\nfrom mcp.server.fastmcp import FastMCP\n\nmcp = FastMCP(\"Test Server\")\n\n\n@mcp.tool()\ndef echo(message: str) -\u003e str:\n    \"\"\"Echo the message back.\"\"\"\n    return f\"Echo: {message}\"\n\n\n@mcp.tool()\ndef slow_echo(message: str) -\u003e str:\n    \"\"\"Echo with a delay to simulate work.\"\"\"\n    import time\n\n    time.sleep(0.5)\n    return f\"Slow Echo: {message}\"\n\n\nif __name__ == \"__main__\":\n    mcp.run(transport=\"streamable-http\")\n```\n\n### Python \u0026 MCP Python SDK\n\n```Text\n- Python: 3.13\n- MCP SDK: 1.25.0\n- Transport: streamable-http\n```","author":{"url":"https://github.com/h-filzer","@type":"Person","name":"h-filzer"},"datePublished":"2025-12-19T13:46:48.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/1805/python-sdk/issues/1805"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:848b496a-8c3e-5bd0-0302-b0107178c017
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id84F6:360CDD:901913:C41529:696E7FAD
html-safe-nonce7051d6ff96d02d05e5af315d58979ea72a575b6a84a0fbb17d339e00f39b7cf2
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NEY2OjM2MENERDo5MDE5MTM6QzQxNTI5OjY5NkU3RkFEIiwidmlzaXRvcl9pZCI6IjYyNjY4OTA4OTY2ODcwOTU3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac320be09b8db8e9535959fd7a5342b4a8c520de85f0978d4b4439a190a5098565
hovercard-subject-tagissue:3747596266
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/1805/issue_layout
twitter:imagehttps://opengraph.githubassets.com/2d29be121db1c5e4d43d7956c9248295318abfee98c9bdd1e1982a3650d886f5/modelcontextprotocol/python-sdk/issues/1805
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/2d29be121db1c5e4d43d7956c9248295318abfee98c9bdd1e1982a3650d886f5/modelcontextprotocol/python-sdk/issues/1805
og:image:altInitial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameh-filzer
hostnamegithub.com
expected-hostnamegithub.com
Nonefdad15fd2ad43212aa8b8be5f2c2725550f8374ceeeb154a999ad9145b43f3f7
turbo-cache-controlno-preview
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
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release27b23bc056eb973d350fc95afc848757edb9e7a9
ui-targetcanary-2
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk/issues/1805#start-of-content
https://patch-diff.githubusercontent.com/
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2F1805
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
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
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://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/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2F1805
Sign up https://patch-diff.githubusercontent.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=modelcontextprotocol%2Fpython-sdk
Reloadhttps://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk/issues/1805
Reloadhttps://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk/issues/1805
Reloadhttps://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk/issues/1805
modelcontextprotocol https://patch-diff.githubusercontent.com/modelcontextprotocol
python-sdkhttps://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk
Notifications https://patch-diff.githubusercontent.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk
Fork 3k https://patch-diff.githubusercontent.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk
Star 21.2k https://patch-diff.githubusercontent.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk
Code https://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk
Issues 238 https://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk/issues
Pull requests 90 https://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk/pulls
Actions https://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk/actions
Projects 0 https://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk/projects
Security Uh oh! There was an error while loading. Please reload this page. https://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk/security
Please reload this pagehttps://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk/issues/1805
Insights https://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk/pulse
Code https://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk
Issues https://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk/issues
Pull requests https://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk/pulls
Actions https://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk/actions
Projects https://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk/projects
Security https://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk/security
Insights https://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk/pulse
New issuehttps://patch-diff.githubusercontent.com/login?return_to=https://github.com/modelcontextprotocol/python-sdk/issues/1805
New issuehttps://patch-diff.githubusercontent.com/login?return_to=https://github.com/modelcontextprotocol/python-sdk/issues/1805
Possible ressource leak / race condition in streamable_http_clienthttps://patch-diff.githubusercontent.com/modelcontextprotocol/python-sdk/issues/1805#top
bugSomething isn't workinghttps://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22bug%22
needs confirmationNeeds confirmation that the PR is actually required or needed.https://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22needs%20confirmation%22
https://github.com/h-filzer
https://github.com/h-filzer
h-filzerhttps://github.com/h-filzer
on Dec 19, 2025https://github.com/modelcontextprotocol/python-sdk/issues/1805#issue-3747596266
https://github.com/modelcontextprotocol/python-sdk/issueshttps://github.com/modelcontextprotocol/python-sdk/issues
bugSomething isn't workinghttps://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22bug%22
needs confirmationNeeds confirmation that the PR is actually required or needed.https://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22needs%20confirmation%22
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.