From 870144a7cc0f3bc38ccb07f275b02cc69644537a Mon Sep 17 00:00:00 2001 From: Chris Edwards <279314+ckedwards@users.noreply.github.com> Date: Thu, 12 Nov 2026 13:14:03 -0500 Subject: [PATCH] fix(rivetkit-core): keep one sleep rearm task per actor Every finished HTTP request spawned a task that waits for the actor's in-flight requests to reach zero before re-arming the sleep timer. Under steady concurrent load the count rarely reaches zero, so these tasks piled up, one per request, and every idle notification (from any counter wired to idle_notify) woke all of them to re-check and wait again. CPU per call grew with the backlog: 0.7 ms with one request in flight, 18 to 43 ms with 4 to 64, and worse the longer the load lasted. A request now spawns the waiter only if none is pending: an AtomicBool on SleepState is claimed before spawning and released when the waiter returns, just before it resets the sleep timer, so a request finishing in that gap is covered by the reset that follows. With the change, a call costs 0.7 ms of CPU at every concurrency (1 to 64 in flight, held for 60 s). Fixes #5991 --- .../packages/rivetkit-core/src/actor/sleep.rs | 20 +++++++++++++++++++ .../rivetkit-core/src/registry/http.rs | 5 +++++ 2 files changed, 25 insertions(+) diff --git a/rivetkit-rust/packages/rivetkit-core/src/actor/sleep.rs b/rivetkit-rust/packages/rivetkit-core/src/actor/sleep.rs index 08cafde6f2..cb9e2294c0 100644 --- a/rivetkit-rust/packages/rivetkit-core/src/actor/sleep.rs +++ b/rivetkit-rust/packages/rivetkit-core/src/actor/sleep.rs @@ -46,6 +46,9 @@ pub(crate) struct SleepState { // than a destroy. pub(super) stop_error: Mutex>, pub(super) lifecycle_started: AtomicBool, + // Whether a task is already waiting for HTTP requests to go idle to re-arm the sleep timer: one is + // enough, and one per request piles up under steady load, each woken by every idle notification. + pub(super) http_idle_rearm_pending: AtomicBool, pub(super) run_handler_active_count: AtomicUsize, // Forced-sync: the compatibility sleep timer is aborted from sync paths. pub(super) sleep_timer: Mutex>>, @@ -75,6 +78,7 @@ impl SleepState { http_request_counter: Mutex::new(None), stop_error: Mutex::new(None), lifecycle_started: AtomicBool::new(false), + http_idle_rearm_pending: AtomicBool::new(false), run_handler_active_count: AtomicUsize::new(0), sleep_timer: Mutex::new(None), work: WorkRegistry::new(), @@ -385,6 +389,22 @@ impl ActorContext { counter.wait_zero(deadline).await } + /// Claims the one task that re-arms the sleep timer once HTTP requests go idle: false if one is waiting. + pub(crate) fn claim_http_idle_rearm(&self) -> bool { + !self + .0 + .sleep + .http_idle_rearm_pending + .swap(true, Ordering::AcqRel) + } + + pub(crate) fn release_http_idle_rearm(&self) { + self.0 + .sleep + .http_idle_rearm_pending + .store(false, Ordering::Release); + } + pub(crate) async fn wait_for_http_requests_idle(&self) { loop { let idle = self.0.sleep.work.idle_notify.notified(); diff --git a/rivetkit-rust/packages/rivetkit-core/src/registry/http.rs b/rivetkit-rust/packages/rivetkit-core/src/registry/http.rs index 97db38173f..66d187ea2a 100644 --- a/rivetkit-rust/packages/rivetkit-core/src/registry/http.rs +++ b/rivetkit-rust/packages/rivetkit-core/src/registry/http.rs @@ -603,9 +603,14 @@ fn method_not_allowed_response( } fn rearm_sleep_after_request(ctx: ActorContext) { + // one waiter per actor, not one per request: under steady load the count rarely reaches zero + if !ctx.claim_http_idle_rearm() { + return; + } let sleep_ctx = ctx.clone(); ctx.wait_until(async move { sleep_ctx.wait_for_http_requests_idle().await; + sleep_ctx.release_http_idle_rearm(); sleep_ctx.reset_sleep_timer(); }); }