Skip to content

Commit 76fb752

Browse files
authored
Rollup merge of rust-lang#81546 - hyd-dev:libtest-run-out-of-threads, r=Mark-Simulacrum
[libtest] Run the test synchronously when hitting thread limit libtest currently panics if it hits the thread limit. This often results in spurious test failures (<code>thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 11, kind: WouldBlock, message: "Resource temporarily unavailable" }'</code> ... `error: test failed, to rerun pass '--lib'`). This PR makes it continue to run the test synchronously if it runs out of threads. Closes rust-lang#78165. ``@rustbot`` label: A-libtest T-libs
2 parents 272b7d0 + 2587d3f commit 76fb752

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

test/src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,18 @@ pub fn run_test(
506506
let supports_threads = !cfg!(target_os = "emscripten") && !cfg!(target_arch = "wasm32");
507507
if concurrency == Concurrent::Yes && supports_threads {
508508
let cfg = thread::Builder::new().name(name.as_slice().to_owned());
509-
Some(cfg.spawn(runtest).unwrap())
509+
let mut runtest = Arc::new(Mutex::new(Some(runtest)));
510+
let runtest2 = runtest.clone();
511+
match cfg.spawn(move || runtest2.lock().unwrap().take().unwrap()()) {
512+
Ok(handle) => Some(handle),
513+
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
514+
// `ErrorKind::WouldBlock` means hitting the thread limit on some
515+
// platforms, so run the test synchronously here instead.
516+
Arc::get_mut(&mut runtest).unwrap().get_mut().unwrap().take().unwrap()();
517+
None
518+
}
519+
Err(e) => panic!("failed to spawn thread to run test: {}", e),
520+
}
510521
} else {
511522
runtest();
512523
None

0 commit comments

Comments
 (0)