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.
This commit is contained in:
Yong Kang
2025-10-10 10:34:14 +00:00
parent 0b18f6488a
commit 42ceeddef7

View File

@@ -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();
}
}
}