Skip to content

Commit 3cd5324

Browse files
committed
Use add_spawn_hook for libtest's output capturing.
1 parent 4a9c3ce commit 3cd5324

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

library/std/src/thread/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,6 @@ impl Builder {
509509
});
510510
let their_packet = my_packet.clone();
511511

512-
let output_capture = crate::io::set_output_capture(None);
513-
crate::io::set_output_capture(output_capture.clone());
514-
515512
// Pass `f` in `MaybeUninit` because actually that closure might *run longer than the lifetime of `F`*.
516513
// See <https://github.com/rust-lang/rust/issues/101983> for more details.
517514
// To prevent leaks we use a wrapper that drops its contents.
@@ -542,7 +539,6 @@ impl Builder {
542539
imp::Thread::set_name(name);
543540
}
544541

545-
crate::io::set_output_capture(output_capture);
546542
for hook in hooks {
547543
hook();
548544
}

library/test/src/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#![feature(process_exitcode_internals)]
2424
#![feature(panic_can_unwind)]
2525
#![feature(test)]
26+
#![feature(thread_spawn_hook)]
2627
#![allow(internal_features)]
2728

2829
// Public reexports
@@ -138,6 +139,16 @@ pub fn test_main(args: &[String], tests: Vec<TestDescAndFn>, options: Option<Opt
138139
}
139140
});
140141
panic::set_hook(hook);
142+
// Use a thread spawning hook to make new threads inherit output capturing.
143+
std::thread::add_spawn_hook(|_| {
144+
// Get and clone the output capture of the current thread.
145+
let output_capture = io::set_output_capture(None);
146+
io::set_output_capture(output_capture.clone());
147+
// Set the output capture of the new thread.
148+
Ok(|| {
149+
io::set_output_capture(output_capture);
150+
})
151+
});
141152
}
142153
let res = console::run_tests_console(&opts, tests);
143154
// Prevent Valgrind from reporting reachable blocks in users' unit tests.

0 commit comments

Comments
 (0)