fix(core): isolate shared initialization cancellation - #745
Open
hsusul wants to merge 1 commit into
Open
Conversation
4 tasks
Contributor
|
/gcbrun |
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.
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
🛠️ Fixes #743
Summary
Problem
All first MCP requests share one
_init_taskso session initialization runs once. However,_ensure_initialized()awaited that task directly. Python therefore propagated cancellation from any waiting request into the shared task.A minimal reproduction starts two
_ensure_initialized()waiters while initialization is blocked on an event, then cancels only the first waiter.Before this change:
The cancelled request unintentionally cancelled unrelated concurrent and later requests.
Root cause
_McpHttpTransportBase._ensure_initialized()retained one shared task but usedawait self._init_task. Cancellation of a coroutine awaiting another task is propagated to that task unless it is shielded.Solution
Await the retained initialization task through
asyncio.shield(). The calling request still receivesCancelledError, while the shared initialization continues for other waiters.Genuine initialization exceptions are unchanged and still propagate to waiters.
Tests
python -m pytest tests/mcp_transport/test_base.py::TestMcpHttpTransportBase::test_cancelled_waiter_does_not_cancel_shared_initialization -vCancelledError.python -m pytest tests/mcp_transport/test_base.py -q— 38 passed.python -m pytest tests -q --ignore=tests/test_e2e.py --ignore=tests/test_sync_e2e.py --ignore=tests/test_e2e_mcp.py --ignore=tests/conformance --cov=src/toolbox_core --cov-report=term --cov-fail-under=90— 481 passed, 15 warnings, 91.71% coverage.python -m pytest tests/unit -qintoolbox-adk— 59 passed, 10 dependency deprecation warnings.python -m pytest -k "not e2e" -qintoolbox-langchain— 60 passed, 64 deselected.python -m pytest -k "not e2e" -qintoolbox-llamaindex— 61 passed, 64 deselected.Validation
black --check .— passed; 44 files unchanged.isort --check .— passed.MYPYPATH='./src' mypy --install-types --non-interactive --cache-dir=.mypy_cache/ -p toolbox_core— passed; no issues in 29 source files.git diff upstream/main...HEAD --check— passed.Compatibility and risk
There is no public API, schema, serialization, authentication, telemetry, or successful-call behavior change. The change applies uniformly to every MCP protocol implementation through the shared transport base.
Shielding means initialization can finish after the request that first triggered it is cancelled. This is intentional because the task is retained by the transport and may already serve other requests.
Non-goals
close()cancellation semantics.Documentation
No documentation change is required because this restores internal lifecycle isolation without changing the public interface.
Related issues and prior work
AI assistance
This issue investigation and patch were prepared with AI assistance. The reproduction, regression sensitivity, diff, and validation results were verified locally against current
upstream/main.