Repository navigation
fix(mcp): close connected servers when context startup is cancelled - #1028
Open
ranjianghong526-hash wants to merge 1 commit into
Open
ranjianghong526-hash wants to merge 1 commit into
ranjianghong526-hash wants to merge 1 commit into
Conversation
This branch has not been deployed
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
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.
Change Summary
Cancelling
async with MCPClient(...)during parallel startup can leave servers that have already initialized connected.CancelledErrorbypasses theexcept Exceptionin__aenter__, and Python does not call__aexit__when entry fails. An initialized stdio server continues answering tool calls after the cancelled entry.BaseExceptionduring entry, then re-raise the original exception. Transports still close in their existing owner tasks.Related issue number
Independently discovered and reproduced on
mainat9b0244a43c6a15b7d3f52d0f1c50617ba00e7268; no existing issue is being closed.Validation
tests/mcpandtests/config/test_mcp_resolver.py) passes all 36 tests on each platform.test_classify_mcp_failureandtest_is_connection_errorexpect builtinTimeoutErrorto be the same asasyncio.TimeoutError. Both failures reproduce on the unmodified upstream client; their runtime implementation and tests match upstream byte-for-byte. They are outside this resource-ownership change. The new tests use the already-installed AnyIO timeout context to support Python 3.10.pongafter failed entry. After, both owner tasks are finished and all session/task/shutdown registries are empty; cancellation still propagates.git diff --checkpass.pre-commit run --all-fileswas run, but is not green: the clean upstream control fails the same flake8/isort/yapf/trailing-whitespace/string/line-ending hooks. All 69 flake8 diagnostics are identical. Unrelated automatic fixes are excluded from this PR.This is scoped validation, not a full repository or live model-provider test run. The existing ready-future cancellation diagnostic is reproducible before and after; this PR addresses connected-resource ownership only.
AI assistance
Codex assisted with source/duplication investigation, real-process reproduction, the minimal fix, tests, validation and this draft.
Checklist
pre-commit installandpre-commit run --all-filesbefore git commit, and passed lint check. (Run; existing upstream failures described above.)