From 55dd5d868ef635da819c67601731da1205fce6f9 Mon Sep 17 00:00:00 2001 From: Nathaniel Cook Date: Tue, 14 Nov 2023 16:35:10 -0700 Subject: [PATCH] fix: panic with divide by zero duration math (#184) --- p2p/src/publisher.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/p2p/src/publisher.rs b/p2p/src/publisher.rs index 0225b6c65..9723d1ab8 100644 --- a/p2p/src/publisher.rs +++ b/p2p/src/publisher.rs @@ -245,9 +245,15 @@ impl Stream for Publisher { let batch_average_seconds = average.as_secs(); let lag_ratio = needed_seconds / deadline_seconds; - if !spare.is_zero() { + if remaining_batches == 0.0 { + // We do not have any more batches, fetch one more to be sure. + // If it comes back empty we will delay until the interval is complete. + self.state = State::StartingFetch; + } else if !spare.is_zero() { // Be conservative and adjust our delay based on our optimism of // the estimate. + // If remaining_batches is zero this math panics, so we ensure its + // not zero with the above case. let delay = spare.div_f64(remaining_batches / OPTIMISM); debug!( batch_average_seconds,