Skip to content

[python/google_genai] preserve MCP failure occurring after connection establishment (#572) - #630

Merged
brianstrauch merged 2 commits into
temporalio:mainfrom
vjsujal:fix-572-mcp-post-connect-failure
Nov 18, 2026
Merged

brianstrauch merged 2 commits into
temporalio:mainfrom
vjsujal:fix-572-mcp-post-connect-failure

Conversation

@vjsujal

@vjsujal vjsujal commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #572

Root cause

When an MCP connection fails after establishing a session, _ConnectionRecord._run() caught the exception and re-raised it because self._ready.done() was true. Because self._owner is a background task not awaited outside aclose(), re-raising caused an unhandled task exception (Task exception was never retrieved). Furthermore, the failure was lost from the connection record, causing subsequent calls to session() to return a stale session instead of raising the error.

Fix

  • Add self._failure to _ConnectionRecord.
  • On post-connection failure in _run(), store the error in self._failure, cancel any active idle timer handle, and do not re-raise standard exceptions into the un-awaited owner task.
  • In _ConnectionRecord.session(), check self._failure and raise it if set.
  • BaseExceptions (CancelledError, SystemExit, etc.) continue to propagate.

Tests

  • Added test_mcp_failure_after_connect_reported_and_creates_fresh_connection verifying that post-connect failures are raised on session(), subsequent requests obtain a fresh connection, and no unhandled task exceptions are logged.
  • Full test suite passing (278 passed).
  • Formatting, pyright, mypy, basedpyright, and pydocstyle checks passing.

… establishment (temporalio#572)

Fixes temporalio#572

### Root cause
When an MCP connection fails after establishing a session, _ConnectionRecord._run() caught the exception and re-raised it because self._ready.done() was true. Because self._owner is a background task not awaited outside aclose(), re-raising caused an unhandled task exception (Task exception was never retrieved). Furthermore, the failure was lost from the connection record, causing subsequent calls to session() to return a stale session instead of raising the error.

### Fix
- Add self._failure to _ConnectionRecord.
- On post-connection failure in _run(), store the error in self._failure, cancel any active idle timer handle, and do not re-raise standard exceptions into the un-awaited owner task.
- In _ConnectionRecord.session(), check self._failure and raise it if set.
- BaseExceptions (CancelledError, SystemExit, etc.) continue to propagate.

### Tests
- Added test_mcp_failure_after_connect_reported_and_creates_fresh_connection verifying that post-connect failures are raised on session(), subsequent requests obtain a fresh connection, and no unhandled task exceptions are logged.
- Full test suite passing (278 passed).
@vjsujal
vjsujal requested a review from a team as a code owner November 17, 2026 11:58
@CLAassistant

CLAassistant commented Nov 17, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@brianstrauch brianstrauch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The implementation appears sound, but the regression test falsely passes when unhandled task exceptions are reintroduced, confirmed through mutation testing. Full-suite execution was blocked by a locked dependency download failure.

@brianstrauch brianstrauch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

@brianstrauch
brianstrauch enabled auto-merge (squash) November 18, 2026 16:36
@brianstrauch
brianstrauch merged commit 76b1ba0 into temporalio:main Nov 18, 2026
16 checks passed
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.

[Bug] google_genai: MCP failures after connecting can log an unhandled task exception

3 participants