Sub topic 16
Why some futures are not Send
This chapter assumes you know what Send means for threads and that you have read the earlier chapters on state machines and executors.
A future is a struct of its saved locals
The compiler turns an async fn into a state machine. Every local variable that is still alive across an .await becomes a field of that state machine. A struct is Send only if all its fields are Send. So the future's Send-ness depends on what you keep alive across .await points.
A multi-threaded executor can resume a task on a different thread after each .await. That is why spawn functions, such as tokio::spawn, require Future + Send. If the future holds a value that must stay on one thread, moving the future would be unsound.
A failing example
Rc is not Send, because its reference count is not atomic. The helper require_send below stands in for a spawn function.
use std::rc::Rc;
async fn tick() {}
fn require_send<T: Send>(_: T) {}
async fn bad() {
let data = Rc::new(5);
tick().await;
println!("{data}");
}
fn main() {
require_send(bad());
}This does not compile. The error says the future returned by bad "cannot be sent between threads safely", and it points at data, which is "used later" after the .await.
The fix: end the borrow before the await
Only values that live across an .await end up in the state machine. If you end the value's scope first, it is not stored.
use std::rc::Rc;
async fn tick() {}
fn require_send<T: Send>(_: T) {}
async fn good() {
{
let data = Rc::new(5);
println!("{data}");
}
tick().await;
}
fn main() {
require_send(good());
println!("good() is Send");
}Common culprits
| Type | Why it is not `Send` |
|---|---|
| `Rc<T>` | Non-atomic reference count |
| `RefCell` borrow guards (`Ref`, `RefMut`) | Tied to a thread-local borrow flag discipline; the guards hold `&RefCell`, which is not `Sync` |
| `std::sync::MutexGuard` | Must be unlocked on the thread that locked it |
| Raw pointers | No thread-safety guarantee |
Holding a std::sync::MutexGuard across an .await is the most common case in real code. Either drop the guard in an inner block before awaiting, or use an async-aware mutex from your runtime. An async mutex's guard is Send, but holding it across an await can still block other tasks that want the lock.
What the compiler checks
The compiler reasons about which values may be alive at each await point. It uses scopes, not drop(x) calls, for this in many versions, so an inner block is the reliable fix. The error message names the offending variable and the .await that keeps it alive. Read it from the bottom up.
You can also avoid the requirement. Single-threaded executors and tokio::task::spawn_local accept futures that are not Send.
Exercise
A function holds a std::sync::MutexGuard in a local variable, then awaits a network call, then uses the guard again. Passing the future to a spawn function fails with "future cannot be sent between threads safely". Give two ways to fix it.
Show answer
(1) Finish using the guard inside an inner block, so it is dropped before the .await, and lock again afterward if needed. Then the guard is never stored in the future. (2) Replace the std Mutex with an async-aware mutex whose guard is Send. A third option is to run the future on a single-threaded executor or with spawn_local, which does not require Send.