How to test `rayon` with `miri`?

When I try to run some tests which use rayon using miri, it complains:
error: the main thread terminated without waiting for all remaining threads

I tried to use local threadpool (hoping that rayon would finish all workers when it dropped) but error still persists.

    #[test]
    fn test_pool(){
        let pool = rayon::ThreadPoolBuilder::new().build().unwrap();
        pool.install(|| {});
    }

Are there some way to fix this?
Or, at least, workaround to avoid running such tests with miri?

The global pool never terminates. A local pool will terminate after dropping, but not synchronously, so it's still possible for you to race to the exit while the threads are alive. This shouldn't be any problem in practice, but I don't know if you can convince miri that it's ok.

If you really want to be strict about it, you can use build_scoped -- just call thread.run() in the wrapper and the rest of your code in the other closure using the pool.

2 Likes

Thanks, using of build_scoped helped.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.