Skip to content

test(google): disarm GenAI client finalizers to prevent task leak across unit tests (#6881) - #7247

Open
rrfunde wants to merge 2 commits into
livekit:mainfrom
rrfunde:fix/google-genai-finalizer-leak
Open

test(google): disarm GenAI client finalizers to prevent task leak across unit tests (#6881)#7247
rrfunde wants to merge 2 commits into
livekit:mainfrom
rrfunde:fix/google-genai-finalizer-leak

Conversation

@rrfunde

@rrfunde rrfunde commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Disarm google.genai.client.AsyncClient.__del__ and BaseApiClient.__del__ in tests/test_plugin_google_realtime.py so that asynchronous client finalizers do not schedule aclose() tasks on subsequent unrelated tests' event loops during GC.
  • Add gc.collect() and task drain in _make_session and _make_configured_session.
  • Fix flag parsing in tests/conftest.py so boolean category flags (like --unit) do not swallow subsequent test file arguments.

Fixes #6881

@rrfunde
rrfunde requested a review from a team as a code owner September 12, 2026 11:12
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 12, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-12T12:04:54.731429Z 695dcf7 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Devin Review: 2 flags

Not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0ba8d8dfb1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tests/test_plugin_google_realtime.py Outdated


# Disarm finalizers on classes as well to prevent aclose() tasks scheduling on active event loops
AsyncClient.__del__ = lambda self: None # type: ignore[assignment]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Scope the SDK finalizer patch to individual tests

When this module is collected alongside other Google tests, this assignment runs during collection and is never restored, so every google.genai.AsyncClient created anywhere in that pytest process loses its normal finalizer. Unfiltered or combined-category runs can consequently leak clients from unrelated tests and exercise different lifecycle behavior depending on whether this module was collected; use a fixture/monkeypatch with teardown or limit the change to the session instances.

Useful? React with 👍 / 👎.

Comment thread tests/conftest.py Outdated
Comment on lines +327 to +331
if coro_name.endswith(".aclose"):
frame = getattr(coro, "cr_frame", None)
module = frame.f_globals.get("__name__", "") if frame else ""
if module.startswith("google.genai"):
return True

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep GenAI cleanup tasks visible to the leak checker

If code under test forgets to close a GenAI client and its finalizer schedules a pending aclose(), this branch now classifies that actual cleanup task as ignorable, so fail_on_leaked_tasks passes instead of detecting the regression. The module-specific fixture can drain known finalizer tasks, but the global checker should not suppress every google.genai aclose() created by any test.

Useful? React with 👍 / 👎.

@rrfunde

rrfunde commented Sep 12, 2026

Copy link
Copy Markdown
Contributor Author

Addressed feedback: reverted the global GenAI check in conftest.py and scoped finalizer disarming to an autouse pytest fixture using monkeypatch in test_plugin_google_realtime.py so that other tests and global leak checks are completely unaffected.

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.

Flaky unit-tests: a genai client finalizer lands in an unrelated test's leak check

1 participant