The symptom#
The test worked yesterday. Someone added one more helper call to it, and now the run ends with no assertion failure and no backtrace. Locally, cargo test prints:
thread 'inline_replays' (2055704) has overflowed its stack
fatal runtime error: stack overflow, aborting
error: test failed, to rerun pass `--test flow`
Caused by:
process didn't exit successfully: `.../flow-aa77e2e226c95e0b inline --quiet` (signal: 6, SIGABRT: process abort signal)
A CI wrapper or test runner often keeps only the last line, such as error: test run failed or SIGABRT, and drops the line that names the stack. When a Rust test aborts without a panic, read the full job log for has overflowed its stack before looking for a logic error. The exit code guide covers pipelines that hide the failing status itself.
Why futures use the stack#
An async fn compiles to a state machine. Each local variable that is still alive at an .await becomes a field of that state machine, and so does the future being awaited. Calling helper().await inside test() therefore puts helper's whole future inside test's future. The compiler can overlap fields that are never alive at the same time, so three sequential awaits do not cost three times as much, but the largest one sets the floor.
Where that future lives depends on how it is run. #[tokio::test] builds a current-thread runtime on the test's own thread and blocks on the test's future, so the future and every poll frame use the test thread's stack. The standard library gives spawned threads 2 MiB of stack unless RUST_MIN_STACK says otherwise, and the test harness runs each test on such a thread. Debug builds are the tightest: without optimization, a future can be moved and copied between stack frames several times while it is created and pinned, so the stack needed is a multiple of the future's size.
The lab#
The lab needs a Rust toolchain and the tokio crate, and runs in under a minute including the build. Each "replay" keeps a 512 KiB buffer alive across an .await, which stands in for a large set of fixtures or a big response struct.
cargo new --lib stack-lab && cd stack-lab
cargo add tokio --features rt,macros
mkdir tests
// tests/flow.rs
async fn replay(tag: u8) -> u64 {
let evidence = [tag; 512 * 1024];
tokio::task::yield_now().await;
evidence.iter().map(|b| u64::from(*b)).sum()
}
async fn flow_inline() -> u64 {
let a = replay(1).await;
let b = replay(2).await;
let c = replay(3).await;
a + b + c
}
async fn flow_boxed() -> u64 {
let a = Box::pin(replay(1)).await;
let b = Box::pin(replay(2)).await;
let c = Box::pin(replay(3)).await;
a + b + c
}
#[test]
fn future_sizes() {
println!("replay: {} bytes", std::mem::size_of_val(&replay(1)));
println!("flow_inline: {} bytes", std::mem::size_of_val(&flow_inline()));
println!("flow_boxed: {} bytes", std::mem::size_of_val(&flow_boxed()));
}
#[tokio::test]
async fn inline_replays() {
assert_eq!(flow_inline().await, 6 * 512 * 1024);
}
#[tokio::test]
async fn boxed_replays() {
assert_eq!(flow_boxed().await, 6 * 512 * 1024);
}
With Rust 1.100 and tokio 1.54 on Linux, the sizes are:
$ cargo test --test flow future_sizes -- --nocapture
replay: 524312 bytes
flow_inline: 524336 bytes
flow_boxed: 32 bytes
The three inline awaits share one slot, so flow_inline is barely larger than one replay. Even so, at about a quarter of the 2 MiB stack, it is enough. cargo test --test flow inline aborts with the stack overflow shown above, and boxed passes. Two more runs confirm the diagnosis rather than fix it: RUST_MIN_STACK=8388608 cargo test --test flow inline passes with an 8 MiB stack, and so does cargo test --release --test flow inline, because the optimizer removes the extra copies.
Clippy can find these futures before they abort anything. The large_futures lint is in the pedantic group and off by default; in the lab, cargo clippy --tests -- -W clippy::large_futures reported each call:
warning: large future with a size of 524312 bytes
--> tests/flow.rs:8:13
|
8 | let a = replay(1).await;
| ^^^^^^^^^ help: consider `Box::pin` on it: `Box::pin(replay(1))`
Its lint documentation describes the threshold, which future-size-threshold in clippy.toml changes.
Choose the fix#
Box::pin allocates the future on the heap and returns a pinned pointer, which is itself a future. The caller's state machine then holds a pointer instead of the whole state, which is why flow_boxed is 32 bytes. The cost is one allocation per call, which is negligible next to a future of this size.
Box at the call that adds the size, not at every await. The lint's output points to them. In a test that strings together large helpers, box each new helper call from the start, because the next field added to a shared struct can push an unboxed call over the limit without touching the test.
The other options treat the symptom:
- A larger stack.
RUST_MIN_STACKor a thread built with a bigger stack size moves the limit, and the next added helper moves past it again. Set it only when you also know why the future is large. - Release builds for tests. They hide the problem in tests and slow every build. Production code built in debug, or a deeper call path, can still overflow.
- Spawning the work.
tokio::spawnstores the task on the heap, but the future is still built on the caller's stack before it moves there, and the task must beSendand'static, which test helpers that borrow fixtures often are not.
Where this came from#
Proofline found this in its own end-to-end investigation tests and shipped the fix in its production release of 22 November 2026. Adding a field to a log-read result made one investigation flow test abort with SIGABRT and fatal runtime error: stack overflow on the test thread, and the test runner's summary showed only that the run had failed. Boxing the test's three direct log-read futures with Box::pin, as an earlier change had done for the workflow step under test, brought it back under the limit. The future sizes were not measured; the 512 KiB figures above come from the lab. Proofline reviews GitHub pull requests; new organizations get one calendar month of hosted code review without a cloud connection. Start a free GitHub review ยท Eligibility and fair-use terms
Questions#
Why does the test pass in release mode?
Optimization removes most of the copies a debug build makes while it creates and polls a future, so the same future needs much less stack. The future is the same size in both.
How do I find which future is large?
Run Clippy with -W clippy::large_futures, or print std::mem::size_of_val(&some_async_fn()) in a test. Creating a future does not run it, so measuring it is cheap.
Does #[tokio::test(flavor = "multi_thread")] avoid the overflow?
Not by itself. The test's own future is still driven by block_on on the test thread, so a large top-level future still uses that thread's stack.