| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| 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"] | ||
|
|
||
| Expand Down Expand Up | @@ -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() | ||
|
Comment thread
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityP2: When an SSE reader is cancelled, sse_events awaits aclose() inside the already-cancelled AnyIO scope, so asynchronous iterator cleanup can be cancelled and the response resources can leak. Run events.aclose() inside a shielded anyio.CancelScope. Prompt for AI agentsCheck if this issue is valid — if so, understand the root cause and fix it. At src/mcp/shared/_httpx_utils.py, line 182:
<comment>When an SSE reader is cancelled, `sse_events` awaits `aclose()` inside the already-cancelled AnyIO scope, so asynchronous iterator cleanup can be cancelled and the response resources can leak. Run `events.aclose()` inside a shielded `anyio.CancelScope`.</comment>
<file context>
@@ -165,6 +166,22 @@ async def sse_within_origin(
+ yield events
+ finally:
+ if isinstance(events, _AsyncClosable):
+ await events.aclose()
+
+
</file context>
Sorry, something went wrong.
All reactions
|
||
|
|
||
|
|
||
| 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.""" | ||
| Expand Down | ||
| Back | FazBrowse Home | New Git URL |
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityWhen a reader is cancelled—such as during transport teardown or modern request cancellation—AnyIO's level cancellation remains active while this finally block runs, so an aclose() 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 👍 / 👎.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.