rust-lang / rust-lang/rust

Repeated calls to the same `async` fn can cause stack overflow

Open
#132,050 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-async-await C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

#[tokio::main]
async fn main() {
    test_buf().await;
    test_buf().await;
    test_buf().await;
    // ... Execute n times, do not use for loop
}
async fn test_buf(){
    let buf = [0;65536];
    test_buf0(&buf).await;
}
async fn test_buf0(_buf:&[u8]){}

I expected to see this code can work

Instead, this happened:

fatal runtime error: stack overflow

rustc --version --verbose:

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-apple-darwin
release: 1.82.0
LLVM version: 19.1.1

This issue can also be directly reproduced on playground, since the stack size on Linux is larger than that on Windows, the n times that can reproduce the issue is greater than that on Windows

Exited with signal 6 (SIGABRT): abort program

Standard Error

   Compiling playground v0.0.1 (/playground)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.38s
     Running `target/debug/playground`

thread 'main' has overflowed its stack
fatal runtime error: stack overflow

The tokio side think this should be a bug of the compiler, https://github.com/tokio-rs/tokio/issues/5909

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied reproducer or its Rust Playground link and confirm that repeated explicit async calls overflow the stack. Investigate rustc's async-function lowering and determine whether the behavior is compiler-generated; done means identifying the cause and adding a regression test or a confirmed compiler fix.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.