Summary
When an MCP transport initialization task is cancelled, _McpHttpTransportBase.close() propagates asyncio.CancelledError before closing its internally managed aiohttp.ClientSession.
Environment
- SDK revision:
88cc7959d9c7ebc1d92dedeae90e56fb10d2a943 (upstream/main)
- Python: 3.13.5
- Toolbox server: not required; the failure occurs in local transport cleanup
Current behavior
- Create a core MCP transport with its internally managed session.
- Assign or reach a cancelled initialization task.
- Call
await transport.close().
close() raises CancelledError, and transport._session.closed remains False.
Expected behavior
Explicitly closing the transport should close its internally managed session even when initialization was cancelled, just as it already does for other initialization failures. Externally supplied sessions should remain caller-owned.
Root cause
asyncio.CancelledError inherits from BaseException, so the existing except Exception cleanup path does not catch it and session cleanup is skipped.
Proposed scope
Handle the cancelled cached initialization task inside close() and add focused regression coverage. Initialization retry and request-cancellation semantics are out of scope.
AI assistance
OpenAI Codex assisted with repository auditing, reproduction, and test preparation.
Summary
When an MCP transport initialization task is cancelled,
_McpHttpTransportBase.close()propagatesasyncio.CancelledErrorbefore closing its internally managedaiohttp.ClientSession.Environment
88cc7959d9c7ebc1d92dedeae90e56fb10d2a943(upstream/main)Current behavior
await transport.close().close()raisesCancelledError, andtransport._session.closedremainsFalse.Expected behavior
Explicitly closing the transport should close its internally managed session even when initialization was cancelled, just as it already does for other initialization failures. Externally supplied sessions should remain caller-owned.
Root cause
asyncio.CancelledErrorinherits fromBaseException, so the existingexcept Exceptioncleanup path does not catch it and session cleanup is skipped.Proposed scope
Handle the cancelled cached initialization task inside
close()and add focused regression coverage. Initialization retry and request-cancellation semantics are out of scope.AI assistance
OpenAI Codex assisted with repository auditing, reproduction, and test preparation.