use a bounded channel and fix panic in race condition

This commit is contained in:
2025-11-01 09:40:42 +01:00
parent 241fecd8a7
commit b1450070d1
+5 -5
View File
@@ -72,7 +72,7 @@ impl Fingerprunk {
pub fn run(mut self) -> anyhow::Result<()> { pub fn run(mut self) -> anyhow::Result<()> {
self.started_instant = Instant::now(); self.started_instant = Instant::now();
let (sender, receiver) = mpsc::channel(); let (sender, receiver) = mpsc::sync_channel(16);
{ {
let sender = sender.clone(); let sender = sender.clone();
@@ -133,7 +133,7 @@ impl Fingerprunk {
}) })
} }
fn worker_thread(&self, sender: mpsc::Sender<Message>) { fn worker_thread(&self, sender: mpsc::SyncSender<Message>) {
let mut fingerprint_hex = String::with_capacity(20 * 2); let mut fingerprint_hex = String::with_capacity(20 * 2);
while !self.stop.load(Ordering::Relaxed) { while !self.stop.load(Ordering::Relaxed) {
@@ -143,9 +143,9 @@ impl Fingerprunk {
write!(fingerprint_hex, "{:X}", key.fingerprint()) write!(fingerprint_hex, "{:X}", key.fingerprint())
.expect("should write into string without error"); .expect("should write into string without error");
if self.check_fingerprint(&fingerprint_hex) { if self.check_fingerprint(&fingerprint_hex) {
sender // The channel might already be closed here if we're stopping.
.send(Message::Key(Key::V4(key))) // That is fine, so we just ignore the error.
.expect("should be able to send key"); let _ = sender.send(Message::Key(Key::V4(key)));
} }
self.counter_tried.fetch_add(1, Ordering::Relaxed); self.counter_tried.fetch_add(1, Ordering::Relaxed);
} }