Skip to content

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

Closed

Description

@campoy

What are you really trying to do?

When an MCP connection fails, keep its error available to callers and let later calls open a new connection.

Describe the bug

Each MCP connection has a task that keeps its session open.

If that task fails after the session is ready, _run() removes the connection from the cache and raises the error. Nobody may be waiting for that task, so asyncio logs:

Task exception was never retrieved

The connection record also forgets the failure. Calling session() on that record returns the old session instead of raising the error.

How did you hit this issue?

Found during review of #531.

That PR handles failures before the session is ready. We left failures after readiness for a separate fix.

Minimal Reproduction

The failure sequence is:

  1. Open an MCP connection and wait until its session is ready.
  2. Make the task holding the session fail.
  3. Call session() on the old connection record.
  4. It returns the old session instead of raising the failure.
  5. Once the failed task is collected, asyncio can log the unhandled exception.

Environment/Versions

Package: temporalio-google-genai.

Affected code: _ConnectionRecord._run() and session() in _mcp.py. This bug predates #531.

Additional context

Store the failure on the connection record and have session() raise it. The shared pool in python/mcp already follows this approach.

Add tests that check:

  • The old record raises the original error.
  • A later call opens a fresh connection.
  • Asyncio reports no unhandled task exception.

Use events to control when the connection fails, so the tests don't depend on timing.

Activity

  1. added
    bug
    on Nov 16, 2026
  2. rahulsjha commented on Nov 16, 2026

    @rahulsjha

    Hi @brianstrauch @campoy, I'd like to pick this up. Plan: in python/google_genai/src/temporalio/google_genai/_mcp.py, have _ConnectionRecord._run store a failure that happens after the session is ready instead of re-raising it, and have session() raise that stored error, following the shared pool in python/mcp. The pre-ready path from #531 and the cancellation and interpreter-exit propagation stay unchanged, and the record still drops itself from _CONNECTIONS so the next call reconnects. Tests driven by asyncio.Event, not timing: the old record raises the original error, a later call opens a fresh connection, and no 'Task exception was never retrieved' is logged. Could you assign it to me?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions