Skip to content

The chat question refund runs its Firestore transaction on the event loop #15966

Closed

Description

@singhharsh1708

The refund added in #15304 runs its Firestore transaction on the event loop, and it fires exactly when a provider is already failing.

_release_chat_quota_question_best_effort (backend/routers/chat.py:371) is a plain def:

def _release_chat_quota_question_best_effort(
    uid: str,
    *,
    idempotency_key: str,
) -> None:
    try:
        llm_usage_db.release_chat_quota_question(uid, idempotency_key)

release_chat_quota_question (backend/database/llm_usage.py:375) is synchronous and, per its own docstring, marks the event doc released "in the same transaction as the decrement", so it is a Firestore transaction, not a single write.

Both callers are inside generate_stream (backend/routers/chat.py:728 and :744), the async generator handed to StreamingResponse, so its body runs on the event loop rather than in FastAPI's threadpool. Line 744 is in the except Exception: arm, so the transaction also runs while a failure is propagating.

The timing is what makes this worth fixing. A release only happens when a turn fails terminally, so these calls do not arrive one at a time; they arrive in a burst when a provider is degraded and many turns fail at once. That is the moment the loop can least afford a stall, and every other request and WebSocket on the pod waits behind them.

The same generator already has both halves of the correct shape: it offloads with await run_blocking(db_executor, notification_db.sync_user_time_zone_from_client, ...) at line 623, and it awaits a yielded helper at yield await emit_done_frame(response).

Proposing this as a paid bounty ($50) per the contribution docs, @kodjima33.

Activity

  1. moved this from Someday to In progress in omi TODO / bountieson Sep 22, 2026
  2. kodjima33 commented on Sep 23, 2026

    @kodjima33
    Collaborator

    Fixed in #16028 (merged as 3c5fd43) — the chat quota release now runs via run_blocking(db_executor, ...) instead of inline on the event loop. Closing as resolved.

  3. moved this from In progress to In review in omi TODO / bountieson Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions