Skip to content

Commit 6b85e59

Browse files
author
widgetwalker-username
committed
Fix SSE timeout hang by propagating transport exceptions to pending requests
This fix ensures that ClientSession.request() does not hang indefinitely when the underlying SSE transport encounters a timeout or other fatal exception before the RPC response is received. It propagates the exception to all in-flight request streams, waking up waiters immediately.
1 parent 8f806da commit 6b85e59

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/mcp/shared/session.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,21 @@ async def _handle_session_message(message: SessionMessage) -> None:
428428
async for message in self._read_stream:
429429
if isinstance(message, Exception):
430430
await self._handle_incoming(message)
431+
432+
# Fix #1401: Propagate exception to all pending requests
433+
# This prevents waiters from hanging when the transport fails
434+
error_data = (
435+
message.to_error_data()
436+
if isinstance(message, MCPError)
437+
else ErrorData(code=0, message=str(message))
438+
)
439+
jsonrpc_error = JSONRPCError(jsonrpc="2.0", id=None, error=error_data) # id=None because it applies to all
440+
441+
# We must send an error to every individual waiter
442+
for req_id, stream in list(self._response_streams.items()):
443+
# Send a response with the correct ID
444+
await stream.send(JSONRPCError(jsonrpc="2.0", id=req_id, error=error_data))
445+
431446
continue
432447

433448
await _handle_session_message(message)

0 commit comments

Comments
 (0)