Repository navigation
fix(sessions): close Vertex clients on their owning event loop - #8479
Open
CaoYuhaoCarl wants to merge 1 commit into
Open
CaoYuhaoCarl wants to merge 1 commit into
CaoYuhaoCarl wants to merge 1 commit into
Conversation
Keep an async generator with each cached session client so orderly loop shutdown awaits transport cleanup. Preserve client reuse between session calls and leave subclass-supplied clients under caller ownership. Fixes google#8464
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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.
Link to issue
Closes #8464
When
VertexAiSessionServiceoutlived its event loop, the SDK tried to close that loop's pooled HTTP connection during the next request.All requests could succeed while cleanup failed with
RuntimeError: Event loop is closed.The session service now keeps a started async generator alongside each loop-owned client.
During
shutdown_asyncgens(), the owning loop awaitsclient.aclose()while the transport can still deliver its close callback.Calls on the same loop continue to reuse one client.
Subclasses that supply a client through
_get_api_client()retain ownership of it.Testing plan
The new regression test checks that two session calls reuse one client and that
asyncio.run()finishes closing it on the original loop.This test failed on
main.A second test covers a caller-owned client.
Manual end-to-end test
I ran the HTTP/1.1 reproduction from #8464 against
mainand the built wheel, using Python 3.12.15, google-cloud-agentplatform 2.10.0, google-genai 2.34.0 and httpx 0.28.1, without aiohttp or cloud credentials.All three requests succeeded in both runs.
mainlogged two unhandled cleanup exceptions and the wheel logged zero.I also issued two
list_sessionscalls perasyncio.run()on the same service and had the server wait for EOF after each run.On
main, the server never received EOF within two seconds of any loop ending.With the wheel installed, the server recorded:
Hosts that drive loops manually must call
loop.shutdown_asyncgens()beforeloop.close()for this cleanup to run, asasyncio.run()does.Unit tests and local checks
pytest -q tests/unittests/sessions/ tests/unittests/utils/test_event_loop_cache.pyon Python 3.11 passed 1,160 tests, with 26 expected failures.uv buildpassed.The first Python 3.11 run reported six failures in a nested agent-directory test, a stdio MCP evaluation test, and four LiveKit call tests.
All six passed when rerun together on both the branch and clean
main.The four full runs used
toxwith three pytest-xdist workers per environment and--dist=loadfile.The full Python 3.11 rerun passed with six workers and the default xdist scheduler.
Checklist