From 42ceeddef79e557cbda775de553fc926f42bb44c Mon Sep 17 00:00:00 2001 From: Yong Kang Date: Fri, 10 Oct 2025 10:34:14 +0000 Subject: [PATCH] fix: prevent active_handles underflow in ProofTaskManagerHandle - Added a debug assertion to ensure active_handles does not underflow when dropping a ProofTaskManagerHandle. - Implemented metrics recording to flush before exit when the last handle is dropped, enhancing monitoring capabilities. --- crates/trie/parallel/src/proof_task.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/trie/parallel/src/proof_task.rs b/crates/trie/parallel/src/proof_task.rs index 7d923acc6a..a01b1540e2 100644 --- a/crates/trie/parallel/src/proof_task.rs +++ b/crates/trie/parallel/src/proof_task.rs @@ -1029,7 +1029,19 @@ impl Drop for ProofTaskManagerHandle { fn drop(&mut self) { // Decrement the number of active handles. // When the last handle is dropped, the channels are dropped and workers shut down. - self.active_handles.fetch_sub(1, Ordering::SeqCst); + // atomically grab the current handle count and decrement it for Drop. + let previous_handles = self.active_handles.fetch_sub(1, Ordering::SeqCst); + + debug_assert_ne!( + previous_handles, 0, + "active_handles underflow in ProofTaskManagerHandle::drop" + ); + + #[cfg(feature = "metrics")] + if previous_handles == 1 { + // Flush metrics before exit. + self.metrics.record(); + } } }