Skip to content

Commit 5226395

Browse files
committed
Fix soundness issue in scoped threads.
1 parent 6045c34 commit 5226395

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

library/std/src/thread/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,23 @@ impl<'scope, T> Drop for Packet<'scope, T> {
12931293
// panicked, and nobody consumed the panic payload, we make sure
12941294
// the scope function will panic.
12951295
let unhandled_panic = matches!(self.result.get_mut(), Some(Err(_)));
1296+
// Drop the result before decrementing the number of running
1297+
// threads, because the Drop implementation might still use things
1298+
// it borrowed from 'scope.
1299+
// This is only relevant for threads that aren't join()ed, as
1300+
// join() will take the `result` and set it to None, such that
1301+
// there is nothing left to drop here.
1302+
// If this drop panics, that just results in an abort, because
1303+
// we're outside of the outermost `catch_unwind` of our thread.
1304+
// The same happens for detached non-scoped threads when dropping
1305+
// their ignored return value (or panic payload) panics, so
1306+
// there's no need to try to do anything better.
1307+
// (And even if we tried to handle it, we'd also need to handle
1308+
// the case where the panic payload we get out of it also panics
1309+
// on drop, and so on. See issue #86027.)
1310+
*self.result.get_mut() = None;
1311+
// Now that there will be no more user code running on this thread
1312+
// that can use 'scope, mark the thread as 'finished'.
12961313
scope.decrement_num_running_threads(unhandled_panic);
12971314
}
12981315
}

0 commit comments

Comments
 (0)