Skip to content

fix(backend-sync): 202 accept path regression from executor bulkhead PR #7343 #7361

Description

@beastoin

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

  1. Each background pipeline holds 1 postprocess_executor slot for the full VAD→STT→LLM duration (60–300s). It calls future.result(timeout=300) on sync_executor futures inside (lines 1418, 1522 of routers/sync.py).

  2. With Cloud Run concurrency=12 per instance, burst traffic fills all 8 postprocess_executor slots quickly.

  3. _precache_all_parallel and _cache_uncached_parallel (lines 216, 315) also run on postprocess_executor — they compete with background pipelines for the same 8 slots.

  4. When postprocess_executor is full, precache coordinators queue, which holds storage_executor slots longer when they eventually run.

  5. The v2 fast path has 4 sequential await run_blocking() calls before returning 202:

    • await run_blocking(critical_executor, is_hard_restricted, uid) — line 1613
    • await run_blocking(critical_executor, has_transcription_credits, uid) — line 1616
    • await run_blocking(storage_executor, _retrieve_file_paths_v2, ...) — line 1633
    • await run_blocking(db_executor, create_sync_job, ...) — line 1636
  6. Under pool saturation, these awaits queue behind other work on the same pools, compounding latency on the 202 response.

  7. 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

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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions