fix(fastmcp): add session_idle_timeout parameter to prevent unbounded session memory growth#3443
Open
abhijeethp wants to merge 1 commit intoPrefectHQ:mainfrom
Open
fix(fastmcp): add session_idle_timeout parameter to prevent unbounded session memory growth#3443abhijeethp wants to merge 1 commit intoPrefectHQ:mainfrom
abhijeethp wants to merge 1 commit intoPrefectHQ:mainfrom
Conversation
… session memory growth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Add
session_idle_timeoutparameter to prevent unbounded session memory growthIn stateful HTTP mode (the default),
StreamableHTTPSessionManageraccumulates sessions in an in-memory dict without any TTL, cleanup, or max session limit. Disconnected clients leave orphaned sessions with live tasks and memory streams. Terminated sessions persist as zombie entries. Over time this causes unbounded memory growth, eventually leading to OOM on long-running servers.The MCP Python SDK has merged support for
session_idle_timeout([modelcontextprotocol/python-sdk#2022](modelcontextprotocol/python-sdk#2022), [modelcontextprotocol/python-sdk#1994](modelcontextprotocol/python-sdk#1994)) but has not yet released it. This PR threads the parameter through FastMCP so it is ready to use once the SDK releases.Changes
settings.py: Addsession_idle_timeout: float | None = None(configurable viaFASTMCP_SESSION_IDLE_TIMEOUTenv var)server/http.py: Acceptsession_idle_timeoutincreate_streamable_http_app(), pass it toStreamableHTTPSessionManagerwith runtime detection — logs a warning if the installed MCP SDK doesn't support it yetserver/mixins/transport.py: Threadsession_idle_timeoutthroughhttp_app()andrun_http_async()with settings fallbacktests/client/test_streamable_http.py: 6 new tests covering parameter threading, settings defaults, settings override, SDK compatibility warning, and end-to-end idle session cleanup (auto-skipped until SDK releases)Usage (once MCP SDK >= 1.27.0 is released)
Or via environment variable:
export FASTMCP_SESSION_IDLE_TIMEOUT=1800This PR was authored with the help of Claude Code.
Contributors Checklist
Review Checklist