Skip to content

fix(sessions): close Vertex clients on their owning event loop - #8479

Open
CaoYuhaoCarl wants to merge 1 commit into
google:mainfrom
CaoYuhaoCarl:fix/vertex-session-client-shutdown
Open

CaoYuhaoCarl wants to merge 1 commit into
google:mainfrom
CaoYuhaoCarl:fix/vertex-session-client-shutdown

Conversation

@CaoYuhaoCarl

@CaoYuhaoCarl CaoYuhaoCarl commented Dec 2, 2026 •

Copy link
Copy Markdown

Link to issue

Closes #8464

When VertexAiSessionService outlived 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 awaits client.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 main and 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.
main logged two unhandled cleanup exceptions and the wheel logged zero.

I also issued two list_sessions calls per asyncio.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:

loop 1: sockets=1, GETs=[2], EOF before next loop=True
loop 2: sockets=1, GETs=[2], EOF before next loop=True
loop 3: sockets=1, GETs=[2], EOF before next loop=True
loop-handler errors: 0

Hosts that drive loops manually must call loop.shutdown_asyncgens() before loop.close() for this cleanup to run, as asyncio.run() does.

Unit tests and local checks

  • pytest -q tests/unittests/sessions/ tests/unittests/utils/test_event_loop_cache.py on Python 3.11 passed 1,160 tests, with 26 expected failures.
  • Pre-commit checks on both changed files, mypy on the service module, and uv build passed.
  • The service and loop-cache tests passed on all four supported Python versions (96 per version).
Full tox suite Result
Python 3.11 26,702 passed, 0 failed on the full rerun
Python 3.12 26,693 passed, 0 failed
Python 3.13 26,693 passed, 0 failed
Python 3.14 26,695 passed, 0 failed

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 tox with 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

  • Read the contribution guide and reviewed the diff.
  • Added unit tests and verified the regression against the base commit.
  • Ran the full unit suite on all four Python versions.
  • Built the wheel and verified the reported flow end to end.

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
@google-cla

google-cla Bot commented Dec 2, 2026

Copy link
Copy Markdown

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.

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.

VertexAiSessionService: per-loop client eviction schedules aclose() on the wrong loop ("RuntimeError: Event loop is closed" on Agent Engine)

1 participant