# Should \`Backtrace\` be RefUnwindSafe?

**URL:** <https://internals.rust-lang.org/t/should-backtrace-be-refunwindsafe/17169>\
**Category:** Uncategorized\
**Created:** [August 11, 2022, 7:46pm UTC](https://internals.rust-lang.org/t/should-backtrace-be-refunwindsafe/17169 "2022-08-11T19:46:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cole-miller](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cole-miller/32/7607_2.png) [@cole-miller](https://internals.rust-lang.org/u/cole-miller)\
**Post date:** [August 11, 2022, 7:46pm UTC](https://internals.rust-lang.org/t/should-backtrace-be-refunwindsafe/17169/1 "2022-08-11T19:46:41Z")

</div>

I'm using the soon-to-be stable `std::backtrace::Backtrace` in one of my libraries, and noticed that it's currently [`!RefUnwindSafe`](https://doc.rust-lang.org/stable/std/backtrace/struct.Backtrace.html#impl-RefUnwindSafe). This is ultimately because [this gadget](https://stdrs.dev/nightly/x86_64-unknown-linux-gnu/std/backtrace/struct.LazilyResolvedCapture.html) contains an `UnsafeCell<...>` and has `unsafe impl Sync` but not `impl RefUnwindSafe`. I'm wondering whether this was perhaps an oversight, and whether we should have `Backtrace: RefUnwindSafe` after all.

---

<div class="post-metadata">

**Author:** ![tvallotton](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tvallotton/32/8153_2.png) [@tvallotton](https://internals.rust-lang.org/u/tvallotton)\
**Post date:** [August 12, 2022, 5:11am UTC](https://internals.rust-lang.org/t/should-backtrace-be-refunwindsafe/17169/2 "2022-08-12T05:11:00Z")

</div>

I imagine that it not being `RefUnwindSafe` makes it very useless, since its very purpose is to report errors.

---

<div class="post-metadata">

**Author:** ![xfix](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/xfix/32/2726_2.png) [@xfix](https://internals.rust-lang.org/u/xfix)\
**Post date:** [August 12, 2022, 2:48pm UTC](https://internals.rust-lang.org/t/should-backtrace-be-refunwindsafe/17169/3 "2022-08-12T14:48:35Z")

</div>

It probably should, as `LazilyResolvedCapture` uses interior mutability for a performance optimization that should be invisible to an user. Panics shouldn't cause issues with it, as there is no actual state.

Edit: Created a pull request - [Implement RefUnwindSafe for Backtrace by xfix · Pull Request #100455 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/pull/100455).

---

<div class="post-metadata">

**Author:** ![cole-miller](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cole-miller/32/7607_2.png) [@cole-miller](https://internals.rust-lang.org/u/cole-miller)\
**Post date:** [August 13, 2022, 5:33pm UTC](https://internals.rust-lang.org/t/should-backtrace-be-refunwindsafe/17169/4 "2022-08-13T17:33:11Z")

</div>

Thanks!

While we're here, I'm wondering why `std::io::Error` is `!UnwindSafe` and `!RefUnwindSafe` -- it likewise doesn't have any visible mutable state, right? Would it be breaking to have `std::io::Error: UnwindSafe + RefUnwindSafe` in a future release?

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [August 13, 2022, 9:07pm UTC](https://internals.rust-lang.org/t/should-backtrace-be-refunwindsafe/17169/5 "2022-08-13T21:07:42Z")

</div>

> [@cole-miller](#):
>
> it likewise doesn't have any visible mutable state, right?

It can wrap any arbitrary error value including one not unwind safe.

---

<div class="post-metadata">

**Author:** ![cole-miller](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cole-miller/32/7607_2.png) [@cole-miller](https://internals.rust-lang.org/u/cole-miller)\
**Post date:** [August 13, 2022, 9:15pm UTC](https://internals.rust-lang.org/t/should-backtrace-be-refunwindsafe/17169/6 "2022-08-13T21:15:17Z")

</div>

Ah, right, of course. My mistake!

---

<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:** [August 13, 2022, 10:59pm UTC](https://internals.rust-lang.org/t/should-backtrace-be-refunwindsafe/17169/7 "2022-08-13T22:59:54Z")

</div>

> [@cole-miller](#):
>
> has `unsafe impl Sync`

`Sync => RefUnwindSafe`, _conceptually_, and ditto for `Send` and `UnwindSafe` \[1\]. So yeah, if we have the former but not the latter it's a code smell.

* * *

1. 

```rust
fn my_catch_unwind<R, F : FnOnce()> (
   f: F,
) -> Result<(), Box<dyn Any + Send>>
where
   F : Send,
{
   ::std::thread::scope(|scope| {
       scope.spawn(f).join()
   })
}

```

---

<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:** [November 11, 2022, 11:00pm UTC](https://internals.rust-lang.org/t/should-backtrace-be-refunwindsafe/17169/8 "2022-11-11T23:00:25Z")

</div>

Closed on November 11, 2022, 11:00pm UTC

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