Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2cc4b2e

Browse files
committedNov 19, 2024
Use add_spawn_hook for libtest's output capturing.
1 parent 24a0765 commit 2cc4b2e

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed
 

‎std/src/thread/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,6 @@ impl Builder {
502502
});
503503
let their_packet = my_packet.clone();
504504

505-
let output_capture = crate::io::set_output_capture(None);
506-
crate::io::set_output_capture(output_capture.clone());
507-
508505
// Pass `f` in `MaybeUninit` because actually that closure might *run longer than the lifetime of `F`*.
509506
// See <https://github.com/rust-lang/rust/issues/101983> for more details.
510507
// 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
}

‎test/src/lib.rs

+11
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)
Failed to load comments.