From b1450070d119ff2450bb5eecc17e0e6ac52822b1 Mon Sep 17 00:00:00 2001 From: Niklas Elsbrock Date: Sat, 1 Nov 2025 09:40:42 +0100 Subject: [PATCH] use a bounded channel and fix panic in race condition --- src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 535d319..6c2d3a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,7 +72,7 @@ impl Fingerprunk { pub fn run(mut self) -> anyhow::Result<()> { self.started_instant = Instant::now(); - let (sender, receiver) = mpsc::channel(); + let (sender, receiver) = mpsc::sync_channel(16); { let sender = sender.clone(); @@ -133,7 +133,7 @@ impl Fingerprunk { }) } - fn worker_thread(&self, sender: mpsc::Sender) { + fn worker_thread(&self, sender: mpsc::SyncSender) { let mut fingerprint_hex = String::with_capacity(20 * 2); while !self.stop.load(Ordering::Relaxed) { @@ -143,9 +143,9 @@ impl Fingerprunk { write!(fingerprint_hex, "{:X}", key.fingerprint()) .expect("should write into string without error"); if self.check_fingerprint(&fingerprint_hex) { - sender - .send(Message::Key(Key::V4(key))) - .expect("should be able to send key"); + // The channel might already be closed here if we're stopping. + // That is fine, so we just ignore the error. + let _ = sender.send(Message::Key(Key::V4(key))); } self.counter_tried.fetch_add(1, Ordering::Relaxed); }