fix(tests): settle genai client finalizers in the test that owns them - #6882
Open
helin-mktech wants to merge 2 commits into
Open
fix(tests): settle genai client finalizers in the test that owns them#6882helin-mktech wants to merge 2 commits into
helin-mktech wants to merge 2 commits into
Conversation
AsyncClient.__del__ schedules aclose() on whatever event loop is running when the collector reaches it, with no check for a client that was already closed explicitly. Collected late, those tasks surface as leaked tasks in an unrelated test in a later module. Collect and drain them while the test that built the client still owns the loop, so they cannot escape into someone else's leak check. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
helin-mktech
force-pushed
the
fix/genai-finalizer-leak
branch
from
August 17, 2026 06:45
42c04a1 to
db4c1d5
Compare
longcw
approved these changes
Aug 17, 2026
| if pending := [ | ||
| task | ||
| for task in asyncio.all_tasks() | ||
| if not task.done() and "aclose" in (getattr(task.get_coro(), "__qualname__", "") or "") |
Contributor
There was a problem hiding this comment.
maybe also check if it's from genai
and (getattr(task.get_coro(), "__module__", "") or "").startswith("google.genai")
Author
There was a problem hiding this comment.
Good catch — aclose on its own is too loose. Done, keyed on the coroutine's defining module now.
One correction on the mechanics: coroutine objects don't carry module at all, so getattr(coro, "module", "") is always '' and that check would never match — the fixture would have gone quietly inert.
coro.cr_frame.f_globals["name"] gives the module.
helin-mktech
force-pushed
the
fix/genai-finalizer-leak
branch
from
August 17, 2026 09:00
8c6a874 to
d321395
Compare
`aclose` is a common method name, so matching on it alone could pull in an unrelated pending task. Key on the coroutine's defining module instead. Reading the module off the coroutine takes cr_frame.f_globals rather than __module__, which coroutine objects do not carry at all. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
helin-mktech
force-pushed
the
fix/genai-finalizer-leak
branch
from
August 17, 2026 09:35
d321395 to
26f55db
Compare
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.
Fixes the flaky
Test leaked tasksfailure described in #6881.AsyncClient.__del__schedulesaclose()on whatever event loop is running when the collector reaches it, even for a client the test already closed. Collected late, those tasks surface as leaked tasks in an unrelated test in a later module — usuallytests/test_plugin_openai_stt_context.py, which never touches google.This adds an autouse fixture to the one module that builds genai clients, so the finalizers run and finish while that test still owns the loop.
Verified on the CPython build CI uses (3.12.13), full unit suite:
unit-testsReverting the fixture brings the failure straight back.
🤖 Generated with Claude Code