Repository navigation
Conversation
There was a problem hiding this comment.
1 issue found across 3 files
Confidence score: 5/5
- In
backend/tests/unit/test_fair_use_classifier_async_offload.py, both tests patch module-level_classifier_llm.ainvokewithout teardown, so test behavior can leak across cases if order changes and cause flaky or misleading unit results; this is low runtime risk but can hide regressions in CI—restore the originalainvokein teardown (or use scoped monkeypatch fixtures) before merging.
Reply with feedback, questions, or to request a fix.
Fix all with cubic | Re-trigger cubic
…not leak across tests
|
Hi @ZachL111 — thank you for finding and writing up this async/resource correctness issue. We consolidated this set into a smaller aggregate PR so the fixes share infrastructure and are easier to maintain/review: #8364. Your original discovery, reproduction details, and candidate fix are credited in the new PR body and commit metadata. I’m closing this individual PR as superseded by #8364, not because the issue is invalid. Thanks again for surfacing it. |
|
Hey @ZachL111 👋 Thank you so much for taking the time to contribute to Omi! We truly appreciate you putting in the effort to submit this pull request. After careful review, we've decided not to merge this particular PR. Please don't take this personally — we genuinely try to merge as many contributions as possible, but sometimes we have to make tough calls based on:
Your contribution is still valuable to us, and we'd love to see you contribute again in the future! If you'd like feedback on how to improve this PR or want to discuss alternative approaches, please don't hesitate to reach out. Thank you for being part of the Omi community! 💜 |
## 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` <!-- This is an auto-generated description by cubic. --> <a href="https://cubic.dev/pr/BasedHardware/omi/pull/8364?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. -->
Supersedes BasedHardware#8268, BasedHardware#8274, BasedHardware#8296, BasedHardware#8300, BasedHardware#8314, BasedHardware#8323. Original issue discovery, reproductions, and candidate fixes by @ZachL111. Co-authored-by: ZachL111 <[email protected]>
Summary
classify_user_purposein the fair-use classifier is anasync def, but it calls_prepare_conversation_summaries(uid)directly, and that function runs the synchronous Firestore SDK (database.conversations.get_conversations). A blocking Firestore read inside an async coroutine blocks the event loop for the whole process while the query is in flight, so every other concurrent request, health check, and background task stalls until it returns. This PR offloads that read onto thedb_executorthread pool withrun_blocking, so the coroutine yields while Firestore is queried instead of holding the loop. This is a proactive hardening change; there is no filed GitHub issue for it.Root cause
In
backend/utils/llm/fair_use_classifier.py,classify_user_purposefetches the conversation summaries inline:_prepare_conversation_summariesis a plaindefthat callsconversations_db.get_conversations(...), which is the synchronous Firestore client. When a sync Firestore call runs straight inside anasync def, it does not raise; it just sits on the event loop thread and blocks it for the duration of the network round trip. While that classifier runs, no other coroutine on the same loop can make progress, which is exactly the failure mode the backend async rules call out fordatabase.*functions. The classifier fires after a user crosses the speech-hour soft cap, so under load this is on a hot path where a stalled loop has wide blast radius.Fix
Import
db_executorandrun_blockingfromutils.executorsand offload the blocking read so the coroutine awaits it on the thread pool instead of calling it inline:run_blockingruns_prepare_conversation_summaries(uid)ondb_executor(the pool reserved for Firestore and Redis CRUD) and returns its result, so the summaries and the rest of the classification logic are unchanged. For valid input the output is identical; the only difference is that the Firestore query no longer runs on the event loop thread.Testing
Added
backend/tests/unit/test_fair_use_classifier_async_offload.py, registered inbackend/test.sh. The classifier pulls in heavy dependencies (langchain, the LLM clients, Firestore), so the test stubs those modules before importingutils.llm.fair_use_classifier, then callsclassify_user_purposedirectly withrun_blockingand_prepare_conversation_summariespatched viapatch.object. One case asserts the read goes throughrun_blockingexactly once, ontodb_executor, wrapping_prepare_conversation_summarieswith the uid passed through, and that the offloaded summaries flow end to end into the result. A second case feeds empty summaries through the offloaded path and confirms the classifier short-circuits to the default result without ever awaiting the LLM, proving the offloaded value is the one actually consumed. Verified red to green: the test fails on current main and passes with this change.PR status check
No open PR changes this logic. The offload of
_prepare_conversation_summariesthroughrun_blockingdoes not appear anywhere in this file's history, so this is not a previously reverted fix being resubmitted. No filed GitHub issue matches.