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

panic in select! with insufficient arms #2740

Closed
djmarcin opened this issue Aug 6, 2020 · 2 comments
Closed

panic in select! with insufficient arms #2740

djmarcin opened this issue Aug 6, 2020 · 2 comments
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-macros Module: macros in the main Tokio crate

Comments

@djmarcin
Copy link

djmarcin commented Aug 6, 2020

Version
└── tokio v0.2.22
└── tokio-macros v0.2.5

Platform
macOS 10.15.5

Description
tokio panics when all select! arms have been disabled

I tried this code:

#[tokio::main]
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let (_, mut rx) = tokio::sync::watch::channel("a");
    let join_handle = tokio::spawn(async move {
        loop {
            tokio::select! {
                None = rx.recv() => {
                    return;
                }
            }
        }
    });

    join_handle.await?;

    Ok(())
}

Result:

thread 'tokio-runtime-worker' panicked at 'internal error: entered unreachable code', src/main.rs:5:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: JoinError::Panic(...)

I was thinking that since the async function had returned, the select would do something, and seeing no matched arms would proceed to the next iteration of the loop. Despite this being a potentially faulty mental model of how select! is supposed to work, the "entered unreachable code" bit seemed worthy of a bugrep.

This corrected code does not panic, but requires me to handle all intermediate values on the channel, which I just want to ignore.

#[tokio::main]
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let (_, mut rx) = tokio::sync::watch::channel("a");
    let join_handle = tokio::spawn(async move {
        loop {
            tokio::select! {
                o = rx.recv() => {
                    match o {
                        None => { return; }
                        _ => {}
                    }
                }
            }
        }
    });

    join_handle.await?;

    Ok(())
}
@djmarcin djmarcin added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Aug 6, 2020
@Darksonn Darksonn added the M-macros Module: macros in the main Tokio crate label Aug 7, 2020
@reza-ebrahimi
Copy link

Confirmed. This code also gets the error.

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let rt = Runtime::new().unwrap();
    let (tx1, mut rx1) = mpsc::channel(128);
    let (tx2, mut rx2) = mpsc::channel(128);
    let (tx3, mut rx3) = mpsc::channel(128);

    rt.spawn(async move {
        let _ = tx1.send(thread::current().id()).await;
    });
    rt.spawn(async move {
        let _ = tx2.send(thread::current().id()).await;
    });
    rt.spawn(async move {
        let _ = tx3.send(thread::current().id()).await;
    });

    let tloop = rt.spawn(async move {
        loop {
            let msg = tokio::select! {
                Some(msg) = rx1.recv() => msg,
                Some(msg) = rx2.recv() => msg,
                Some(msg) = rx3.recv() => msg,
            };
            println!("{:?} <= {:?}", thread::current().id(), msg);
        }
    });

    tloop.await.unwrap();
    Ok(())
}
    Finished dev [unoptimized + debuginfo] target(s) in 0.93s
     Running `target/debug/tokio_test`
ThreadId(24) <= ThreadId(24)
ThreadId(24) <= ThreadId(19)
ThreadId(24) <= ThreadId(24)
thread 'tokio-runtime-worker' panicked at 'internal error: entered unreachable code', src/main.rs:74:23
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: JoinError::Panic(...)', src/main.rs:84:14

@Darksonn
Copy link
Contributor

Fixed in #3352.

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. M-macros Module: macros in the main Tokio crate
Projects
None yet
Development

No branches or pull requests

3 participants