Skip to content

Commit 711aecd

Browse files
author
widgetwalker-username
committed
Refine fix: explicitly pop and close response streams to avoid double-processing
1 parent 58aca04 commit 711aecd

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/mcp/shared/session.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,13 @@ async def _handle_session_message(message: SessionMessage) -> None:
439439

440440
# We must send an error to every individual waiter
441441
for req_id, stream in list(self._response_streams.items()):
442-
# Send a response with the correct ID
443-
await stream.send(JSONRPCError(jsonrpc="2.0", id=req_id, error=error_data))
444-
442+
try:
443+
# Send a response with the correct ID
444+
await stream.send(JSONRPCError(jsonrpc="2.0", id=req_id, error=error_data))
445+
finally:
446+
# Ensure we clean up the stream so finally block doesn't double-handle
447+
self._response_streams.pop(req_id, None)
448+
await stream.aclose()
445449
continue
446450

447451
await _handle_session_message(message)

0 commit comments

Comments
 (0)