Repository navigation
Description
Summary
PR #7343 (executor bulkhead architecture) introduced a latency regression on the /v2/sync-local-files 202 accept path. The 202 response went from 0.1s P50 to 158.9s P50, causing Cloud Run instance spike (21.8/25 max) and new 429 rate-limiting errors.
Production Evidence
Pre-deploy (01:30–02:30 UTC):
- 202 responses: n=461, avg=0.2s, P50=0.1s, max=3.7s
- 504s: 30, 429s: 0
Post-deploy alert window (05:00–05:40 UTC):
- 202 responses: n=380, avg=188.6s, P50=158.9s, max=562.1s
- 504s: 21, 429s: 76 (NEW — did not exist pre-deploy)
Hourly request volume comparable (1200–2200/h). This is latency-driven, not traffic-driven.
Root Cause
_run_full_pipeline_background was moved from the default asyncio executor (unbounded) to postprocess_executor (8 workers):
# BEFORE (worked — unbounded pool):
loop_v2.run_in_executor(None, _run_full_pipeline_background, ...)
# AFTER (PR #7343 — 8-worker pool):
submit_with_context(postprocess_executor, _run_full_pipeline_background, ...)Starvation cascade
-
Each background pipeline holds 1
postprocess_executorslot for the full VAD→STT→LLM duration (60–300s). It callsfuture.result(timeout=300)onsync_executorfutures inside (lines 1418, 1522 ofrouters/sync.py). -
With Cloud Run concurrency=12 per instance, burst traffic fills all 8
postprocess_executorslots quickly. -
_precache_all_paralleland_cache_uncached_parallel(lines 216, 315) also run onpostprocess_executor— they compete with background pipelines for the same 8 slots. -
When
postprocess_executoris full, precache coordinators queue, which holdsstorage_executorslots longer when they eventually run. -
The v2 fast path has 4 sequential
await run_blocking()calls before returning 202:await run_blocking(critical_executor, is_hard_restricted, uid)— line 1613await run_blocking(critical_executor, has_transcription_credits, uid)— line 1616await run_blocking(storage_executor, _retrieve_file_paths_v2, ...)— line 1633await run_blocking(db_executor, create_sync_job, ...)— line 1636
-
Under pool saturation, these awaits queue behind other work on the same pools, compounding latency on the 202 response.
-
Requests hold Cloud Run instance slots for ~159s instead of 0.1s → at concurrency=12, only ~4.5 req/min throughput per instance → Cloud Run returns 429.
Why submit_with_context alone doesn't explain the blocking
submit_with_context(postprocess_executor, ...) is fire-and-forget (returns Future immediately). The 202 response is NOT blocked by the background dispatch itself. It's blocked by the cascading saturation of storage_executor and critical_executor from the overloaded postprocess_executor coordinators.
Service Config Context
| Parameter | Value |
|---|---|
| Cloud Run concurrency | 12 per instance |
| Max instances | 25 |
postprocess_executor |
8 workers |
sync_executor |
12 workers |
storage_executor |
16 workers |
critical_executor |
8 workers |
Fix Options
Option A — Immediate hotfix (recommended):
Revert _run_full_pipeline_background to default executor (None). The pre-deploy code intentionally used the unbounded default pool with a comment documenting the deadlock avoidance reason. One-line change in routers/sync.py line 1649.
# Revert to:
loop_v2 = asyncio.get_running_loop()
loop_v2.run_in_executor(None, _run_full_pipeline_background, ...)Option B — Follow-up proper fix:
Create a dedicated sync_coordinator_executor with 24+ workers, sized for the actual concurrent pipeline count in production. Preserves bulkhead isolation.
Files
backend/routers/sync.py— lines 1648–1660 (the problematic change)backend/utils/executors.py— pool definitions
Related
- PR fix(backend): eliminate async event-loop blockers in FastAPI endpoints #7343 (executor bulkhead architecture)
- Deployed as revision
backend-sync-00596-2c5at 02:58 UTC
Activity
fix(backend): async coordinator pattern for sync v2 pipeline (BasedHa…