Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/mcp/client/sse.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ async def sse_reader(
raise sse_exc
except Exception as exc:
logger.exception("Error in sse_reader")
await read_stream_writer.send(exc)
try:
await read_stream_writer.send(exc)
except anyio.ClosedResourceError:
# Stream already closed during shutdown, which is expected
logger.debug("Stream closed during error handling - ignoring")
finally:
await read_stream_writer.aclose()

Expand Down Expand Up @@ -142,7 +146,13 @@ async def post_writer(endpoint_url: str):
try:
yield read_stream, write_stream
finally:
tg.cancel_scope.cancel()
# FIX: Removed manual tg.cancel_scope.cancel() that violated anyio lifecycle
# The original code called tg.cancel_scope.cancel() here, but this caused:
# "RuntimeError: Attempted to exit cancel scope in a different task than it was entered in"
#
# The task group's __aexit__ will properly cancel all child tasks when
# this context manager exits, so no manual cancellation is needed.
pass
finally:
await read_stream_writer.aclose()
await write_stream.aclose()
Loading