fix(mcp): cleanup cancelled servers during connect failure#3919
Merged
seratch merged 1 commit intoJul 23, 2026
Conversation
hsusul
force-pushed
the
fix/mcp-manager-cancelled-connect-cleanup
branch
from
July 23, 2026 05:38
82abf55 to
1a26008
Compare
Record CancelledError failures before re-raising when suppression is disabled so connect_all() includes the cancelled server in its failure cleanup path, matching BaseException handling and avoiding leaks under async with where __aexit__ never runs.
hsusul
force-pushed
the
fix/mcp-manager-cancelled-connect-cleanup
branch
from
July 23, 2026 05:38
1a26008 to
5457733
Compare
seratch
enabled auto-merge (squash)
July 23, 2026 07:45
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.
Summary
When
MCPServerManageris configured withsuppress_cancelled_error=False, aCancelledErrorraised duringconnect()was re-raised without recording the server as failed. Sequentialconnect_all()failure cleanup only walksconnected_servers + failed_servers, so the cancelled server was skipped. Under the recommendedasync with MCPServerManager(...)pattern,__aexit__never runs if__aenter__raises, so there is no later chance to callcleanup_all()— leaving transports / subprocesses / sessions open.This aligns
CancelledErrorhandling with the existingBaseExceptionpath: always_record_failure(...)first, then re-raise when suppression is disabled.Root cause
In
_attempt_connect:Neighboring
BaseExceptionhandling already records before re-raising, and the lifecycle reference requires partial connection failures to close every context already entered.Reproduction
Before this change: cancelled server stays
resource_open=True,cleanup_calls=0.After this change:
connect_all()failure cleanup runscleanup()on the cancelled server.Solution
Record the cancelled server via
_record_failurebefore optionally re-raising, soconnect_all()'s sequential failure cleanup includes it (same contract as fatalBaseExceptionconnect failures).Test plan
test_manager_cleanup_runs_on_cancelled_error_during_connectto assert the cancelled server is recorded and cleaned byconnect_all()itselftest_manager_async_with_cleans_cancelled_server_when_unsuppressedcovering theasync withleak path.agents/skills/code-change-verification/scripts/run.sh(format, lint, typecheck, full tests) — all passedgit diff --checkcleanIssue number
N/A — found via code-path review; no existing issue/PR covering this exact gap (nearby MCP cleanup work targets stdio semaphore / exit-stack reconnect issues, not this manager cancellation recording path).
Checks
.agents/skills/code-change-verification/scripts/run.sh/reviewbefore submitting this PRCompatibility and risk
suppress_cancelled_error=Truebehavior is unchanged (still records and does not re-raise).Validation