Skip to content

Commit 16b0b4f

Browse files
committed
Use add_spawn_hook for libtest's output capturing.
1 parent 79adab1 commit 16b0b4f

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

library/std/src/thread/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,6 @@ impl Builder {
499499
});
500500
let their_packet = my_packet.clone();
501501

502-
let output_capture = crate::io::set_output_capture(None);
503-
crate::io::set_output_capture(output_capture.clone());
504-
505502
// Pass `f` in `MaybeUninit` because actually that closure might *run longer than the lifetime of `F`*.
506503
// See <https://github.com/rust-lang/rust/issues/101983> for more details.
507504
// To prevent leaks we use a wrapper that drops its contents.
@@ -539,7 +536,6 @@ impl Builder {
539536
imp::Thread::set_name(name);
540537
}
541538

542-
crate::io::set_output_capture(output_capture);
543539
for hook in hooks {
544540
hook();
545541
}

library/test/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#![feature(process_exitcode_internals)]
2525
#![feature(panic_can_unwind)]
2626
#![feature(test)]
27+
#![feature(thread_spawn_hook)]
2728
#![allow(internal_features)]
2829
#![warn(rustdoc::unescaped_backticks)]
2930

@@ -134,6 +135,16 @@ pub fn test_main(args: &[String], tests: Vec<TestDescAndFn>, options: Option<Opt
134135
}
135136
});
136137
panic::set_hook(hook);
138+
// Use a thread spawning hook to make new threads inherit output capturing.
139+
std::thread::add_spawn_hook(|_| {
140+
// Get and clone the output capture of the current thread.
141+
let output_capture = io::set_output_capture(None);
142+
io::set_output_capture(output_capture.clone());
143+
// Set the output capture of the new thread.
144+
Ok(|| {
145+
io::set_output_capture(output_capture);
146+
})
147+
});
137148
}
138149
let res = console::run_tests_console(&opts, tests);
139150
// Prevent Valgrind from reporting reachable blocks in users' unit tests.

0 commit comments

Comments
 (0)