Search code examples
rustrayon

Rust rayon crate par_iter().map() "stalls" when saving images


I'm trying to learn Rust from an example on data-parallelism in this linked rust-cookbook example.

However, when I run the code (with what I believe are the correct dependencies), the parallel iterator functions do not complete. This is the exact repo I am struggling with.

When I run cargo run at the root of the project containing 5 images, I get the following in the console.

Current number threads: 4
Saving 5 thumbnails into 'thumbnails'...

However, none of the thumbnails are created, and the program never exits. The code compiles.

I have the same problem in another little CLI I'm working on to try to learn Rust, but I think the example from the cookbook is easier to work with.

Any tips, even with what I might look for, would be greatly appreciated. I wonder if it is machine relating.


Solution

  • This is a known deadlock bug in the image crate's JPEG decoder: GitHub issue

    The JPEG decoding itself makes use of rayon internally by default, and when you use rayon yourself to decode several JPEGs at once, this bug is triggered. As a workaround, you can stop the JPEG decoder from using rayon by specifying the image crate dependency in Cargo.toml like this:

    image = { version = "0.24.1", default-features = false, features = ["jpeg"] }
    

    That disables the image crate's default features, one of which is jpeg_rayon.