-
Notifications
You must be signed in to change notification settings - Fork 376
fix(mcp): auto cleanup on exceptions occurring in __enter__ #833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+126
−18
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dbschmigelski
commented
Sep 9, 2025
Unshure
previously approved these changes
Sep 11, 2025
JackYPCOnline
previously approved these changes
Sep 11, 2025
7c773c8
zastrowm
approved these changes
Sep 16, 2025
JackYPCOnline
approved these changes
Sep 16, 2025
This was referenced Sep 16, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixed a critical race condition in MCPClient that caused hanging when initialization failed due to timeout. The root issue was misunderstanding Python's context manager protocol: exit is only called if enter succeeds. Since we performed heavy initialization work in enter, failures left background threads running indefinitely with no cleanup mechanism.
The fix adds defensive cleanup directly in the start() method to call stop() on any initialization failure, ensuring proper resource cleanup regardless of where the failure occurs. Made stop() method robust to handle partial initialization states where event loop or session may not exist yet.
I added TODO comment acknowledging that the current approach of doing heavy work in enter is non-idiomatic Python. The proper solution would be lazy initialization on first method call, but this fix ensures no resource leaks in the current architecture.
Related Issues
#664
Documentation PR
Note needed
Type of Change
Bug fix
Testing
Added unit tests for timeout cleanup, defensive stop behavior, and client reuse after failure.
Added integration test that reproduces the exact hanging scenario with real stdio_client. Verified no thread leaks occur during timeout scenarios and tested client recovery by adjusting timeout and successful reuse.
I ran
hatch run prepare
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.