@@ -113,18 +113,23 @@ where
113
113
pub ( super ) fn run_spawn_hooks ( thread : & Thread ) -> ChildSpawnHooks {
114
114
// Get a snapshot of the spawn hooks.
115
115
// (Increments the refcount to the first node.)
116
- let hooks = SPAWN_HOOKS . with ( |hooks| {
116
+ if let Ok ( hooks) = SPAWN_HOOKS . try_with ( |hooks| {
117
117
let snapshot = hooks. take ( ) ;
118
118
hooks. set ( snapshot. clone ( ) ) ;
119
119
snapshot
120
- } ) ;
121
- // Iterate over the hooks, run them, and collect the results in a vector.
122
- let to_run: Vec < _ > = iter:: successors ( hooks. first . as_deref ( ) , |hook| hook. next . as_deref ( ) )
123
- . map ( |hook| ( hook. hook ) ( thread) )
124
- . collect ( ) ;
125
- // Pass on the snapshot of the hooks and the results to the new thread,
126
- // which will then run SpawnHookResults::run().
127
- ChildSpawnHooks { hooks, to_run }
120
+ } ) {
121
+ // Iterate over the hooks, run them, and collect the results in a vector.
122
+ let to_run: Vec < _ > = iter:: successors ( hooks. first . as_deref ( ) , |hook| hook. next . as_deref ( ) )
123
+ . map ( |hook| ( hook. hook ) ( thread) )
124
+ . collect ( ) ;
125
+ // Pass on the snapshot of the hooks and the results to the new thread,
126
+ // which will then run SpawnHookResults::run().
127
+ ChildSpawnHooks { hooks, to_run }
128
+ } else {
129
+ // TLS has been destroyed. Skip running the hooks.
130
+ // See https://github.com/rust-lang/rust/issues/138696
131
+ ChildSpawnHooks :: default ( )
132
+ }
128
133
}
129
134
130
135
/// The results of running the spawn hooks.
0 commit comments