Skip to content

Commit dcac243

Browse files
committed
fix: Improve streamable HTTP client stream cleanup with comprehensive exception handling
1 parent 3343410 commit dcac243

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/mcp/client/streamable_http.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -413,22 +413,15 @@ async def handle_request_async():
413413
except Exception as exc:
414414
logger.error(f"Error in post_writer: {exc}")
415415
finally:
416-
# Improved stream cleanup with comprehensive exception handling
417-
try:
418-
await read_stream_writer.aclose()
419-
except (anyio.ClosedResourceError, anyio.BrokenResourceError):
420-
# Stream already closed, ignore
421-
pass
422-
except Exception as exc:
423-
logger.debug(f"Error closing read_stream_writer in cleanup: {exc}")
424-
416+
# Only close the write stream here, read_stream_writer is shared
417+
# and will be closed in the main cleanup
425418
try:
426419
await write_stream.aclose()
427420
except (anyio.ClosedResourceError, anyio.BrokenResourceError):
428421
# Stream already closed, ignore
429422
pass
430423
except Exception as exc:
431-
logger.debug(f"Error closing write_stream in cleanup: {exc}")
424+
logger.debug(f"Error closing write_stream in post_writer cleanup: {exc}")
432425

433426
async def terminate_session(self, client: httpx.AsyncClient) -> None:
434427
"""Terminate the session by sending a DELETE request."""
@@ -522,19 +515,35 @@ def start_get_stream() -> None:
522515
logger.debug(f"Error terminating session: {exc}")
523516
tg.cancel_scope.cancel()
524517
finally:
525-
# Improved stream cleanup with comprehensive exception handling
518+
# Comprehensive stream cleanup with exception handling
526519
try:
527520
await read_stream_writer.aclose()
528521
except (anyio.ClosedResourceError, anyio.BrokenResourceError):
529522
# Stream already closed, ignore
530523
pass
531524
except Exception as exc:
532-
logger.debug(f"Error closing read_stream_writer in cleanup: {exc}")
525+
logger.debug(f"Error closing read_stream_writer in main cleanup: {exc}")
533526

534527
try:
535528
await write_stream.aclose()
536529
except (anyio.ClosedResourceError, anyio.BrokenResourceError):
537530
# Stream already closed, ignore
538531
pass
539532
except Exception as exc:
540-
logger.debug(f"Error closing write_stream in cleanup: {exc}")
533+
logger.debug(f"Error closing write_stream in main cleanup: {exc}")
534+
535+
try:
536+
await read_stream.aclose()
537+
except (anyio.ClosedResourceError, anyio.BrokenResourceError):
538+
# Stream already closed, ignore
539+
pass
540+
except Exception as exc:
541+
logger.debug(f"Error closing read_stream in main cleanup: {exc}")
542+
543+
try:
544+
await write_stream_reader.aclose()
545+
except (anyio.ClosedResourceError, anyio.BrokenResourceError):
546+
# Stream already closed, ignore
547+
pass
548+
except Exception as exc:
549+
logger.debug(f"Error closing write_stream_reader in main cleanup: {exc}")

0 commit comments

Comments
 (0)