Repository navigation
backend: lower precache concurrency cap 4 → 2 to free storage_executor for sync - #7526
Conversation
…sync The storage_executor pool (96 workers) is shared between the sync v2 pipeline and the playback/precache flows. Under load, precache fan-out can pin up to ~36 workers per instance (4 outer slots × up to 9 each for chunk downloads), starving the sync pipeline's GCS reads. Halving the per-process precache concurrency cap halves precache's worst-case storage footprint to ~18 workers per instance, leaving the sync pipeline more headroom on the shared pool. The cost is slower speculative cache warming (one-off ~few-second hit when a user opens a brand-new conversation before warming completes); on-demand /urls playback path is unchanged.
Greptile SummaryHalves
Confidence Score: 5/5Safe to merge — the change is a single constant reduction with no logic alterations and no new code paths. The semaphore value is the only thing that changed. The acquire/release pattern in No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[precache_conversation_audio] -->|postprocess_executor.submit| B[_precache_all]
B -->|_PRECACHE_FILE_SEM.acquire max=2 was 4| C[storage_executor.submit _cache_single]
C --> D[get_or_create_merged_audio]
D -->|cache hit| E[return cached WAV bytes]
D -->|cache miss| F[download_audio_chunks_and_merge]
F -->|STORAGE_CHUNK_SEM x8 in-flight per call| G[storage_executor chunk downloads]
G --> H[GCS download + decode]
C -->|done_callback: _PRECACHE_FILE_SEM.release| B
Reviews (1): Last reviewed commit: "backend: lower _PRECACHE_FILE_SEM 4 → 2 ..." | Re-trigger Greptile |
…gle janitor thread (#7855) ## Problem Four call sites (1 in `routers/sync.py`, 3 voice-message flows in `utils/chat.py`) delete temporal GCS blobs by parking a `storage_executor` thread in `time.sleep(480)` per file. At current sync volume (~20 jobs/min post-#7801) that keeps **~90 of the pool's 128 threads asleep as ad-hoc timers** — confirmed in prod `executor_pool_health` logs (~70–80% \"utilization\" with queue_depth 0, i.e. occupancy without work). This is the remaining root cause behind #7531's storage-pool saturation: the pool was bumped 32→64→96→128 in ten days and `_PRECACHE_FILE_SEM` was halved (#7526) largely to feed threads whose only job was waiting. ## Change - **New `utils/other/deferred_delete.py`**: `DeferredDeleter` — a due-time min-heap plus one lazily-started daemon thread. `schedule()` is an O(log n) heap push; the janitor wakes when the next deletion is due. An earlier-due schedule arriving mid-wait re-notifies and re-peeks, so ordering holds. Delete failures are logged and skipped (the syncing bucket's lifecycle rule remains the backstop, exactly as it was for the sleeping threads). - **`storage.py`**: `schedule_syncing_temporal_file_deletion(path, delay=480s)` wraps a module-level janitor bound to `delete_syncing_temporal_file`. Same 480s semantics (under the 15-min signed-URL validity). - **All four call sites** switch to the scheduler; `chat.py` drops its now-unused `storage_executor`/`time` imports. - **`_PRECACHE_FILE_SEM` 2 → 4**, reverting #7526's load-shed now that the sleepers are gone — audio merge/precache get the freed headroom. Deletion timing, crash semantics (pending deletions die with the process either way), and the lifecycle backstop are all unchanged. Hundreds of pending deletions now cost one thread instead of hundreds. ## Expected impact (measurable immediately after deploy) `executor_pool_health` storage `active_count` should drop from ~90–100 to real work only (~5–15). That single number is the before/after check. ## Tests - New `tests/unit/test_deferred_blob_janitor.py` (11 tests): real-module behavioral coverage (due-order with out-of-order schedules, near-term schedule interrupting a long wait, failure doesn't kill the janitor, 200 pending = exactly 1 thread) + structural guards (no `time.sleep(480)` remains, all four sites use the scheduler, sem = 4). Registered in `test.sh`. - `test_storage_fanout_limits.py`: semaphore value assertion updated 2 → 4 with rationale. - `test_sync_silent_failure.py`: removed the executor-swap setup/teardown machinery that existed solely to neutralize the old sleeping deleters in tests; all 41 pass. - `scan_async_blockers.py`: no new findings (the one `chat.py` hit pre-exists on main). - `test_sync_v2.py` remains at the main baseline (5 pre-existing failures from #7819, unrelated). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
…estore precache sem to 4 The 4→2 cut (BasedHardware#7526) was load-shedding while the pool was full of sleeping deletion timers; with the janitor holding those, precache gets its concurrency back.
Summary
_PRECACHE_FILE_SEMfrom 4 → 2 inbackend/utils/other/storage.py.Why
The
storage_executor(96 workers) is shared between the sync v2 pipeline and the playback / precache flows. Under load, precache fan-out can pin up to ~36 workers per instance (4 outer slots × up to 9 each for chunk downloads), starving the sync pipeline's GCS reads.Recent prod data (48h, all services):
caller=process_conversation: 7,914 events (37% of audio_merge work)caller=precache_endpoint: 8,430 events (40%)caller=sync_urls_first: 3,562 events (17%)caller=sync_urls_bg: 1,285 events (6%)Storage pool saturation warnings on
backend-syncwere averaging 96% utilization with peak queue depth 67. Sync's decode_ms p95 was sitting at 252s, largely queue wait.Halving the cap drops precache's worst-case storage footprint to ~18 workers per instance, leaving ~78 free for sync.
Tradeoff
Speculative cache warming takes longer. Users see a one-off slowdown only if they open a brand-new conversation before warming finishes; on-demand
/v1/sync/audio/{conv}/urlsplayback is unchanged.Test plan
executor_pool_healthwarnings onbackend-syncfor 30 min post-deploy —storagepool max_q should drop noticeablysync_v2 bg completedecode_ms p95 — should improve toward p50/v2/sync-local-filesor/v1/sync/audio/*/precache