-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat: add idle timeout for StreamableHTTP sessions #1994
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
felixweinberger
wants to merge
5
commits into
v1.x
Choose a base branch
from
fweinberger/idle-timeout-termination
base: v1.x
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.
+135
−20
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2c87708
feat: add idle timeout for StreamableHTTP sessions
felixweinberger 7345bd7
Clean up idle timeout implementation
felixweinberger 2c9e60c
Remove _effective_idle_timeout, use session_idle_timeout directly
felixweinberger d5686df
Address review feedback: docstring formatting, test reorganization
felixweinberger 4191aa6
Address latest review feedback
felixweinberger 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -313,3 +313,80 @@ async def mock_receive(): | |
| assert error_data["id"] == "server-error" | ||
| assert error_data["error"]["code"] == INVALID_REQUEST | ||
| assert error_data["error"]["message"] == "Session not found" | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_idle_session_is_reaped(): | ||
| """After idle timeout fires, the session returns 404.""" | ||
| app = Server("test-idle-reap") | ||
| manager = StreamableHTTPSessionManager(app=app, session_idle_timeout=0.05) | ||
|
|
||
| async with manager.run(): | ||
| sent_messages: list[Message] = [] | ||
|
|
||
| async def mock_send(message: Message): | ||
| sent_messages.append(message) | ||
|
|
||
| scope = { | ||
| "type": "http", | ||
| "method": "POST", | ||
| "path": "/mcp", | ||
| "headers": [(b"content-type", b"application/json")], | ||
| } | ||
|
|
||
| async def mock_receive(): # pragma: no cover | ||
| return {"type": "http.request", "body": b"", "more_body": False} | ||
|
|
||
| await manager.handle_request(scope, mock_receive, mock_send) | ||
|
|
||
| session_id = None | ||
| for msg in sent_messages: # pragma: no branch | ||
| if msg["type"] == "http.response.start": # pragma: no branch | ||
| for header_name, header_value in msg.get("headers", []): # pragma: no branch | ||
| if header_name.decode().lower() == MCP_SESSION_ID_HEADER.lower(): | ||
| session_id = header_value.decode() | ||
| break | ||
| if session_id: # pragma: no branch | ||
| break | ||
|
|
||
| assert session_id is not None, "Session ID not found in response headers" | ||
|
|
||
| # Wait for the 50ms idle timeout to fire and cleanup to complete | ||
| await anyio.sleep(0.1) | ||
|
Contributor
Author
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. After some back and forth trying out event based stuff, given we're implementing timeout behavior here, this actually seems like the right way to test this - a case where a |
||
|
|
||
| # Verify via public API: old session ID now returns 404 | ||
| response_messages: list[Message] = [] | ||
|
|
||
| async def capture_send(message: Message): | ||
| response_messages.append(message) | ||
|
|
||
| scope_with_session = { | ||
| "type": "http", | ||
| "method": "POST", | ||
| "path": "/mcp", | ||
| "headers": [ | ||
| (b"content-type", b"application/json"), | ||
| (b"mcp-session-id", session_id.encode()), | ||
| ], | ||
| } | ||
|
|
||
| await manager.handle_request(scope_with_session, mock_receive, capture_send) | ||
|
|
||
| response_start = next( | ||
| (msg for msg in response_messages if msg["type"] == "http.response.start"), | ||
| None, | ||
| ) | ||
| assert response_start is not None | ||
| assert response_start["status"] == 404 | ||
|
|
||
|
|
||
| def test_session_idle_timeout_rejects_non_positive(): | ||
| with pytest.raises(ValueError, match="positive number"): | ||
| StreamableHTTPSessionManager(app=Server("test"), session_idle_timeout=-1) | ||
| with pytest.raises(ValueError, match="positive number"): | ||
| StreamableHTTPSessionManager(app=Server("test"), session_idle_timeout=0) | ||
|
|
||
|
|
||
| def test_session_idle_timeout_rejects_stateless(): | ||
| with pytest.raises(RuntimeError, match="not supported in stateless"): | ||
| StreamableHTTPSessionManager(app=Server("test"), session_idle_timeout=30, stateless=True) | ||
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.
What happens if the response takes more than the deadline?
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.
Hmm good question - there's no logic to prevent the cleanup from happening, so the response wouldn't make it back before the Transport gets closed. It'd get a
ClosedResourceErrorand the response would be lost. I'd probably argue that's something for the server dev to calibrate for their use case if they have very long running requests? Default stays no timeout anyway.