A Rust library's patch release keeps an `Rc` alive across an `.await` inside `pub async fn fetch`; why do some downstream builds stop compiling?
answer
- traits nobody wrote in the signature
- the opaque future leaks its contents
- Send decided by what lives across .await
- losing an auto trait is breaking
basics
~20 sSend is an auto trait: the future fetch returns is Send only if everything it holds across .await is Send. An Rc held there removes Send, so callers that need a Send future stop compiling: a breaking change.
solid answer
~50 s`Send`, `Sync`, `Unpin`, `UnwindSafe` and `RefUnwindSafe` are **auto traits**: rustc implements them for a type from what the type contains, and no signature mentions them. An `async fn` returns an opaque `impl Future<Output = usize>`, but callers can still rely on its auto traits, because rustc looks through the opaque type to the state machine. A local `Rc` that lives across an `.await` is stored in that state machine, so the future stops being `Send`, and a caller passing it to anything that requires `Send` (a work-stealing executor's spawn, an `F: Future + Send` bound) gets `future cannot be sent between threads safely`. Losing an auto trait is a breaking change, so it needs a major version or a fix: use `Arc`, or drop the `Rc` before the `.await`. A struct gaining a private `Rc` field breaks callers the same way.
code
rust · 18 linesmod httpc {
use std::rc::Rc;
async fn read_body() -> usize { 42 }
// 1.4.1: a "cache" Rc now lives across the .await
pub async fn fetch(url: &str) -> usize {
let cache = Rc::new(url.len());
let body = read_body().await;
body + *cache
}
}
fn require_send<F: std::future::Future + Send>(f: F) -> F { f }
fn main() {
let _fut = require_send(httpc::fetch("https://example.com"));
}go deeper
Recall that Send and Sync are implemented automatically from a type's fields, and that Rc is not Send.
Explain how an async fn's future captures locals that live across .await, and why that decides whether the future is Send.
Diagnose the downstream break from the rustc note, call it a breaking change, and add compile-time Send assertions or an explicit + Send bound.
Decide which auto-trait promises each public type and future makes, and make CI enforce them so a private refactor cannot silently break users.
## Auto traits are part of the API nobody wrote down Most of a crate's public API is visible in its signatures. **Auto traits** are the exception. The standard library declares five stable ones: `Send`, `Sync` and `Unpin` in `core::marker` (the first two as `unsafe auto trait`), and `UnwindSafe` and `RefUnwindSafe` in `core::panic`. rustc implements an auto trait for a type automatically when all of the type's components implement it. Nothing in the type's declaration says so, and no `impl` block appears. That makes auto traits a **leak**: they depend on private details. If `pub struct Client` gains a private field `cache: Rc<u32>`, `Client` silently stops being `Send` and `Sync`. Code in another crate that sends a `Client` to another thread then fails with `error[E0277]: `Rc<u32>` cannot be sent between threads safely`, with the note `required because it appears within the type `Client``. The library changed no public signature, yet downstream builds break. ## Why an `async fn` leaks too `pub async fn fetch(url: &str) -> usize` really returns an **opaque type**, `impl Future<Output = usize>`. Callers cannot name its concrete type, but rustc lets them use its auto traits: when checking `F: Send`, it looks through the opaque type at the generated state machine. That state machine stores every local that is still alive at an `.await`, because the function must resume with it later. So: - A `Rc` created and still in scope at an `.await` is stored in the future, and the future is **not `Send`**. - rustc's error points at the cause: `future cannot be sent between threads safely`, then `future is not `Send` as this value is used across an await`. - Any caller that needed `Send`, typically to spawn the future on a multi-threaded, work-stealing executor or to satisfy an `F: Future + Send` bound, stops compiling. The rules about which values may cross an `.await` and why spawning needs `Send` futures are covered with `Send` futures and spawning; the point here is that the library's change is a **breaking change**. The same leakage applies to any function returning `impl Trait`, not only `async fn`. ## Classifying the change | Change | Effect on callers | Classification | |---|---|---| | Public type or returned future **loses** `Send`, `Sync` or `Unpin` | code requiring the trait fails | **Major** (or revert the change) | | Public type or returned future **gains** an auto trait | existing code keeps compiling | Compatible in practice | | Private field type changes but auto traits stay the same | none | Not breaking | The 1.4.1 patch therefore has two honest exits: ship 2.0.0, or restore `Send` in 1.4.2 and treat 1.4.1 as a bug. ## Fixing and guarding against it Repairs inside the library: 1. Replace `Rc` with `Arc` when the value really must live across the `.await`. 2. Keep the non-`Send` value in a block that ends before the `.await`, so it is not part of the future's saved state. 3. Hold non-`Send` state outside the future altogether. Guards so the next regression fails in the library's own CI instead of in a user's build: - **Compile-time assertions.** The Rust API Guidelines suggest tests such as `fn assert_send<T: Send>() {}` called as `assert_send::<Client>()`. For a future, take a reference to a value of it: `fn assert_send<T: Send>(_: &T) {}` and `assert_send(&fetch(""))`. - **State the promise in the signature.** Writing `pub fn fetch(url: &str) -> impl Future<Output = usize> + Send` turns the auto trait into a declared bound; rustc then rejects the library itself if the body stops being `Send`. - **Lints.** Clippy's `future_not_send` lint, in the allow-by-default nursery group, flags functions whose returned futures are not `Send`. ## What to look for in review - A new field type in a public struct: `Rc` is neither `Send` nor `Sync`, `Cell` and `RefCell` are not `Sync`, raw pointers are neither. - A new local in a public `async fn` that is still alive at an `.await`. - A function returning `impl Trait` whose body now builds a different concrete type. ## The broader lesson Every public type and every public `async fn` makes an implicit promise about `Send`, `Sync` and `Unpin`. A crate that is used from multi-threaded code should decide which of those promises it intends to keep and test them, because rustc will otherwise let a private refactor change them without a single signature changing.
- How can a Rust library make its own build fail if `fetch`'s future stops being `Send`?Either declare it: return `impl Future<Output = usize> + Send` instead of using `async fn`, so rustc checks the body against that bound; or assert it in a test or private function with `fn assert_send<T: Send>(_: &T) {}` applied to `&fetch("")`. Clippy's allow-by-default `future_not_send` lint can flag it too.
- Does `Unpin` leak the same way as `Send` and `Sync`?Yes. `Unpin` is also an auto trait, so a public type that gains a field which is not `Unpin`, such as `PhantomPinned`, stops being `Unpin` without any signature change, and callers that relied on it, for example by calling `Pin::new` on it, stop compiling.
- Changing a private field can never affect a crate's public API.
- An async fn's future is Send unless its signature says otherwise.
- Losing Send is a patch-level change because no signature changed.
- An Rc inside an async fn is harmless if it is dropped before the function returns.
- Writing impl Send for Client restores the trait safely.