Skip to content

Lesson 3 of 9 · 27m

Pinning, Lifetimes, and the Infamous Async Borrow Errors

Decode compiler errors about captured references, self-referential futures, non-Send tasks, and values dropped across await points.

The first time rustc tells you a future is not Send, or that a borrowed value does not live long enough, the error can feel personal. It is not arbitrary compiler noise. Async functions become state machines, and anything alive across an .await becomes part of that machine. Once you see the generated shape, the errors become legible.

Values held across await become state

async fn handle(socket: &mut TcpStream) {
    let mut buf = vec![0; 4096];
    let n = socket.read(&mut buf).await?;
    parse(&buf[..n]);
}

buf and the mutable borrow of socket must remain valid across the await. The future therefore stores enough state to resume correctly. This is why async borrows are more complicated than ordinary function borrows: suspension extends their lifetime.

'static means owned or leaked, not immortal data

tokio::spawn usually requires 'static because the spawned task may outlive the caller. The compiler is right to reject captured references with shorter lifetimes. Move owned data into the task, clone an Arc, or use structured concurrency APIs that tie task lifetime to the parent scope:

async fn launch(data: Arc<Data>) {
    tokio::spawn(async move {
        use_data(&data).await;
    });
}

Inside the task, borrowing the Arc is fine because the task owns the strong reference.

Non-Send errors point at cross-await state

A common error says a future is not Send because some Rc, raw pointer, mutex guard, or non-thread-safe type is held across .await. Standard mutex guards are the classic case:

// bad: guard is alive across the await
let mut guard = shared.lock().unwrap();
guard.counter += 1;
write_log().await;

// better: drop it before suspension
{
    let mut guard = shared.lock().unwrap();
    guard.counter += 1;
}
write_log().await;

With tokio::sync::Mutex, holding a guard across await is allowed but often still a bad idea. It turns asynchronous waiting into lock-holding time and creates deadlocks surprisingly fast.

Pinning exists because futures can be self-referential

Compiler-generated futures may contain internal references from one field to another. Moving such a future after it starts polling could invalidate those references. Pin is Rust's promise that a value will not move once pinned. Most application code sees it only when storing erased futures such as Pin<Box<dyn Future<Output = ()> + Send>>. Treat Pin as memory-safety plumbing, not as a trick to defeat borrow errors; unsafe { Pin::new_unchecked(...) } is almost never the answer.

Read the error bottom-up

Rust async errors often point at a distant spawn call while the real cause is inside the future. Start at the innermost note: which value is borrowed, which guard is held, which await extends its lifetime? Then decide whether to clone, move, scope the borrow, use an async-aware primitive, or redesign the task boundary. Do not reach for Arc<Mutex<T>> reflexively; it can convert a compile-time lifetime problem into a runtime contention problem.

Field notes

  • The exact word Send usually means the task may migrate across worker threads; it is not just about sharing data.
  • Scope temporary borrows in braces before .await when they do not need to survive suspension.

Was this lesson helpful?

Sign in to rate