googleapis / googleapis/mcp-toolbox-sdk-python

fix(core): close managed session after cancelled initialization

Open
#744 1 comment 0 reactions 1 assignee Claimed by @anubhav756 View on GitHub
priority: p2 type: bug
Dominant language
Python
Stars
191
Forks
60
Avg merge
5d 20m
Merged PRs (30d)
13

Description

## 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.