|
73 | 73 | trace!(thread = ?id, "spawn");
|
74 | 74 |
|
75 | 75 | Scheduler::spawn(Box::new(move || {
|
| 76 | + /// the given closure `f` may panic when executed. |
| 77 | + /// when this happens, we still want to ensure that |
| 78 | + /// thread locals are destructed. therefore, we set |
| 79 | + /// up a guard that is dropped as part of the unwind |
| 80 | + /// logic when `f` panics. |
| 81 | + struct PanicGuard; |
| 82 | + impl Drop for PanicGuard { |
| 83 | + fn drop(&mut self) { |
| 84 | + thread_done(false); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // set up the panic guard |
| 89 | + let panic_guard = PanicGuard; |
| 90 | + |
| 91 | + // execute the closure, note that `f()` may panic! |
76 | 92 | f();
|
77 |
| - thread_done(false); |
| 93 | + |
| 94 | + // if `f()` didn't panic, then we terminate the |
| 95 | + // spawned thread by dropping the guard ourselves. |
| 96 | + drop(panic_guard); |
78 | 97 | }));
|
79 | 98 |
|
80 | 99 | id
|
@@ -172,6 +191,21 @@ where
|
172 | 191 | }
|
173 | 192 |
|
174 | 193 | pub fn thread_done(is_main_thread: bool) {
|
| 194 | + let is_active = execution(|execution| execution.threads.is_active()); |
| 195 | + if !is_active { |
| 196 | + // if the thread is not active and the current thread is panicking, |
| 197 | + // then this means that loom has detected a problem (e.g. a deadlock). |
| 198 | + // we don't want to throw a double panic here, because this would cause |
| 199 | + // the entire test to abort and this hides the error from the end user. |
| 200 | + // instead we ensure that the current thread is panicking already, |
| 201 | + // or we cause a panic if it's not yet panicking (which it otherwise |
| 202 | + // would anyway, on the call to `execution.threads.active_id()` below). |
| 203 | + let panicking = std::thread::panicking(); |
| 204 | + trace!(?panicking, "thread_done: no active thread"); |
| 205 | + assert!(panicking); |
| 206 | + return; |
| 207 | + } |
| 208 | + |
175 | 209 | let locals = execution(|execution| {
|
176 | 210 | let thread = execution.threads.active_id();
|
177 | 211 |
|
|
0 commit comments