René's URL Explorer Experiment


Title: `ClientSession` never sends `notifications/cancelled` when `call_tool` is cancelled — server-side coroutines leak · Issue #2507 · modelcontextprotocol/python-sdk · GitHub

Open Graph Title: `ClientSession` never sends `notifications/cancelled` when `call_tool` is cancelled — server-side coroutines leak · Issue #2507 · modelcontextprotocol/python-sdk

X Title: `ClientSession` never sends `notifications/cancelled` when `call_tool` is cancelled — server-side coroutines leak · Issue #2507 · modelcontextprotocol/python-sdk

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

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

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

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`ClientSession` never sends `notifications/cancelled` when `call_tool` is cancelled — server-side coroutines leak","articleBody":"### Initial Checks\n\n- [x] I confirm that I'm using the latest version of MCP Python SDK (1.29.0 verified)\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`ClientSession.send_request()` (and therefore every `call_tool`, `list_tools`, etc.) never emits a `notifications/cancelled` message when its in-flight `await` is interrupted, regardless of whether the interruption comes from the SDK's own timeout or from the caller's `asyncio.wait_for`. The MCP spec (cancellation.mdx) requires the sender to issue this notification on timeout, and any cooperative cancellation likewise leaves the server with an orphan request.\n\nEmpirical impact: server-side tool coroutines remain suspended after a client cancellation. They hold whatever resources they had acquired (DB connections, cursors, locks, file handles) until the session itself ends. With long-lived sessions and cancellable workloads, every cancelled call is a silent leak.\n\nThis was previously raised in #1458 (\"Missing Cancellation Notifications on Request Timeout\") and closed as `DUPLICATE`, but the proposed fix never landed and the underlying behavior is still present in `1.29.0`. The current report adds a second uncovered path (external `CancelledError`) and concrete evidence of the resource-leak impact, so I'm filing rather than commenting on the closed issue.\n\n### Two paths, neither sends the notification\n\n**Path A — SDK-internal timeout (`anyio.fail_after`):** Already documented in #1458. The `except TimeoutError` branch raises `McpError` and falls into the `finally` block that closes the local response stream. No `CancelledNotification` is sent.\n\n`mcp/shared/session.py:290-303` (1.29.0):\n\n```python\ntry:\n    with anyio.fail_after(timeout):\n        response_or_error = await response_stream_reader.receive()\nexcept TimeoutError:\n    raise McpError(\n        ErrorData(\n            code=httpx.codes.REQUEST_TIMEOUT,\n            message=(\n                f\"Timed out while waiting for response to \"\n                f\"{request.__class__.__name__}. Waited \"\n                f\"{timeout} seconds.\"\n            ),\n        )\n    )\n```\n\n**Path B — external cancellation:** When the caller wraps `session.call_tool(...)` in `asyncio.wait_for(...)` (or any other cancellation source), `asyncio.CancelledError` is raised at the `await response_stream_reader.receive()` point. There is no `except` for `CancelledError` / `anyio.get_cancelled_exc_class()` anywhere in `send_request`. The exception flows up through the `finally`, which only cleans up the *client-local* response stream:\n\n`mcp/shared/session.py:310-313`:\n\n```python\nfinally:\n    self._response_streams.pop(request_id, None)\n    self._progress_callbacks.pop(request_id, None)\n    await response_stream.aclose()\n    await response_stream_reader.aclose()\n```\n\nThe server is never told the request is gone. Its in-flight tool task continues to completion, then sends back a response that gets dropped because no one is reading the response stream.\n\n### Reproduction\n\nMinimal repro script (full version: https://github.com/sherman94062/databricks-ai-steward/blob/main/stress/probe_a1_leak.py):\n\n```python\nimport asyncio\nfrom mcp import ClientSession, StdioServerParameters\nfrom mcp.client.stdio import stdio_client\n\n# stress.server is a tiny FastMCP server with two tools:\n#   hangs_forever_async_guarded — `await asyncio.sleep(300)`\n#   task_count                  — returns len(asyncio.all_tasks()) - 1\nasync def main():\n    params = StdioServerParameters(\n        command=\"python\", args=[\"-m\", \"stress.server\"]\n    )\n    async with stdio_client(params) as (read, write):\n        async with ClientSession(read, write) as session:\n            await session.initialize()\n\n            baseline = await session.call_tool(\"task_count\", {})\n            print(\"baseline:\", baseline)\n\n            for _ in range(50):\n                try:\n                    await asyncio.wait_for(\n                        session.call_tool(\"hangs_forever_async_guarded\", {}),\n                        timeout=0.1,\n                    )\n                except asyncio.TimeoutError:\n                    pass\n\n            await asyncio.sleep(0.5)  # let any cleanup settle\n            after = await session.call_tool(\"task_count\", {})\n            print(\"after 50 cancels:\", after)\n\nasyncio.run(main())\n```\n\nOutput (mcp 1.29.0, Python 3.14):\n\n```\nbaseline:        4 tasks\nafter 50 cancels: 54 tasks\n```\n\n50 cancelled `call_tool` invocations → 50 leaked server-side coroutines, persistent until the session closes. Same numbers under stdio and `streamable_http`.\n\nIf the client sent `notifications/cancelled`, the server's existing handler at `mcp/shared/session.py:402-406` would cancel each leaked coroutine immediately:\n\n```python\nif isinstance(notification.root, CancelledNotification):\n    cancelled_id = notification.root.params.requestId\n    if cancelled_id in self._in_flight:\n        await self._in_flight[cancelled_id].cancel()\n```\n\nThe server side already does the right thing on receipt. Only the client-side emit is missing.\n\n### Spec citations\n\n\u003e Implementations **SHOULD** establish timeouts for all sent requests… When the request has not received a success or error response within the timeout period, the sender **SHOULD** issue a cancellation notification for that request and stop waiting for a response.\n\n\u003e Either side can cancel an in-progress request by sending a cancellation notification.\n\n— https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/docs/specification/draft/basic/utilities/cancellation.mdx\n\n### Proposed fix\n\nTwo small additions to `BaseSession.send_request`. The internal-timeout branch is exactly what #1458 proposed; the external-cancellation branch is new and uses `anyio.get_cancelled_exc_class()` so it works under both asyncio and trio backends:\n\n```python\ntry:\n    with anyio.fail_after(timeout):\n        response_or_error = await response_stream_reader.receive()\nexcept TimeoutError:\n    await self._send_cancelled_notification(request_id, \"request timed out\")\n    raise McpError(...)\nexcept anyio.get_cancelled_exc_class():\n    await self._send_cancelled_notification(request_id, \"request cancelled by caller\")\n    raise\n\n\nasync def _send_cancelled_notification(self, request_id, reason):\n    try:\n        await self.send_notification(\n            ClientNotification(\n                CancelledNotification(\n                    method=\"notifications/cancelled\",\n                    params=CancelledNotificationParams(\n                        requestId=request_id, reason=reason\n                    ),\n                )\n            )\n        )\n    except Exception:\n        # Best-effort: if the transport is already gone, nothing to do.\n        logger.warning(\n            \"failed to send cancellation notification for request %s\",\n            request_id,\n        )\n```\n\nThe notification *must* be sent before re-raising — once the cancellation propagates out of `send_request`, the caller may close the session and the write stream becomes unusable. A small async-shielded wrapper around the `send_notification` call may be needed to guarantee delivery on the cancellation path; happy to put that into a PR.\n\n### Why this matters in practice\n\nMost production MCP servers acquire external resources inside tool handlers — DB connections, HTTP clients, transactions, file locks. Without the cancellation notification, every aborted client call wastes one such resource for the lifetime of the session. We discovered this while building a Databricks-facing MCP server: a connection pool of 10 plus 10 cancelled tool calls = pool exhausted.\n\nA server-side per-tool timeout (`asyncio.wait_for` inside the tool wrapper) bounds the leak window, but it shouldn't be load-bearing. The client should tell the server when a request is dead.\n\n### Environment\n\n- `mcp` 1.29.0 (latest at time of writing)\n- Python 3.14.3, macOS 14\n- Same behavior reproduced with `streamable_http` transport (different transport, identical client cancellation path)\n\n### Related\n\n- #1458 — same bug, closed as DUPLICATE, fix not shipped\n- #1420 — closed; missing cancellation reason logging (server-side)\n- #1419, #2480 — closed/open; server-side spec gap (sending response after cancellation)\n- #2416 — open; server-side cancellation race\n","author":{"url":"https://github.com/sherman94062","@type":"Person","name":"sherman94062"},"datePublished":"2026-04-26T15:08:53.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/2507/python-sdk/issues/2507"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:b797481f-45b9-e47a-84f8-6534e9d5e9e0
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD76E:1F8A40:25F46F:3636E0:6A59D5E9
html-safe-nonce965801f17c2c1e0e85fe08dcf957a9aa0c37a4460f9496018b0c869edc8fb564
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENzZFOjFGOEE0MDoyNUY0NkY6MzYzNkUwOjZBNTlENUU5IiwidmlzaXRvcl9pZCI6IjQxMTkyOTczMTE0MDIyMTg5ODUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacd996974aaaacdc52921ea5eabe07f650a7f882c9a5b0be6441d1b65f55252f73
hovercard-subject-tagissue:4331191649
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/2507/issue_layout
twitter:imagehttps://opengraph.githubassets.com/8013fe12e8b16a1d6caa401b60b524668d26412d93ec2a87b04a80b9dbe32421/modelcontextprotocol/python-sdk/issues/2507
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/8013fe12e8b16a1d6caa401b60b524668d26412d93ec2a87b04a80b9dbe32421/modelcontextprotocol/python-sdk/issues/2507
og:image:altInitial Checks I confirm that I'm using the latest version of MCP Python SDK (1.29.0 verified) I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues be...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamesherman94062
hostnamegithub.com
expected-hostnamegithub.com
Noneba3976babb66479b1c943a8edc0777d96157da48fadc0161f9ddb219deee8353
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
releasebfacf98c3b7ad151665d6ddd216469389872b251
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/modelcontextprotocol/python-sdk/issues/2507#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2F2507
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
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
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
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/enterprise/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://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2F2507
Sign up https://github.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://github.com/modelcontextprotocol/python-sdk/issues/2507
Reloadhttps://github.com/modelcontextprotocol/python-sdk/issues/2507
Reloadhttps://github.com/modelcontextprotocol/python-sdk/issues/2507
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/issues/2507
modelcontextprotocol https://github.com/modelcontextprotocol
python-sdkhttps://github.com/modelcontextprotocol/python-sdk
Notifications https://github.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk
Fork 3.7k https://github.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk
Star 23.6k https://github.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk
Code https://github.com/modelcontextprotocol/python-sdk
Issues 255 https://github.com/modelcontextprotocol/python-sdk/issues
Pull requests 303 https://github.com/modelcontextprotocol/python-sdk/pulls
Actions https://github.com/modelcontextprotocol/python-sdk/actions
Projects https://github.com/modelcontextprotocol/python-sdk/projects
Models https://github.com/modelcontextprotocol/python-sdk/models
Security and quality 6 https://github.com/modelcontextprotocol/python-sdk/security
Insights https://github.com/modelcontextprotocol/python-sdk/pulse
Code https://github.com/modelcontextprotocol/python-sdk
Issues https://github.com/modelcontextprotocol/python-sdk/issues
Pull requests https://github.com/modelcontextprotocol/python-sdk/pulls
Actions https://github.com/modelcontextprotocol/python-sdk/actions
Projects https://github.com/modelcontextprotocol/python-sdk/projects
Models https://github.com/modelcontextprotocol/python-sdk/models
Security and quality https://github.com/modelcontextprotocol/python-sdk/security
Insights https://github.com/modelcontextprotocol/python-sdk/pulse
#2838https://github.com/modelcontextprotocol/python-sdk/pull/2838
#2514https://github.com/modelcontextprotocol/python-sdk/pull/2514
ClientSession never sends notifications/cancelled when call_tool is cancelled — server-side coroutines leakhttps://github.com/modelcontextprotocol/python-sdk/issues/2507#top
#2838https://github.com/modelcontextprotocol/python-sdk/pull/2838
#2514https://github.com/modelcontextprotocol/python-sdk/pull/2514
https://github.com/sherman94062
sherman94062https://github.com/sherman94062
on Apr 26, 2026https://github.com/modelcontextprotocol/python-sdk/issues/2507#issue-4331191649
https://github.com/modelcontextprotocol/python-sdk/issueshttps://github.com/modelcontextprotocol/python-sdk/issues
#1458https://github.com/modelcontextprotocol/python-sdk/issues/1458
#1458https://github.com/modelcontextprotocol/python-sdk/issues/1458
https://github.com/sherman94062/databricks-ai-steward/blob/main/stress/probe_a1_leak.pyhttps://github.com/sherman94062/databricks-ai-steward/blob/main/stress/probe_a1_leak.py
https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/docs/specification/draft/basic/utilities/cancellation.mdxhttps://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/docs/specification/draft/basic/utilities/cancellation.mdx
#1458https://github.com/modelcontextprotocol/python-sdk/issues/1458
MCP Python SDK Implementation Gap-17: Missing Cancellation Notifications on Request Timeout #1458https://github.com/modelcontextprotocol/python-sdk/issues/1458
MCP Python SDK Protocol Compliance Gap-2: Missing Logging and Propagation of Cancellation Reasons #1420https://github.com/modelcontextprotocol/python-sdk/issues/1420
MCP Python SDK Protocol Compliance Gap-1:Response Sent After Receiving Cancellation Notifications #1419https://github.com/modelcontextprotocol/python-sdk/issues/1419
RequestResponder.cancel sends JSON-RPC response, violating cancellation spec #2480https://github.com/modelcontextprotocol/python-sdk/issues/2480
Bug: AssertionError: Request already responded to — cancellation race in v1.27.0 #2416https://github.com/modelcontextprotocol/python-sdk/issues/2416
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.