Skip to content

Commit 3fcabb6

Browse files
SAY-5SAY-5
andauthored
wasm-mutate-stats: release worklist mutex before file I/O (#2491)
The writer thread popped from the to_write queue with if let Some(wasm) = to_write_clone.lock().unwrap().pop() { std::fs::write(filename, &wasm)?; } The temporary MutexGuard returned by .lock() lives for the entire 'if let' / 'while let' body, so the queue mutex stayed held across each std::fs::write call, blocking the producer thread (and shutdown) on every file write. Hoist the .pop() into its own statement so the guard is dropped before the I/O, then match on the resulting Option in a separate scope. Cargo check on -p wasm-mutate-stats stays green. Co-authored-by: SAY-5 <saiasish.cnp@gmail.com>
1 parent 99a4c68 commit 3fcabb6

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

crates/wasm-mutate-stats/src/bin/wasm-mutate-stats.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -453,18 +453,24 @@ impl State {
453453
let mut counter = 0;
454454

455455
while !finish_writing_wrap_clone.load(Relaxed) {
456-
// pop from worklist
457-
if let Some(wasm) = to_write_clone.lock().unwrap().pop() {
456+
// Pop from the worklist while holding the mutex for as short a window
457+
// as possible — the previous form `if let Some(_) = to_write_clone.lock()
458+
// .unwrap().pop()` keeps the temporary MutexGuard alive for the entire
459+
// body of the `if let`, blocking the producers (and shutdown) on every
460+
// file write.
461+
let wasm = to_write_clone.lock().unwrap().pop();
462+
if let Some(wasm) = wasm {
458463
let filename = artifact_folder_cp.join(format!("mutated.{counter}.wasm"));
459464
std::fs::write(filename, &wasm).context("Failed to write mutated wasm")?;
460465
counter += 1;
461466
}
462467
}
463468
eprintln!("Writing down pending mutated binaries!");
464-
// Then write pending wasms
465-
while let Some(wasm) = to_write_clone.lock().unwrap().pop() {
469+
// Then write pending wasms — same lock-narrowing as above.
470+
loop {
471+
let wasm = to_write_clone.lock().unwrap().pop();
472+
let Some(wasm) = wasm else { break };
466473
let filename = artifact_folder_cp.join(format!("mutated.{counter}.wasm"));
467-
468474
std::fs::write(filename, &wasm).context("Failed to write mutated wasm")?;
469475
counter += 1;
470476
}

0 commit comments

Comments
 (0)