From a3cf562a25ca08dc118c3f22f032f4daed65296a Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sat, 21 Nov 2026 11:06:25 -0500 Subject: [PATCH 1/2] rustc_thread_pool: test whether `build_scoped` waits for thread exit Records the current behavior: it returns before the threads have exited. --- .../tests/scoped_threadpool.rs | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/compiler/rustc_thread_pool/tests/scoped_threadpool.rs b/compiler/rustc_thread_pool/tests/scoped_threadpool.rs index 295da650e8805..15fc320d6d387 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` returns before the threads have exited. + assert_eq!(EXITED.load(Ordering::SeqCst), 0); +} From 4e78a54a621b587ce36b55b66ee0082942af7353 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sat, 21 Nov 2026 11:06:25 -0500 Subject: [PATCH 2/2] rustc_thread_pool: join worker threads in `build_scoped` `wait_until_stopped` and the scope's implicit join only wait for the worker closures to return. The threads may still be exiting, e.g. running TLS destructors, when `build_scoped` returns. If the process exits then, they get killed midway, and on windows-gnu rustc sometimes crashes at exit with a stack overflow. Join the threads, like `run_in_thread_with_globals` does for the serial compiler. A worker panic is resumed after all threads are joined. --- compiler/rustc_thread_pool/src/lib.rs | 19 +++++++++++++++---- .../tests/scoped_threadpool.rs | 4 ++-- 2 files changed, 17 insertions(+), 6 deletions(-) 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 15fc320d6d387..bf1e62ed02a9d 100644 --- a/compiler/rustc_thread_pool/tests/scoped_threadpool.rs +++ b/compiler/rustc_thread_pool/tests/scoped_threadpool.rs @@ -128,6 +128,6 @@ fn build_scoped_thread_exit() { |pool| pool.install(|| {}), ) .expect("thread pool created"); - // `build_scoped` returns before the threads have exited. - assert_eq!(EXITED.load(Ordering::SeqCst), 0); + // `build_scoped` waits for the threads to exit. + assert_eq!(EXITED.load(Ordering::SeqCst), NUM_THREADS); }