Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a task in one spawn will prevent the execution of a task in another spawn #6954

Closed
gengfanbin opened this issue Nov 6, 2024 · 1 comment
Closed
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug.

Comments

@gengfanbin
Copy link

gengfanbin commented Nov 6, 2024

Version
List the versions of all tokio crates you are using. The easiest way to get
this information is using cargo tree subcommand:

cargo tree | grep tokio

tokio v1.40.0

Platform
The output of uname -a (UNIX), or version and 32 or 64-bit (Windows)

Windows

Description
Enter your issue details here.
One way to structure the description:

[short summary of the bug]
a task in one spawn will prevent the execution of a task in another spawn

I tried this code:

[code sample that causes the bug]

#[tokio::main]
async fn main() {
    tokio::spawn(async move {
        let handle = std::thread::current();
        println!("Execute thread::{:?}::{:?}", handle.id(), handle.name());
        let mut i = 0;
        loop {
            if i < 2 {
                i = i + 1;
                println!("spawn {i}");
                tokio::spawn(async move {
                    demo(i).await;
                });
            } else { 
                // Only when this spawn is suspended will the output in the “fn demo” be executed
                // tokio::task::yield_now().await; 
            }
        }
    }).await;
}
async fn demo(i: i32) {
    let handle = std::thread::current();
    println!("Execute thread::{}::{:?}::{:?}", i, handle.id(), handle.name());
}

I expected to see this happen: [explanation]
I thought println in the “fn demo” would execute correctly

like it:
Execute thread::ThreadId(6)::Some("tokio-runtime-worker")
spawn 1
spawn 2
Execute thread::2::ThreadId(6)::Some("tokio-runtime-worker")
Execute thread::1::ThreadId(11)::Some("tokio-runtime-worker")

Instead, this happened: [explanation]
But during execution, it was found that the 'fn demo' would be blocked
Execute thread::ThreadId(8)::Some("tokio-runtime-worker")
spawn 1
spawn 2
Execute thread::1::ThreadId(6)::Some("tokio-runtime-worker")

@gengfanbin gengfanbin added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Nov 6, 2024
@Darksonn
Copy link
Contributor

Darksonn commented Nov 6, 2024

Your code is blocking the thread because the loop runs forever without encountering an .await point.

Closing as duplicate of #4941.

@Darksonn Darksonn closed this as not planned Won't fix, can't repro, duplicate, stale Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants