Skip to content

Fix async resource boundaries - #8364

Merged
Git-on-my-level merged 3 commits into
mainfrom
david/zach-async-resource-correctness
Jun 25, 2026
Merged

Git-on-my-level merged 3 commits into
mainfrom
david/zach-async-resource-correctness

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Jun 25, 2026 •

Copy link
Copy Markdown
Member

Summary

This aggregates ZachL111's async/resource correctness fixes into one maintained PR:

  • Offload synchronous Firestore/database reads from async code with utils.executors.run_blocking(..., db_executor, ...) in app integrations, notification memories, and the fair-use classifier.
  • Make the Gmail retrieval path async end-to-end: the tool is async, message list/fetch Google API calls are awaited, and auth refresh goes through a shared retry_on_auth_async helper beside the existing sync helper.
  • Add the missing asyncio import in routers/agent_tools.py and verify changed modules with a static undefined-name check.
  • Ensure speech-profile matching closes the uploaded audio file handle deterministically.
  • Teach scan_async_blockers.py that work wrapped by repository run_blocking is executor-offloaded.
  • Add a focused async/resource regression test module to the backend test runner.

Supersedes #8268, #8274, #8296, #8300, #8314, #8323. Original issue discovery, reproductions, and candidate fixes by @ZachL111.

Deferred: #8265, per the aggregation plan, because it mixes Firestore writes, cache work, and potentially expensive memory/LLM postprocessing and needs a fuller executor design.

Tests

  • uv run --with pytest --with pytest-asyncio --with httpx --with pydub pytest tests/unit/test_async_resource_correctness.py -v
  • uv run python -m py_compile routers/agent_tools.py utils/app_integrations.py utils/llm/notifications.py utils/retrieval/tools/gmail_tools.py utils/retrieval/tools/integration_base.py utils/llm/fair_use_classifier.py utils/stt/speech_profile.py scripts/scan_async_blockers.py tests/unit/test_async_resource_correctness.py
  • uv run python scripts/scan_async_blockers.py --dirs routers utils (exit 0; reports 0 high network I/O / 0 file I/O / 0 no-await endpoints, plus pre-existing async helper/sync DB findings elsewhere)
  • uv run --with pyflakes pyflakes routers/agent_tools.py utils/retrieval/tools/gmail_tools.py tests/unit/test_async_resource_correctness.py

Review in cubic

Supersedes #8268, #8274, #8296, #8300, #8314, #8323.

Original issue discovery, reproductions, and candidate fixes by @ZachL111.

Co-authored-by: ZachL111 <[email protected]>

@cubic-dev-ai cubic-dev-ai 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.

1 issue found across 10 files

Confidence score: 4/5

  • In backend/tests/unit/test_async_resource_correctness.py, test_speech_profile_closes_audio_file_handle leaves utils.stt.speech_profile in sys.modules after _load_module, which can contaminate later tests that import the same module and cause order-dependent/flaky CI failures; add teardown cleanup (e.g., remove the injected module from sys.modules) before merging.

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

@Git-on-my-level Git-on-my-level left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Addressed the unresolved Cubic test-isolation comment.

Changes:

  • test_speech_profile_closes_audio_file_handle now removes the manually loaded utils.stt.speech_profile module from sys.modules in a finally block.
  • Stubbed pydub locally in the test so the focused unit file remains hermetic in a minimal uv run environment.

Verification:

  • ENCRYPTION_SECRET=*** VIRTUAL_ENV= uv run pytest tests/unit/test_async_resource_correctness.py -q → 5 passed

@Git-on-my-level

Copy link
Copy Markdown
Member Author

@codex 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: cd7df4ed91

ℹ️ 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".

Address Codex P2 review comment on get_gmail_messages_tool:
prepare_access() calls users_db.get_integration() (Firestore read)
synchronously on the event loop since the tool became async. Offload
via run_blocking(db_executor, ...) matching the pattern already used
in fair_use_classifier.py, notifications.py, and app_integrations.py.
@Git-on-my-level

Copy link
Copy Markdown
Member Author

Offloaded Gmail access lookup from the event loop (P2 — Codex feedback)

get_gmail_messages_tool now offloads prepare_access() via await run_blocking(db_executor, prepare_access, ...) instead of calling it synchronously on the event loop. This matches the pattern already established in this PR for fair_use_classifier.py, notifications.py, and app_integrations.py.

What changed:

  • backend/utils/retrieval/tools/gmail_tools.py: added from utils.executors import db_executor, run_blocking and wrapped the prepare_access() call with await run_blocking(db_executor, ...).

Verification:

  • test_async_resource_correctness.py — all 5 tests pass (including the gmail tool async interface + refresh test).
  • scan_async_blockers.py — gmail_tools.py no longer flagged (the prepare_access Firestore call is now offloaded).
  • Pre-commit formatting hook passed.

@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: 323adcb193

ℹ️ 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".

@Git-on-my-level
Git-on-my-level merged commit a650a61 into main Jun 25, 2026
4 checks passed
@Git-on-my-level
Git-on-my-level deleted the david/zach-async-resource-correctness branch June 25, 2026 23:44
@Git-on-my-level

Copy link
Copy Markdown
Member Author

Addressed: token persistence is now offloaded to the DB executor.

The sync users_db.set_integration() Firestore write in refresh_google_token was running directly on the event loop. Wrapped it with await run_blocking(db_executor, ...) so it executes in the DB thread pool, consistent with backend async I/O rules.

Verification:

  • New test test_refresh_google_token_offloads_firestore_write asserts the Firestore write goes through run_blocking with db_executor
  • scan_async_blockers.py reports zero blocking calls for refresh_google_token
  • All 6 tests in test_async_resource_correctness.py pass

Git-on-my-level added a commit that referenced this pull request Jun 26, 2026
## Summary

The async `refresh_google_token` coroutine persisted the new access
token via `users_db.set_integration()` — a synchronous Firestore write —
directly on the event loop. When a Gmail request hit an expired token,
the refresh path blocked concurrent chat/tool streaming until Firestore
returned.

Wrap the Firestore write with `await run_blocking(db_executor, ...)` so
it runs in the DB thread pool, consistent with backend async I/O rules
(AGENTS.md: "never call sync DB/storage/file functions directly inside
async def").

This was originally flagged as P2 by Codex review on PR #8364
(`retry_on_auth_async`), but the fix targets `google_utils.py` which was
not part of that PR's diff. Opening as a standalone fix.

## Test Plan

- [x] `scan_async_blockers.py` reports zero blocking calls for
`refresh_google_token`
- [x] Pre-commit formatting passes (black)
- [ ] CI Hermetic Backend E2E passes


<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/BasedHardware/omi/pull/8378?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
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.

2 participants