Skip to content

Commit

Permalink
Add regression test for deadlocking rayon
Browse files Browse the repository at this point in the history
  • Loading branch information
HeroicKatora committed Feb 25, 2022
1 parent 73b4fe5 commit 5d4e953
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ harness = false
name = "large_image"
harness = false

[[test]]
name = "rayon"
required-features = ["rayon"]

[features]
default = ["rayon"]
platform_independent = []
Expand Down
13 changes: 10 additions & 3 deletions src/worker/multithreaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ use super::immediate::ImmediateWorker;

pub fn with_multithreading<T>(f: impl FnOnce(&mut dyn Worker) -> T) -> T {
#[cfg(not(feature = "rayon"))]
let mut worker = StdThreadWorker(MpscWorker::default());
return self::enter_threads(f);

#[cfg(feature = "rayon")]
jpeg_rayon::enter(|mut worker| {
return jpeg_rayon::enter(|mut worker| {
f(&mut worker)
})
});
}

enum WorkerMsg {
Expand Down Expand Up @@ -128,6 +128,13 @@ fn collect_worker_thread(rx: Receiver<Vec<u8>>) -> Vec<u8> {
rx.recv().expect("jpeg-decoder worker thread error")
}

#[allow(dead_code)]
fn enter_threads<T>(f: impl FnOnce(&mut dyn Worker) -> T) -> T {
let mut worker = StdThreadWorker(MpscWorker::default());
f(&mut worker)
}


#[cfg(feature = "rayon")]
mod jpeg_rayon {
use crate::error::Result;
Expand Down
16 changes: 16 additions & 0 deletions tests/rayon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::{fs::File, path::Path};
use jpeg_decoder::Decoder;

#[test]
fn decoding_in_limited_threadpool_does_not_deadlock() {
let path = Path::new("tests").join("reftest").join("images").join("mozilla").join("jpg-progressive.jpg");

let pool = rayon::ThreadPoolBuilder::new()
.num_threads(1)
.build()
.unwrap();
pool.install(|| {
let mut decoder = Decoder::new(File::open(&path).unwrap());
let _ = decoder.decode().unwrap();
});
}

0 comments on commit 5d4e953

Please sign in to comment.