Title: Potential memory leak in StreamableHTTPSessionManager due to idle sessions not being cleaned up · Issue #1283 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Potential memory leak in StreamableHTTPSessionManager due to idle sessions not being cleaned up · Issue #1283 · modelcontextprotocol/python-sdk
X Title: Potential memory leak in StreamableHTTPSessionManager due to idle sessions not being cleaned up · Issue #1283 · 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 There appears to be a ...
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/1283
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Potential memory leak in StreamableHTTPSessionManager due to idle sessions not being cleaned up","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\nThere appears to be a memory leak in the session management code. The `_server_instances` dictionary grows as new sessions are created, but entries are only removed if a session crashes. When a session is terminated normally, its transport instance remains in the dictionary and is never cleaned up. Over time, this can lead to unbounded memory usage.\n\n**Expected behavior:**\nSession entries in `_server_instances` should eventually be removed if they are no longer active or being reused, to avoid memory leaks.\n\n**Steps to reproduce:**\n1. Start the server and create multiple sessions.\n2. Allow sessions to become idle (no further requests sent).\n3. Observe that `_server_instances` retains references to all idle sessions indefinitely, even after reasonable inactivity.\n\n**Clarification:**\nIt is not correct to simply remove a session from the dictionary when an HTTP request terminates normally, because the same session may be reused by subsequent requests. The issue is that if no further requests ever come for that session, the idle session remains in memory.\n\n**Suggested fix:**\nImplement a session timeout or idle cleanup mechanism so that sessions which haven't received any activity for a configurable period are automatically cleaned up and removed from `_server_instances`.\n\n**Additional notes:**\nThis is not related to any specific Python version. The problem is seen on the current master branch. I am aware that session timeout/cleanup logic is needed to prevent accumulation of idle sessions and associated transports.\n\nSee the relevant code in [`streamable_http_manager.py`](https://github.com/modelcontextprotocol/python-sdk/blob/d1ac8d68eb2d7ed139bdc2608b8b4e2ec4265be5/src/mcp/server/streamable_http_manager.py) — this is currently the only place where a HTTP transport is ever removed from the `_server_instances` dict (specifically, when a session crashes).\n\n### Example Code\n\n```Python\nNo code sample provided, but the issue can be traced to the session lifecycle management in `StreamableHTTPSessionManager` (see `_handle_stateful_request`).\n\nSee code: https://github.com/modelcontextprotocol/python-sdk/blob/d1ac8d68eb2d7ed139bdc2608b8b4e2ec4265be5/src/mcp/server/streamable_http_manager.py\n```\n\n### Python \u0026 MCP Python SDK\n\n```Text\nThis issue is not related to a specific Python version. The problem is observed on the current master branch of the repository.\n```","author":{"url":"https://github.com/raptium","@type":"Person","name":"raptium"},"datePublished":"2025-08-19T04:39:05.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/1283/python-sdk/issues/1283"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:243440cf-ec1c-650a-54ab-5aae3d423d84 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 86FC:1A7259:18614A2:2096CF4:6A5B9F7D |
| html-safe-nonce | 03a4d8a4428a214ae1fcf4435bbabb5a3236b933011f67c28c9aef548ce0cfe6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NkZDOjFBNzI1OToxODYxNEEyOjIwOTZDRjQ6NkE1QjlGN0QiLCJ2aXNpdG9yX2lkIjoiODI2NTUxODU3MjcwMzAzMTE2NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a2d44373074165e055a9643c88a92abc4720c66ac9ca9844935d5117692e5f81 |
| hovercard-subject-tag | issue:3332857362 |
| github-keyboard-shortcuts | repository,issues,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/_view_fragments/issues/show/modelcontextprotocol/python-sdk/1283/issue_layout |
| twitter:image | https://opengraph.githubassets.com/22e15776bc54dcc2d0249b2a93507f2f71315178b19ff77b1560ae16e9061cec/modelcontextprotocol/python-sdk/issues/1283 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/22e15776bc54dcc2d0249b2a93507f2f71315178b19ff77b1560ae16e9061cec/modelcontextprotocol/python-sdk/issues/1283 |
| og:image:alt | 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 ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | raptium |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| turbo-cache-control | no-preview |
| 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 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width