Skip to content

About asynchronous recursion #7182

Answered by Darksonn
fawdlstty asked this question in Q&A
Feb 27, 2025 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

Unfortunately this is just a bad error message, and Tokio can do nothing to improve it. It's a Rust bug, not a Tokio bug. I've converted your bug report to a discussion so lets discuss workarounds.

The usual workaround is to move the call to tokio::spawn into a non-async function:

async fn test_func() {
    test_func_spawn();
}

fn test_func_spawn() {
    _ = tokio::task::spawn(async move {
        tokio::time::sleep(std::time::Duration::from_secs(1)).await;
        test_func().await;
    });
}

#[tokio::main]
async fn main() {
    test_func().await;
}

or in your case just make test_func non-async:

fn test_func() {
    _ = tokio::task::spawn(async move {
        tokio::time::sleep(std::time

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@fawdlstty
Comment options

@Darksonn
Comment options

Answer selected by mox692
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #7181 on February 27, 2025 08:04.