Skip to content

fix(backend): offload chat quota release off the event loop (#15966) - #16028

Merged
kodjima33 merged 1 commit into
mainfrom
watchdog/issue-15966-chat-quota-release-event-loop
Sep 22, 2026
Merged

kodjima33 merged 1 commit into
mainfrom
watchdog/issue-15966-chat-quota-release-event-loop

Conversation

@kodjima33

@kodjima33 kodjima33 commented Sep 22, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • _release_chat_quota_question_best_effort in backend/routers/chat.py ran a synchronous Firestore transaction (llm_usage_db.release_chat_quota_question) directly on the event loop, from the async def generate_stream() generator behind /v2/messages. It fires on the terminal-failure path (provider error, empty answer) — i.e. exactly when a provider is already failing and the pod is under load — and pins the whole worker's event loop for the transaction's duration, stalling every other concurrent request on it.
  • Root cause: the helper was a plain def calling the sync Firestore client directly, unlike its sibling _record_chat_quota_question_best_effort path and every other Firestore/Redis call in the same function (chat_tz, process_message, is_trial_paywalled, resolve_voice_message_language, ...), which already go through await run_blocking(db_executor, ...).
  • Fix: made _release_chat_quota_question_best_effort async def and offloaded the call via await run_blocking(db_executor, llm_usage_db.release_chat_quota_question, uid, idempotency_key), matching the established pattern in this exact file (backend/AGENTS.md "Async I/O (3-Lane Architecture)" — Lane 2 executors, db_executor for Firestore/Redis CRUD). Updated both call sites to await it.
  • Same failure/fix shape as prior merged PRs fix(backend): offload the pusher webhook lookup off the event loop #15291, fix(backend): offload the sync billing and fair-use gates off the event loop #15228, fix(backend): offload the chat answer persistence off the event loop #15273 in this same router file.

Testing

  • black --line-length 120 --skip-string-normalization --check backend/routers/chat.py — passes.
  • python backend/scripts/scan_async_blockers.py --dirs routers utils — 0 findings.
  • pytest backend/tests/unit/test_chat_quota_release.py backend/tests/unit/test_chat_quota_offload.py backend/tests/unit/test_chat_quota_counting_router.py backend/tests/unit/test_chat_quota.py — 35 passed, including test_v2_messages_records_failure_when_the_production_stream_errors, which exercises the exact except Exception release path this change touches.

Failure-Class: none

🤖 automated by hourly watchdog — tested and merged

Review in cubic

_release_chat_quota_question_best_effort ran a synchronous Firestore
transaction (release_chat_quota_question) directly on the event loop
from an async generator (generate_stream in /v2/messages), blocking
all concurrent requests on that worker for the transaction's duration.
It fires exactly when a provider call already failed, i.e. under load.

Route it through await run_blocking(db_executor, ...), matching every
other Firestore/Redis call in this same function (db_executor is
already used at lines 624, 638, 968, 1025, 1055, ...).
@kodjima33
kodjima33 merged commit 3c5fd43 into main Sep 22, 2026
63 checks passed
@kodjima33
kodjima33 deleted the watchdog/issue-15966-chat-quota-release-event-loop branch September 22, 2026 22:17
Git-on-my-level added a commit that referenced this pull request Oct 1, 2026
…ponses (#20168)

What: `POST /v1/oauth/token` reflected raw Firebase verification
exceptions (`InvalidIdTokenError` details, cert fetch URLs, key IDs,
claim state) directly into HTTP 401 response bodies. Unauthenticated
surface: any caller can submit a malformed token and read internal
verification traces.
Why: Internal error-detail leakage on an auth endpoint; real (if minor)
security hygiene issue, not template hardening against impossible
inputs.
Risk: Low — error *bodies* become static strings; status codes
unchanged; detail preserved in server logs.
Solution: Log exception detail server-side (`logger.warning` /
`logger.error`), return static 401 messages. Regression tests assert the
leak strings no longer appear in the raised HTTPException.

Cherry-picked from #16029 by @Mateidslx (code + regression tests as
authored; verified the leak still exists on current `main` before
re-landing). Original bounty proposal #16028 is declined per the
maintainer's standing bounty-policy ruling; the fix lands here with
credit.

Verification evidence:
- `backend/.venv/bin/python -m pytest
tests/unit/test_oauth_firebase_error_sanitization.py` — 2 passed (both
tests fail on unpatched `main` code, pass with this diff)
- `scripts/pr-preflight --pr-body-file` passes with the citations below

Invariants: INV-DATA-1
Failure-Class: none

---
*by AI on behalf of David — if you need David's attention urgently,
please @Git-on-my-level and escalate with `need human response`.*


<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/BasedHardware/omi/pull/20168?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. -->
rahul24 pushed a commit to rahul24/milo that referenced this pull request Oct 1, 2026
…ponses

The oauth_token exchange endpoint reflected raw Firebase verification
exceptions (InvalidIdTokenError details, cert fetch URLs, key IDs,
claim state) directly into HTTP 401 response bodies. Moves the detail
to server-side logging (warning/error) and returns static messages.

Cherry-picked from BasedHardware#16029 by @Mateidslx (code + regression tests as
authored there; verified against current main — the leak is still
present). Original bounty proposal BasedHardware#16028 declined; this lands the fix
under the maintainer account instead.

Verification: backend/.venv pytest tests/unit/test_oauth_firebase_error_sanitization.py — 2 passed.

Failure-Class: none
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.

1 participant