Repository navigation
Conversation
There was a problem hiding this comment.
1 issue found across 3 files
Confidence score: 3/5
- In
backend/utils/social.py, routing memory extraction throughdb_executormixes LLM/post-processing with DB-bound work, which can exhaust DB worker threads under load and delay Firestore/Redis operations for users; move this path to a non-DB executor (or split DB vs compute steps) before merging.
Reply with feedback, questions, or to request a fix.
Fix all with cubic | Re-trigger cubic
kodjima33
left a comment
There was a problem hiding this comment.
Backend hardening bug fix with regression test; matches established pattern. (auto-review)
a1cf9c7 to
82d701f
Compare
|
Hi @ZachL111 — thank you for finding this and writing up a candidate fix. We reviewed it during the ZachL111 aggregate triage. This one needs a bit more product/API/design decision than we want to bundle into the current aggregate PRs (#8362, #8364, #8366, #8367), so we’re deferring rather than merging this exact patch. I’m closing the individual PR to keep the review queue manageable. The report is still appreciated and may be revisited as a more explicit follow-up. Thanks again for the contribution. |
|
Closing after aggregate triage: deferring this design/API decision rather than merging the one-off patch. Thanks @ZachL111 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. -->
Summary
upsert_persona_from_twitter_profileinbackend/utils/social.pyis anasync defthat runs two sync Firestore-backed calls,upsert_app_to_dbandcreate_memories_from_twitter_tweets, directly on the event loop. The Firestore sync SDK is blocking, so each call stalls the whole loop for the duration of the writes, which freezes health checks and every other connection on the pod while a persona is being created or refreshed. This PR offloads both calls todb_executorviarun_blockingso they run on a thread pool instead of the loop. This is a proactive hardening change; there is no filed GitHub issue for it.Root cause
In
backend/utils/social.py,upsert_persona_from_twitter_profilecalls the blocking functions inline:upsert_app_to_dbis adatabase.*function backed by the sync Firestore SDK, andcreate_memories_from_twitter_tweetsdoes Firestore writes underneath. The backend async rules require offloading blocking work inasync defwithawait run_blocking(executor, fn, args); calling them directly holds the event loop for the full round trip to Firestore, blocking unrelated requests and health checks on the same worker. The two functions are awaited sequentially elsewhere, but here they ran on the loop.Fix
Import
db_executorandrun_blockingfromutils.executorsand dispatch both blocking calls through thedb_executorpool, which is the pool for Firestore and Redis CRUD:Both calls still run exactly once and in the same order, so behavior for valid input is unchanged; they just execute on a worker thread instead of the loop.
Testing
New
backend/tests/unit/test_social_persona_async.py(registered intest.sh).utils.socialpulls in a heavy import graph, so the test keeps the realutils,utils.executors, andutils.socialand stubs only the heavy leaves (database,firebase_admin,models,utils.llm,utils.conversations, and similar) through a meta-path finder, then callsupsert_persona_from_twitter_profiledirectly with the surrounding helpers patched. One case patchesrun_blockingto record the function it is handed and asserts bothupsert_app_to_dbandcreate_memories_from_twitter_tweetsare dispatched through it againstdb_executor; the other makesrun_blockinga no-op and asserts neither blocking function runs inline, which fails on current main where they are called directly. Verified red to green: the test fails on current main and passes with this change.PR status check
No open PR changes the logic this PR fixes. The offload wiring added here does not appear anywhere in this file's history, so it is not a previously reverted fix being resubmitted. No filed GitHub issue matches.