fix(core): close session after cancelled initialization - #746
Open
hsusul wants to merge 1 commit into
Open
Conversation
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 #744
Summary
Ensure
_McpHttpTransportBase.close()still closes its internally managedaiohttp.ClientSessionwhen the cached initialization task was cancelled.Problem
asyncio.CancelledErrorinherits directly fromBaseException, so the existingexcept Exceptionaround the initialization task did not handle cancellation.Calling
close()after a cancelled initialization therefore propagatedCancelledErrorbefore reaching session cleanup, leaving the managed sessionopen.
Minimal reproduction
aiohttp.ClientSession._init_task.await transport.close().Before this change,
close()raisesasyncio.CancelledErrorandtransport._session.closedremainsFalse. After this change, explicit cleanupcompletes and the managed session is closed.
Implementation
asyncio.CancelledErroralongside ordinary initialization exceptionsduring explicit transport cleanup.
the managed session from closing.
cleanup behavior.
The change does not alter initialization or request-time cancellation behavior,
and externally managed sessions remain unaffected.
Validation
python -m pytest tests/mcp_transport/test_base.py::TestMcpHttpTransportBase::test_close_managed_session_after_cancelled_initialization -vtransport.close()withasyncio.exceptions.CancelledError.1 passed.regression fail again; restoring it made the test pass.
python -m pytest tests/mcp_transport/test_base.py -q—39 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—
482 passed, total coverage91.80%.python -m pytest tests/unit -qintoolbox-adk—59 passed.python -m pytest tests -k 'not e2e' -qintoolbox-langchain—60 passed,64 deselected.python -m pytest tests -k 'not e2e' -qintoolbox-llamaindex—61 passed,64 deselected.black --check .— passed;44 files would be left unchanged.isort --check .— passed.MYPYPATH='./src' mypy --install-types --non-interactive --cache-dir=<temporary-cache> -p toolbox_core—
Success: no issues found in 29 source files.git diff upstream/main...HEAD --check— passed.Live E2E and conformance suites were not run locally because they require
external Toolbox/GCP infrastructure. Fork CI remains subject to the
repository's maintainer-triggered workflow.
Compatibility and risk
The runtime change is limited to explicit cleanup after an initialization task
has already been cancelled. Ordinary initialization errors, successful
initialization, and unmanaged-session cleanup retain their existing behavior.
Non-goals and related work
initialization behavior addressed independently by fix(core): isolate shared initialization cancellation #745. That PR explicitly
lists changing
close()cancellation semantics as a non-goal and modifies adifferent production hunk.
public APIs.
behaviors.
AI assistance
AI assistance was used for repository inspection, implementation, testing, and
drafting this pull request. The contributor reviewed the final diff and
validation evidence.