Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tokio::sync::Notify is not Unwind Safe #5122

Closed
zicklag opened this issue Oct 24, 2022 · 1 comment · Fixed by #5154
Closed

tokio::sync::Notify is not Unwind Safe #5122

zicklag opened this issue Oct 24, 2022 · 1 comment · Fixed by #5154
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-sync Module: tokio/sync

Comments

@zicklag
Copy link

zicklag commented Oct 24, 2022

Version
1.21.2

Platform
Rust playground and Linux, Ubuntu 20.04.

Description
I was trying to catch_unwind in a project depending on Tokio's tokio::sync::Notify primitive, and ran into a compile error because Notify is not unwind safe.

I'm not sure if this is technically a bug or a feature request.

I tried this code:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c1409cc70ceea71c7d28c41f7232e19c

#[tokio::main]
async fn main() {
    let notify = tokio::sync::Notify::new();
    
    std::panic::catch_unwind(|| {
        notify.notify_one();
    });
}

I expected to see this happen: No compile error

Instead, this happened:

Compiling playground v0.0.1 (/playground)
error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the type `UnsafeCell<AtomicUsize>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
 --> src/main.rs:5:5
  |
5 |     std::panic::catch_unwind(|| {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^ `UnsafeCell<AtomicUsize>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
  |
  = help: within `Notify`, the trait `RefUnwindSafe` is not implemented for `UnsafeCell<AtomicUsize>`
  = note: required because it appears within the type `tokio::loom::std::atomic_usize::AtomicUsize`
  = note: required because it appears within the type `Notify`
  = note: required because of the requirements on the impl of `UnwindSafe` for `&Notify`
note: required because it's used within this closure
 --> src/main.rs:5:30
  |
5 |     std::panic::catch_unwind(|| {
  |                              ^^
note: required by a bound in `catch_unwind`

error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the type `UnsafeCell<tokio::util::linked_list::LinkedList<tokio::sync::notify::Waiter, tokio::sync::notify::Waiter>>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
 --> src/main.rs:5:5
  |
5 |     std::panic::catch_unwind(|| {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^ `UnsafeCell<tokio::util::linked_list::LinkedList<tokio::sync::notify::Waiter, tokio::sync::notify::Waiter>>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
  |
  = help: within `Notify`, the trait `RefUnwindSafe` is not implemented for `UnsafeCell<tokio::util::linked_list::LinkedList<tokio::sync::notify::Waiter, tokio::sync::notify::Waiter>>`
  = note: required because it appears within the type `lock_api::mutex::Mutex<parking_lot::raw_mutex::RawMutex, tokio::util::linked_list::LinkedList<tokio::sync::notify::Waiter, tokio::sync::notify::Waiter>>`
  = note: required because it appears within the type `tokio::loom::std::parking_lot::Mutex<tokio::util::linked_list::LinkedList<tokio::sync::notify::Waiter, tokio::sync::notify::Waiter>>`
  = note: required because it appears within the type `Notify`
  = note: required because of the requirements on the impl of `UnwindSafe` for `&Notify`
note: required because it's used within this closure
 --> src/main.rs:5:30
  |
5 |     std::panic::catch_unwind(|| {
  |                              ^^
note: required by a bound in `catch_unwind`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to 2 previous errors
@zicklag zicklag added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Oct 24, 2022
@Darksonn Darksonn added the M-sync Module: tokio/sync label Oct 25, 2022
@Darksonn
Copy link
Contributor

Sure, we can do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-sync Module: tokio/sync
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants