-
Notifications
You must be signed in to change notification settings - Fork 4k
Close client SSE iterators explicitly when supported #3543
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
Kludex
wants to merge
1
commit into
bound-subscription-cleanup
Choose a base branch
from
close-client-sse-iterators
base: bound-subscription-cleanup
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.
Open
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -1,11 +1,12 @@ | ||
| """Utilities for creating and using httpx2 AsyncClient instances in the MCP transports.""" | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from collections.abc import AsyncGenerator | ||
| from collections.abc import AsyncGenerator, AsyncIterator | ||
| from contextlib import asynccontextmanager | ||
| from typing import Any, Protocol | ||
| from typing import Any | ||
|
|
||
| import httpx2 | ||
| from typing_extensions import Protocol, runtime_checkable | ||
|
|
||
| __all__ = ["create_mcp_http_client", "MCP_DEFAULT_TIMEOUT", "MCP_DEFAULT_SSE_READ_TIMEOUT"] | ||
|
|
||
|
|
@@ -165,6 +166,22 @@ async def sse_within_origin( | |
| yield httpx2.EventSource(response) | ||
|
|
||
|
|
||
| @runtime_checkable | ||
| class _AsyncClosable(Protocol): | ||
| async def aclose(self) -> None: ... | ||
|
|
||
|
|
||
| @asynccontextmanager | ||
| async def sse_events(source: httpx2.EventSource) -> AsyncGenerator[AsyncIterator[httpx2.ServerSentEvent]]: | ||
| """Close the outer EventSource iterator if supported; HTTPX2 owns its nested iterators.""" | ||
| events = source.__aiter__() | ||
| try: | ||
| yield events | ||
| finally: | ||
| if isinstance(events, _AsyncClosable): | ||
| await events.aclose() | ||
|
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. P2: When an SSE reader is cancelled, Prompt for AI agents |
||
|
|
||
|
|
||
| def redirect_location(response: httpx2.Response) -> httpx2.URL | None: | ||
| """Where `response` redirects to, for use in a message: without userinfo, query or fragment, | ||
| which can carry state that does not belong in an error or a log line. None if not a redirect.""" | ||
|
|
||
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
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.
When a reader is cancelled—such as during transport teardown or modern request cancellation—AnyIO's level cancellation remains active while this
finallyblock runs, so anaclose()implementation that reaches an async checkpoint is cancelled before cleanup completes. The new tests miss this because their closers perform no checkpoint, while real iterator cleanup commonly awaits nested resources; repeated cancellations can therefore retain stream resources despite the newly documented guarantee. Run the close operation inside a shielded cancellation scope, ideally with an appropriate bound.Useful? React with 👍 / 👎.