# Does std::panic::catch\_unwind spawn a new thread?

**URL:** <https://internals.rust-lang.org/t/does-std-catch-unwind-spawn-a-new-thread/10549>\
**Category:** libs\
**Created:** [July 11, 2019, 8:33am UTC](https://internals.rust-lang.org/t/does-std-catch-unwind-spawn-a-new-thread/10549 "2019-07-11T08:33:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![CreepySkeleton](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/creepyskeleton/32/6079_2.png) [@CreepySkeleton](https://internals.rust-lang.org/u/CreepySkeleton)\
**Post date:** [July 11, 2019, 8:33am UTC](https://internals.rust-lang.org/t/does-std-catch-unwind-spawn-a-new-thread/10549/1 "2019-07-11T08:33:01Z")

</div>

I’ve been looking for a while but the docs leave it unclear whether this fn spawns a new thread or not. Does it?

---

<div class="post-metadata">

**Author:** ![vorner](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/vorner/32/3310_2.png) [@vorner](https://internals.rust-lang.org/u/vorner)\
**Post date:** [July 11, 2019, 8:47am UTC](https://internals.rust-lang.org/t/does-std-catch-unwind-spawn-a-new-thread/10549/2 "2019-07-11T08:47:54Z")

</div>

No, it does not. Things in std don’t in general start threads on their own unless they say so. There’s no reason to say it doesn’t.

---

<div class="post-metadata">

**Author:** ![dhm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dhm/32/4879_2.png) [@dhm](https://internals.rust-lang.org/u/dhm)\
**Post date:** [July 11, 2019, 10:29pm UTC](https://internals.rust-lang.org/t/does-std-catch-unwind-spawn-a-new-thread/10549/3 "2019-07-11T22:29:45Z")

</div>

> [@vorner](#):
>
> There’s no reason to say it doesn’t.

Making things clearer wouldn't hurt. Stack unwinding stops at thread boundaries or at a `panic::catch_unwind`. Without further knowledge of the black box that unwinding is (for most people), guessing that `catch_unwind` may be implemented by spawning a thread is a **plausible** guess: not everybody knows of Rust "transparency" runtime-wise.

So I think that a short sentence clarifying this point could definitely be added 😉

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [July 11, 2019, 10:40pm UTC](https://internals.rust-lang.org/t/does-std-catch-unwind-spawn-a-new-thread/10549/4 "2019-07-11T22:40:35Z")

</div>

```rust
pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R>

```

Neither `F` nor `R` require the `Send` trait, so it would generally be **illegal** to send them across threads.

It’s _possible_ that it could internally use specialization for `F: Send` and `R: Send`, and only use threads in that case. But since it has to handle the `!Send` case already, why would it bother specializing? So it’s pretty safe to assume that it will run directly, from the type signature alone.

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [July 11, 2019, 10:48pm UTC](https://internals.rust-lang.org/t/does-std-catch-unwind-spawn-a-new-thread/10549/5 "2019-07-11T22:48:50Z")

</div>

> [@dhm](#):
>
> Stack unwinding stops at thread boundaries or at a `panic::catch_unwind` .

FWIW, stopping at the thread boundary is also due to `panic::catch_unwind`:

> <https://github.com/rust-lang/rust/blob/4bb6b4a5ed1cd377c5cfd97721ad12f52e63dd41/src/libstd/thread/mod.rs#L468-L473>

---

<div class="post-metadata">

**Author:** ![dhm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dhm/32/4879_2.png) [@dhm](https://internals.rust-lang.org/u/dhm)\
**Post date:** [July 12, 2019, 7:17am UTC](https://internals.rust-lang.org/t/does-std-catch-unwind-spawn-a-new-thread/10549/6 "2019-07-12T07:17:27Z")

</div>

> [@cuviper](#):
>
> Neither `F` nor `R` require the `Send` trait, so it would generally be **illegal** to send them across threads.

_Touché_

---

<div class="post-metadata">

**Author:** ![CreepySkeleton](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/creepyskeleton/32/6079_2.png) [@CreepySkeleton](https://internals.rust-lang.org/u/CreepySkeleton)\
**Post date:** [July 12, 2019, 10:53pm UTC](https://internals.rust-lang.org/t/does-std-catch-unwind-spawn-a-new-thread/10549/7 "2019-07-12T22:53:01Z")

</div>

> [@dhm](#):
>
> Without further knowledge of the black box that unwinding is (for most people), guessing that `catch_unwind` may be implemented by spawning a thread

Actually, I didn't think that the thread spawning takes place, just saw some mentioning about 'thread boundary' _somewhere in `std::panic`_, got curious, was too lazy to check myself 🙂

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [July 14, 2019, 8:17pm UTC](https://internals.rust-lang.org/t/does-std-catch-unwind-spawn-a-new-thread/10549/8 "2019-07-14T20:17:33Z")

</div>

It does make sense, but it’s not obvious.

There are abstractions like scoped threads which allow passing of references across threads. It wouldn’t be unreasonable to (technically incorrectly) assume that such mechanism may do something clever with an internal thread and blocking of the caller to make it safe.

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [July 15, 2019, 6:56am UTC](https://internals.rust-lang.org/t/does-std-catch-unwind-spawn-a-new-thread/10549/9 "2019-07-15T06:56:24Z")

</div>

Sure, scoped threads take care of `'static`, and I suppose blocking the caller would avoid basic data races, but there’s other state that might tie a value to a thread too. Suppose you have an index associated with some TLS cache, or a main-thread-only GUI handle, or any kind of unsafe user code where the thread context is one of its invariants. So I think it’s not a matter of being clever enough – Rust’s unwinding runtime would be unsound to cheat here.

I don’t mean to say that anyone is wrong to wonder about this, but I think the answer is a firm no, it cannot.

---

<div class="post-metadata">

**Author:** ![FenrirWolf](https://avatars.discourse-cdn.com/v4/letter/f/35a633/32.png) [@FenrirWolf](https://internals.rust-lang.org/u/FenrirWolf)\
**Post date:** [July 15, 2019, 8:44pm UTC](https://internals.rust-lang.org/t/does-std-catch-unwind-spawn-a-new-thread/10549/10 "2019-07-15T20:44:25Z")

</div>

The fact that `catch_unwind` returns a `thread::Result` might also be a slight source of confusion about whether or not it spawns a thread.

---

<div class="post-metadata">

**Author:** ![proc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/proc/32/5844_2.png) [@proc](https://internals.rust-lang.org/u/proc)\
**Post date:** [July 15, 2019, 10:31pm UTC](https://internals.rust-lang.org/t/does-std-catch-unwind-spawn-a-new-thread/10549/11 "2019-07-15T22:31:45Z")

</div>

I still could see where this comes from and wouldn’t have been able to say by myself. Before catch\_unwind the only option to catch panics was creating a new thread which you monitor, so it would have made total sense.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [October 13, 2019, 10:42pm UTC](https://internals.rust-lang.org/t/does-std-catch-unwind-spawn-a-new-thread/10549/12 "2019-10-13T22:42:24Z")

</div>

Closed on October 13, 2019, 10:42pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
