From 10daab06cf35cf5696aa6ed6b790d8115bfeb432 Mon Sep 17 00:00:00 2001 From: Philip Jenvey Date: Fri, 18 Apr 2025 16:29:15 -0700 Subject: [PATCH] fix: avoid underflow of the queued_tasks metric Closes STOR-201 --- syncserver-common/src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/syncserver-common/src/lib.rs b/syncserver-common/src/lib.rs index b509f325..54830063 100644 --- a/syncserver-common/src/lib.rs +++ b/syncserver-common/src/lib.rs @@ -155,7 +155,13 @@ impl BlockingThreadpool { /// Return the pool's current metrics pub fn metrics(&self) -> BlockingThreadpoolMetrics { let spawned_tasks = self.spawned_tasks.load(Ordering::Relaxed); - let active_threads = self.active_threads.load(Ordering::Relaxed); + // active_threads is decremented on a separate thread so there's no + // Drop order guarantee of spawned_tasks decrementing before it does: + // catch the case where active_threads is larger + let active_threads = self + .active_threads + .load(Ordering::Relaxed) + .min(spawned_tasks); BlockingThreadpoolMetrics { queued_tasks: spawned_tasks - active_threads, active_threads,