fix(backend): offload listen WebSocket connect-time blocking reads (#9239)#9393
Open
kodjima33 wants to merge 1 commit into
Open
fix(backend): offload listen WebSocket connect-time blocking reads (#9239)#9393kodjima33 wants to merge 1 commit into
kodjima33 wants to merge 1 commit into
Conversation
…9239) _stream_handler read the user's private-cloud-sync preference (user_db.get_user_private_cloud_sync_enabled, a synchronous Firestore call) and speech-profile presence (get_user_has_speech_profile, a synchronous GCS blob existence check) directly on the event loop at connect time. Per backend/AGENTS.md, sync DB/storage functions in an async def must be offloaded via run_blocking, or a slow read blocks the event loop for every other live connection. Route both through awaited run_blocking(db_executor/storage_executor, ...). Adds an AST structural regression test asserting neither is called directly and both are offloaded to the correct pool. fixes #9239
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The
/listenWebSocket handler (_stream_handlerinbackend/routers/transcribe.py) performed two blocking reads directly on the event loop at connect time:user_db.get_user_private_cloud_sync_enabled(uid)— a synchronous Firestore.get()(database/users.py)get_user_has_speech_profile(uid)— a synchronous GCS blob.exists()check (utils/other/storage.py)Both are now routed through an awaited
run_blocking(...)on the appropriate pool (db_executorfor Firestore,storage_executorfor GCS).Why
From the issue (#9239):
backend/AGENTS.mdis explicit: "Never call sync DB/storage/file functions directly insideasync def— wrap withawait run_blocking(executor, func, args)." A slow Firestore/GCS read on the connect path blocks the event loop for every other live connection on that worker, not just the connecting user. The rest of the handler already follows this pattern (e.g.get_user_speaker_embedding,get_profile_audio_if_exists); these two connect-time reads were missed.What I verified
backend/tests/unit/test_transcribe_connect_offload_async.py— an AST structural test (same pattern as the existingtest_phone_twiml_async.py) asserting neither read is called directly and both are offloaded to the correct executor pool.python3 -m py_compile routers/transcribe.py— OK.tests/unit/test_transcribe_decisions.py+test_transcribe_connect_offload_async.py— pass. (Two unrelated pre-existing failures intest_listen_session_bootstrap.pyreproduce on cleanorigin/mainand are untouched by this change.)Behavior is unchanged for users — the same values are computed; they are now read off the event loop.
Auto-generated from issue feedback by the mini issues-improver. Review before merge.