diff --git a/compiler/rustc_thread_pool/src/lib.rs b/compiler/rustc_thread_pool/src/lib.rs index 56d3bc1184454..0b1b7ed036573 100644 --- a/compiler/rustc_thread_pool/src/lib.rs +++ b/compiler/rustc_thread_pool/src/lib.rs @@ -308,6 +308,7 @@ impl ThreadPoolBuilder { /// to spawn threads in a [`spawn_handler`](#method.spawn_handler). /// The threads in this pool will start by calling `wrapper`, which should /// do initialization and continue by calling `ThreadBuilder::run()`. + /// Once the pool is built, this waits for all its threads to exit before returning. /// /// [`std::thread::scope`]: https://doc.rust-lang.org/std/thread/fn.scope.html /// @@ -345,6 +346,7 @@ impl ThreadPoolBuilder { F: FnOnce(&ThreadPool) -> R, { std::thread::scope(|scope| { + let mut handles = Vec::new(); let pool = self .spawn_handler(|thread| { let mut builder = std::thread::Builder::new(); @@ -354,15 +356,24 @@ impl ThreadPoolBuilder { if let Some(size) = thread.stack_size() { builder = builder.stack_size(size); } - builder.spawn_scoped(scope, || wrapper(thread))?; + handles.push(builder.spawn_scoped(scope, || wrapper(thread))?); Ok(()) }) .build()?; let result = unwind::halt_unwinding(|| with_pool(&pool)); pool.wait_until_stopped(); - match result { - Ok(result) => Ok(result), - Err(err) => unwind::resume_unwinding(err), + // `wait_until_stopped` and the scope's implicit join only wait for the + // closures to return, not for the threads to exit (TLS destructors etc.). + // A process exit in between can crash on windows-gnu. + let mut worker_panic = None; + for handle in handles { + if let Err(err) = handle.join() { + worker_panic.get_or_insert(err); + } + } + match (result, worker_panic) { + (Ok(result), None) => Ok(result), + (Err(err), _) | (Ok(_), Some(err)) => unwind::resume_unwinding(err), } }) } diff --git a/compiler/rustc_thread_pool/tests/scoped_threadpool.rs b/compiler/rustc_thread_pool/tests/scoped_threadpool.rs index 295da650e8805..bf1e62ed02a9d 100644 --- a/compiler/rustc_thread_pool/tests/scoped_threadpool.rs +++ b/compiler/rustc_thread_pool/tests/scoped_threadpool.rs @@ -97,3 +97,37 @@ fn build_scoped_tls_threadpool() { }); }); } + +#[test] +#[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)] +fn build_scoped_thread_exit() { + use std::sync::atomic::{AtomicUsize, Ordering}; + use std::time::Duration; + + const NUM_THREADS: usize = 4; + static EXITED: AtomicUsize = AtomicUsize::new(0); + + struct OnExit; + impl Drop for OnExit { + fn drop(&mut self) { + // Make exiting slow enough that returning early is visible. + std::thread::sleep(Duration::from_millis(10)); + EXITED.fetch_add(1, Ordering::SeqCst); + } + } + // TLS destructors run while the thread exits, after its closure has returned. + thread_local!(static ON_EXIT: OnExit = const { OnExit }); + + ThreadPoolBuilder::new() + .num_threads(NUM_THREADS) + .build_scoped( + |thread| { + ON_EXIT.with(|_| {}); + thread.run() + }, + |pool| pool.install(|| {}), + ) + .expect("thread pool created"); + // `build_scoped` waits for the threads to exit. + assert_eq!(EXITED.load(Ordering::SeqCst), NUM_THREADS); +}