-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(fastmcp): add session_idle_timeout parameter to prevent unbounded session memory growth #3443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abhijeethp
wants to merge
4
commits into
PrefectHQ:main
Choose a base branch
from
abhijeethp:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+129
−9
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e77fefd
fix(fastmcp): add session_idle_timeout parameter to prevent unbounded…
abhijeethp 445698a
Merge branch 'PrefectHQ:main' into main
abhijeethp 909684c
Require mcp>=1.27.0 and remove runtime SDK detection
abhijeethp 9d8c7b0
Merge branch 'PrefectHQ:main' into main
abhijeethp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -289,3 +289,94 @@ async def test_timeout_tool_call_overrides_client_timeout( | |
| ) as client: | ||
| with pytest.raises(McpError): | ||
| await client.call_tool("sleep", {"seconds": 0.2}, timeout=0.1) | ||
|
|
||
|
|
||
| class TestSessionIdleTimeout: | ||
| async def test_session_idle_timeout_parameter_threads_through(self): | ||
| """session_idle_timeout should be accepted by http_app() without errors.""" | ||
| server = create_test_server() | ||
| # Should not raise regardless of SDK support | ||
| app = server.http_app(session_idle_timeout=30.0) | ||
| assert app is not None | ||
|
|
||
| async def test_session_idle_timeout_none_by_default(self): | ||
| """session_idle_timeout should default to None (no timeout).""" | ||
| import fastmcp | ||
|
|
||
| assert fastmcp.settings.session_idle_timeout is None | ||
|
|
||
| async def test_session_idle_timeout_from_settings(self): | ||
| """session_idle_timeout should be configurable via settings.""" | ||
| import fastmcp | ||
|
|
||
| original = fastmcp.settings.session_idle_timeout | ||
| try: | ||
| fastmcp.settings.session_idle_timeout = 60.0 | ||
| server = create_test_server() | ||
| # Should not raise - the setting is picked up | ||
| app = server.http_app() | ||
| assert app is not None | ||
| finally: | ||
| fastmcp.settings.session_idle_timeout = original | ||
|
|
||
| async def test_session_idle_timeout_explicit_overrides_settings(self): | ||
| """Explicit session_idle_timeout should override settings value.""" | ||
| import fastmcp | ||
|
|
||
| original = fastmcp.settings.session_idle_timeout | ||
| try: | ||
| fastmcp.settings.session_idle_timeout = 60.0 | ||
| server = create_test_server() | ||
| # Explicit value should override settings | ||
| app = server.http_app(session_idle_timeout=120.0) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this one is probably fine due to type checking? |
||
| assert app is not None | ||
| finally: | ||
| fastmcp.settings.session_idle_timeout = original | ||
|
|
||
| async def test_session_idle_timeout_cleans_up_idle_sessions(self): | ||
| """Sessions should be cleaned up after the idle timeout expires.""" | ||
| import httpx | ||
|
|
||
| server = create_test_server() | ||
| async with run_server_async(server) as url: | ||
| async with httpx.AsyncClient() as http_client: | ||
| # Initialize a session | ||
| init_resp = await http_client.post( | ||
| url, | ||
| json={ | ||
| "jsonrpc": "2.0", | ||
| "id": "init-1", | ||
| "method": "initialize", | ||
| "params": { | ||
| "protocolVersion": "2025-03-26", | ||
| "capabilities": {}, | ||
| "clientInfo": { | ||
| "name": "test", | ||
| "version": "1.0.0", | ||
| }, | ||
| }, | ||
| }, | ||
| headers={ | ||
| "Accept": "application/json, text/event-stream", | ||
| }, | ||
| ) | ||
| session_id = init_resp.headers.get("Mcp-Session-Id", "") | ||
| assert session_id, "Should get a session ID" | ||
|
|
||
| # Wait for the session to expire (timeout is very short) | ||
| await asyncio.sleep(2.5) | ||
|
|
||
| # Try to use the expired session - should get 404 | ||
| resp = await http_client.post( | ||
| url, | ||
| json={ | ||
| "jsonrpc": "2.0", | ||
| "id": "test-1", | ||
| "method": "tools/list", | ||
| }, | ||
| headers={ | ||
| "Accept": "application/json, text/event-stream", | ||
| "Mcp-Session-Id": session_id, | ||
| }, | ||
| ) | ||
| assert resp.status_code == 404 | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a little weak of an assertion -- is there a way for us to verify this was applied to the server?