Skip to content

fix(core): close managed session after cancelled initialization #744

Description

@hsusul

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

  1. Create a core MCP transport with its internally managed session.
  2. Assign or reach a cancelled initialization task.
  3. 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.

Metadata

Metadata

Assignees

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions