exit_guard: the test-only step between the counts tolerates destroyed thread-locals
In builds of the runtime's own tests, Guard::close's wait runs a test
step between its reads (between_counts), which read BETWEEN_COUNTS with
LocalKey::with. On macOS exit() destroys the exiting thread's
thread-locals before it runs the exit handlers, so a test child that
exited from a thread that had used the step's thread-local, with a
section of the runtime's in progress on another thread, panicked in the
hook ("cannot access a Thread Local Storage value during or after
destruction"), and the panic in exit_handler, which cannot unwind,
aborted the child. Measured on the M3 (MoltenVK 1.4.2): 2965 of 5931
probe children aborted there, every one that exited with a command
buffer in flight, none of those that did not. Builds without cfg(test)
run no step (between_counts is empty), so consumers never reached it.
The step now reads its thread-local with try_with and runs nothing once
it is destroyed. The new test runs the hook's wait in a thread-local's
destructor after the step's thread-local was destroyed: it aborted the
test process before, and now the wait ends at its bound.
(cherry picked from commit 10f0048c43fc0498c61842bd823fdae1dd632c09)
This commit is contained in:
1 parent
cfbf33126e
commit
0e2ac3f345
2 files changed
+46
-2
No files matched your search
@@ -1179,10 +1179,14 @@ where
|
||||
|
||||
/// A test's step between the guard's two counts, run once: between a section's end and its
|
||||
/// making's ([`Busy`]'s drop), or between [`Guard::close`]'s read of the sections and its read of
|
||||
/// the makings, whichever this thread reaches first (`tests::exit_guard`).
|
||||
/// the makings, whichever this thread reaches first (`tests::exit_guard`). None once this thread's
|
||||
/// thread-locals are destroyed: on macOS `exit` destroys the exiting thread's before it runs the
|
||||
/// exit handlers, the hook's [`Guard::close`] among them, where a read of a destroyed one panics,
|
||||
/// which in a handler that cannot unwind aborts the process.
|
||||
#[cfg(test)]
|
||||
fn between_counts() {
|
||||
if let Some(step) = BETWEEN_COUNTS.with(|step| step.borrow_mut().take()) {
|
||||
let step = BETWEEN_COUNTS.try_with(|step| step.borrow_mut().take());
|
||||
if let Ok(Some(step)) = step {
|
||||
step();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,6 +210,46 @@ fn the_hook_waits_past_its_bound_for_a_making_in_progress() {
|
||||
drop(busy);
|
||||
}
|
||||
|
||||
/// The hook's wait on a thread whose thread-locals were destroyed runs no test step and returns:
|
||||
/// on macOS `exit` destroys the exiting thread's thread-locals before it runs the exit handlers,
|
||||
/// and a wait that read the step's (`exit_guard::BETWEEN_COUNTS`) there panicked, which in the
|
||||
/// handler, a function that cannot unwind, aborted every test child that exited from a thread
|
||||
/// with a section of the runtime's in progress (the M3's exit probe, 2965 of 5931 children). Here
|
||||
/// the wait runs in a thread-local's destructor, after the step's was destroyed: thread-locals
|
||||
/// are destroyed in the reverse order of their first use.
|
||||
#[test]
|
||||
fn the_hook_waits_on_a_thread_whose_thread_locals_are_destroyed() {
|
||||
struct WaitsAtExit(&'static Guard, mpsc::Sender<bool>);
|
||||
impl Drop for WaitsAtExit {
|
||||
fn drop(&mut self) {
|
||||
let ended = self.0.close(0, Duration::from_millis(20), Duration::ZERO);
|
||||
let _ = self.1.send(ended);
|
||||
}
|
||||
}
|
||||
thread_local! {
|
||||
static AT_EXIT: std::cell::RefCell<Option<WaitsAtExit>> = const {
|
||||
std::cell::RefCell::new(None)
|
||||
};
|
||||
}
|
||||
let guard = guard();
|
||||
// A section in progress on another thread for the wait to wait for.
|
||||
let busy = guard.enter().expect("open");
|
||||
let (sender, waited) = mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
AT_EXIT.with(|at_exit| *at_exit.borrow_mut() = Some(WaitsAtExit(guard, sender)));
|
||||
// First used after `AT_EXIT`, so destroyed before it.
|
||||
exit_guard::BETWEEN_COUNTS.with(|step| assert!(step.borrow().is_none()));
|
||||
})
|
||||
.join()
|
||||
.expect("the thread's end");
|
||||
assert_eq!(
|
||||
waited.recv_timeout(Duration::from_secs(5)),
|
||||
Ok(false),
|
||||
"the wait for the section in progress ended at its bound"
|
||||
);
|
||||
drop(busy);
|
||||
}
|
||||
|
||||
/// A making that ends as the hook looks, past its bound, is seen ended: whether it ends between
|
||||
/// the hook's read of the sections and its read of the makings, or the hook reads both between
|
||||
/// the section's end and its making's. The hook gave up on such a making once (`close` returned
|
||||
|
||||
Reference in new issue
Block a user