Skip to content

Clear the Client session after an exceptional context exit - #3970

Open
jonxsong wants to merge 1 commit into
modelcontextprotocol:mainfrom
jonxsong:codex/fix-3968-client-exceptional-exit
Open

jonxsong wants to merge 1 commit into
modelcontextprotocol:mainfrom
jonxsong:codex/fix-3968-client-exceptional-exit

Conversation

@jonxsong

@jonxsong jonxsong commented Dec 3, 2026 •

Copy link
Copy Markdown

Fixes #3968

Clear the client session reference when the async context exits, even if the exit stack raises.

Motivation and Context

An application exception can skip the session reference cleanup in Client.__aexit__. A later list_tools() call can then return a cached result after the connection has closed. Clearing the reference in finally lets the existing connection guard reject the call while the application exception propagates.

How Has This Been Tested?

  • The regression fails on the original implementation and passes with the fix. It warms the cache, checks that the original exception propagates, and verifies that a later call raises the closed-client error.
  • The Windows CPython 3.12.4 coverage run passed 6,226 tests with 16 skipped, one expected failure, and 100% coverage. The regression also passed 15 repeated runs and an in-process Trio smoke test.
  • Ruff lint and formatting, Markdown lint, and repository-wide Pyright with the Linux platform passed. Native Windows Pyright passed for both changed Python files. Its repository-wide run reported platform diagnostics in untouched files.

Breaking Changes

The existing closed-client error now applies after an exceptional exit, as it already does after a normal exit.

Types of changes

  • Bug fix
  • Documentation update

Additional context

Codex helped with implementation and testing. Report written, review, and validation by Jon Zhang.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

Re-trigger cubic

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Client serves cached tools after its context exits with an exception

1 participant