diff options
| author | Ralf Jung <post@ralfj.de> | 2025-04-02 23:54:22 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2025-04-02 23:54:22 +0200 |
| commit | e435ba32d85057070dba3f07fa8afd96799a0cfb (patch) | |
| tree | c989d04f4bcd32bcd93ed62208af68e1dbe36b6d /library/std/src/thread/spawnhook.rs | |
| parent | a3af09faa39d5a8d303a4cc2f80182e0f01baa7c (diff) | |
| parent | 64b58dd13b06a23d8429a73c225347b4cd3b2c3c (diff) | |
Merge from rustc
Diffstat (limited to 'library/std/src/thread/spawnhook.rs')
| -rw-r--r-- | library/std/src/thread/spawnhook.rs | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/library/std/src/thread/spawnhook.rs b/library/std/src/thread/spawnhook.rs index 99b5ad9cb9f..98f471ad54b 100644 --- a/library/std/src/thread/spawnhook.rs +++ b/library/std/src/thread/spawnhook.rs @@ -113,18 +113,23 @@ where pub(super) fn run_spawn_hooks(thread: &Thread) -> ChildSpawnHooks { // Get a snapshot of the spawn hooks. // (Increments the refcount to the first node.) - let hooks = SPAWN_HOOKS.with(|hooks| { + if let Ok(hooks) = SPAWN_HOOKS.try_with(|hooks| { let snapshot = hooks.take(); hooks.set(snapshot.clone()); snapshot - }); - // Iterate over the hooks, run them, and collect the results in a vector. - let to_run: Vec<_> = iter::successors(hooks.first.as_deref(), |hook| hook.next.as_deref()) - .map(|hook| (hook.hook)(thread)) - .collect(); - // Pass on the snapshot of the hooks and the results to the new thread, - // which will then run SpawnHookResults::run(). - ChildSpawnHooks { hooks, to_run } + }) { + // Iterate over the hooks, run them, and collect the results in a vector. + let to_run: Vec<_> = iter::successors(hooks.first.as_deref(), |hook| hook.next.as_deref()) + .map(|hook| (hook.hook)(thread)) + .collect(); + // Pass on the snapshot of the hooks and the results to the new thread, + // which will then run SpawnHookResults::run(). + ChildSpawnHooks { hooks, to_run } + } else { + // TLS has been destroyed. Skip running the hooks. + // See https://github.com/rust-lang/rust/issues/138696 + ChildSpawnHooks::default() + } } /// The results of running the spawn hooks. |
