@@ -1287,12 +1287,31 @@ unsafe impl<'scope, T: Sync> Sync for Packet<'scope, T> {}
1287
1287
1288
1288
impl < ' scope , T > Drop for Packet < ' scope , T > {
1289
1289
fn drop ( & mut self ) {
1290
+ // If this packet was for a thread that ran in a scope, the thread
1291
+ // panicked, and nobody consumed the panic payload, we make sure
1292
+ // the scope function will panic.
1293
+ let unhandled_panic = matches ! ( self . result. get_mut( ) , Some ( Err ( _) ) ) ;
1294
+ // Drop the result without causing unwinding.
1295
+ // This is only relevant for threads that aren't join()ed, as
1296
+ // join() will take the `result` and set it to None, such that
1297
+ // there is nothing left to drop here.
1298
+ // If this panics, we should handle that, because we're outside the
1299
+ // outermost `catch_unwind` of our thread.
1300
+ // We just abort in that case, since there's nothing else we can do.
1301
+ // (And even if we tried to handle it somehow, we'd also need to handle
1302
+ // the case where the panic payload we get out of it also panics on
1303
+ // drop, and so on. See issue #86027.)
1304
+ if let Err ( _) = panic:: catch_unwind ( panic:: AssertUnwindSafe ( || {
1305
+ * self . result . get_mut ( ) = None ;
1306
+ } ) ) {
1307
+ rtabort ! ( "thread result panicked on drop" ) ;
1308
+ }
1290
1309
// Book-keeping so the scope knows when it's done.
1291
1310
if let Some ( scope) = self . scope {
1292
- // If this packet was for a thread that ran in a scope, the thread
1293
- // panicked, and nobody consumed the panic payload, we make sure
1294
- // the scope function will panic.
1295
- let unhandled_panic = matches ! ( self . result . get_mut ( ) , Some ( Err ( _ ) ) ) ;
1311
+ // Now that there will be no more user code running on this thread
1312
+ // that can use 'scope, mark the thread as 'finished'.
1313
+ // It's important we only do this after the `result` has been dropped,
1314
+ // since dropping it might still use things it borrowed from 'scope.
1296
1315
scope. decrement_num_running_threads ( unhandled_panic) ;
1297
1316
}
1298
1317
}
0 commit comments