Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libtest] Run the test synchronously when hitting thread limit #81546

Merged
merged 1 commit into from Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion library/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,18 @@ pub fn run_test(
let supports_threads = !cfg!(target_os = "emscripten") && !cfg!(target_arch = "wasm32");
if concurrency == Concurrent::Yes && supports_threads {
let cfg = thread::Builder::new().name(name.as_slice().to_owned());
Some(cfg.spawn(runtest).unwrap())
let mut runtest = Arc::new(Mutex::new(Some(runtest)));
let runtest2 = runtest.clone();
match cfg.spawn(move || runtest2.lock().unwrap().take().unwrap()()) {
Ok(handle) => Some(handle),
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
// `ErrorKind::WouldBlock` means hitting the thread limit on some
// platforms, so run the test synchronously here instead.
Arc::get_mut(&mut runtest).unwrap().get_mut().unwrap().take().unwrap()();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we know at this point that runtest2 has been dropped? That is subtle.

None
}
Err(e) => panic!("failed to spawn thread to run test: {}", e),
}
} else {
runtest();
None
Expand Down
7 changes: 7 additions & 0 deletions src/test/run-make/libtest-thread-limit/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-include ../../run-make-fulldeps/tools.mk

# only-linux

all:
$(RUSTC) test.rs --test --target $(TARGET)
$(shell ulimit -p 0 && $(call RUN,test))
This conversation was marked as resolved.
Show resolved Hide resolved
16 changes: 16 additions & 0 deletions src/test/run-make/libtest-thread-limit/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![feature(once_cell)]

use std::{io::ErrorKind, lazy::SyncOnceCell, thread::{self, Builder, ThreadId}};

static THREAD_ID: SyncOnceCell<ThreadId> = SyncOnceCell::new();

#[test]
fn spawn_thread_would_block() {
assert_eq!(Builder::new().spawn(|| unreachable!()).unwrap_err().kind(), ErrorKind::WouldBlock);
THREAD_ID.set(thread::current().id()).unwrap();
}

#[test]
fn run_in_same_thread() {
assert_eq!(*THREAD_ID.get().unwrap(), thread::current().id());
}