Skip to content

Commit 55ab2e3

Browse files
authored
Rollup merge of #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 #78165. ``@rustbot`` label: A-libtest T-libs
2 parents cb2effd + 43aed74 commit 55ab2e3

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

library/test/src/lib.rs

+12-1
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-include ../../run-make-fulldeps/tools.mk
2+
3+
# only-linux
4+
5+
all:
6+
$(RUSTC) test.rs --test --target $(TARGET)
7+
$(shell ulimit -p 0 && $(call RUN,test))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(once_cell)]
2+
3+
use std::{io::ErrorKind, lazy::SyncOnceCell, thread::{self, Builder, ThreadId}};
4+
5+
static THREAD_ID: SyncOnceCell<ThreadId> = SyncOnceCell::new();
6+
7+
#[test]
8+
fn spawn_thread_would_block() {
9+
assert_eq!(Builder::new().spawn(|| unreachable!()).unwrap_err().kind(), ErrorKind::WouldBlock);
10+
THREAD_ID.set(thread::current().id()).unwrap();
11+
}
12+
13+
#[test]
14+
fn run_in_same_thread() {
15+
assert_eq!(*THREAD_ID.get().unwrap(), thread::current().id());
16+
}

0 commit comments

Comments
 (0)