Skip to main content

max / makenotwork

server: route idempotency cache write through bounded pool (ultra-fuzz Run #1 Perf MODERATE) The idempotency middleware ran on every successful write request and persisted the cached response via a raw tokio::spawn, bypassing the bounded state.bg queue that background.rs exists to enforce. Under a write burst those tasks accumulated unthrottled, each grabbing a connection from the 25-slot pool that also serves request handlers. Route it through state.bg.spawn so it shares the queue + semaphore. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Author: Max Johnson <me@maxj.phd> · 2026-06-19 21:10 UTC
Commit: 8669659e22d6f387b61a3de8d7e080785157f129
Parent: 339512f
1 file changed, +6 insertions, -1 deletion
@@ -400,7 +400,12 @@ pub async fn idempotency_middleware(
400 400 // cached response — subsequent requests should hit the DB and get
401 401 // the cached body, not keep skipping via the stale negative.
402 402 neg_cache.remove(&neg_key);
403 - tokio::spawn(async move {
403 + // Route through the bounded background pool, not a raw `tokio::spawn`:
404 + // this runs on every successful idempotent write, and an ungated
405 + // spawn would let a write burst accumulate tasks each grabbing a DB
406 + // pool connection — the exact contention `background.rs` exists to
407 + // bound (ultra-fuzz Run #1 Perf MODERATE).
408 + state.bg.spawn("idempotency_store", async move {
404 409 if let Err(e) = crate::db::idempotency::store_response(
405 410 &db, &key, user_id, &method, &path, status_code, &body_owned,
406 411 ).await {