fix: prevent RuntimeError: Event loop is closed for Gemini.api_client (#5538)#5543
Open
weiguangli-io wants to merge 1 commit intogoogle:mainfrom
Open
fix: prevent RuntimeError: Event loop is closed for Gemini.api_client (#5538)#5543weiguangli-io wants to merge 1 commit intogoogle:mainfrom
weiguangli-io wants to merge 1 commit intogoogle:mainfrom
Conversation
In multi-threaded deployments (e.g. Vertex AI Agent Engine) each incoming request runs in a fresh OS thread with its own asyncio event loop. When the same Gemini instance is reused across requests the previously cached google.genai.Client (and its underlying aiohttp / httpx session) is bound to the old, now-closed event loop, causing: RuntimeError: Event loop is closed Replace @cached_property on api_client and _live_api_client with a regular @Property that caches the Client keyed by the current running event loop. Each distinct event loop (i.e. each new thread) receives a fresh Client with a fresh HTTP session, while calls within the same loop still reuse the cached instance. No public API is changed. Fixes: google#5538
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In multi-threaded deployments (e.g. Vertex AI Agent Engine), each incoming
request runs in a fresh OS thread with its own asyncio event loop. When the
same
Geminiinstance is reused across requests, the previously@cached_propertyapi_client(agoogle.genai.Client) is bound to theold, now-closed event loop via its underlying aiohttp / httpx session:
Root cause:
@cached_propertystores the firstClientcreated permanently onthe instance, regardless of which event loop is currently running.
Fixes #5538.
Solution
Replace
@cached_propertyonapi_clientand_live_api_clientwith aregular
@propertythat stores a(loop, client)pair in__dict__. On eachaccess:
(no extra allocation on the hot path).
Clientand cache it for the current loop.This means each OS thread's event loop gets an isolated
Clientinstance withits own HTTP session, while calls within the same loop still share one client.
No public API is changed. Subclasses that override
api_clientwith@cached_propertycontinue to work; the docstring hint to use@propertyinstead has been removed as the base class now handles this automatically.
Testing
test_api_client_cached_per_event_loop: two threads each run their ownevent loop and share a single
Geminiinstance; asserts they receive distinctClientobjects.test_api_client_cached_within_same_event_loop: asserts a singleClientis reused for repeated calls within one event loop.