fix: preserve exception propagation through transport cleanup#2591
fix: preserve exception propagation through transport cleanup#2591
Conversation
anyio task groups suppress exceptions when cancel_scope.cancel() is called during cleanup. Capture exceptions before cleanup and re-raise after task group exits cleanly. Also preserve McpError type in client _connect() so callers can catch protocol-level errors specifically.
WalkthroughThe pull request enhances exception handling in the FastMCP client across two files. In Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
When exceptions occur during client initialization (e.g.,
McpErrorfrom server middleware), they were being swallowed and converted to "generator didn't yield" errors. This specifically affected in-memory transport where the server lifespan (including Docket's background worker context managers) is nested inside the client's task group - the complex cleanup chain was suppressing exceptions.Root cause: anyio task groups suppress exceptions when
cancel_scope.cancel()is called during cleanup.Fix: Capture exceptions before task group cleanup and re-raise them after the task group exits cleanly:
Also preserves
McpErrortype in_connect()so callers can catch protocol-level errors:Relevant to fixing #2531