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

@atishay-kasliwal atishay-kasliwal 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.

The new test fails on main and passes here. test_client.py + test_client_caching.py go from 113 to 114 passed.

I also checked the client after three kinds of exit, using the _varying_tools_server helper. After a normal exit, list_tools() and client.session raise on both branches. After a RuntimeError inside the block, and after an outer anyio.CancelScope cancels the block, main still serves the cached ['t0'] and returns the session object. Here both raise Client must be used within an async context manager and the server sees no extra fetch. Re-entering after an exceptional exit still raises Client is already entered, so the docs line about creating a new Client matches.

The cancel case goes through the same finally, so I think the one new test is enough. Looks good to me.

@Raghucharan16 Raghucharan16 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.

Thanks for this. It's a small, correct fix. I checked it locally:

  • I wrote a small in-memory repro (an in-process MCPServer, no network). On main, _session is still set after the block exits with a plain exception, with an anyio cancellation (move_on_after), and with an ExceptionGroup from a task group. With this PR it's cleared in all three cases, and the normal exit still works as before.
  • The new regression test fails on main and passes here. tests/client/test_client_caching.py plus tests/client/test_client.py give 114 passed on Windows.
  • Re-entering after an exceptional exit is still rejected by the existing _entered guard, which matches the updated docs.

Optional nits: you could also reset _exit_stack = None in the finally for tidiness, and a small cancellation case (e.g. anyio.move_on_after around the block) would lock in the cancellation behaviour too. Neither is blocking.

LGTM 👍

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

3 participants