rust async_error ai_generated true

future cannot be sent between threads safely because `Rc<T>` does not implement `Send`

ID: rust/rust-async-send-bound

Also available as: JSON · Markdown
83%Fix Rate
87%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1 active

Root Cause

Async runtimes like tokio require futures to be Send. Non-Send types (Rc, Cell) held across await points cause this error.

generic

Workarounds

  1. 91% success Replace Rc with Arc and RefCell with Mutex/RwLock
    use std::sync::Arc;
    use tokio::sync::Mutex;
    let shared = Arc::new(Mutex::new(data));

    Sources: https://tokio.rs/tokio/tutorial/shared-state

  2. 80% success Use tokio::task::spawn_local for single-threaded execution
    let local = tokio::task::LocalSet::new();
    local.run_until(async { tokio::task::spawn_local(my_future).await }).await;

    Sources: https://docs.rs/tokio/latest/tokio/task/struct.LocalSet.html

Dead Ends

Common approaches that don't work:

  1. Wrap non-Send type in unsafe impl Send 92% fail

    Data races will occur because the type is intentionally not Send for safety

  2. Use tokio::task::spawn_blocking to bypass Send requirement 65% fail

    spawn_blocking still requires Send for the closure; only the inner code can be non-async

Error Chain

Leads to:
Preceded by:
Frequently confused with: