-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix handle sse disconnect event to free read_stream_writers[session_id] #582
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
lcamhoa
wants to merge
3
commits into
modelcontextprotocol:v1.3.x
Choose a base branch
from
lcamhoa:sse-disconnect
base: v1.3.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.
+81
−14
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import asyncio | ||
from uuid import UUID | ||
|
||
import pytest | ||
from starlette.types import Message, Scope | ||
|
||
from mcp.server.sse import SseServerTransport | ||
|
||
|
||
@pytest.mark.anyio | ||
async def test_sse_disconnect_handle(): | ||
transport = SseServerTransport(endpoint="/sse") | ||
# Create a minimal ASGI scope for an HTTP GET request | ||
scope: Scope = { | ||
"type": "http", | ||
"method": "GET", | ||
"path": "/sse", | ||
"headers": [], | ||
} | ||
send_disconnect = False | ||
|
||
# Dummy receive and send functions | ||
async def receive() -> dict: | ||
nonlocal send_disconnect | ||
if not send_disconnect: | ||
send_disconnect = True | ||
return {"type": "http.request"} | ||
else: | ||
return {"type": "http.disconnect"} | ||
|
||
async def send(message: Message) -> None: | ||
await asyncio.sleep(0) | ||
|
||
# Run the connect_sse context manager | ||
async with transport.connect_sse(scope, receive, send) as ( | ||
read_stream, | ||
write_stream, | ||
): | ||
# Assert that streams are provided | ||
assert read_stream is not None | ||
assert write_stream is not None | ||
|
||
# There should be exactly one session | ||
assert len(transport._read_stream_writers) == 1 | ||
# Check that the session key is a UUID | ||
session_id = next(iter(transport._read_stream_writers.keys())) | ||
assert isinstance(session_id, UUID) | ||
|
||
# Check that the writer is still open | ||
writer = transport._read_stream_writers[session_id] | ||
assert writer is not None | ||
|
||
# After context exits, session should be cleaned up | ||
assert len(transport._read_stream_writers) == 0 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 also handled in #586. let's review that one first and then we can review this one as we have a test here -- thank you for adding it