rustasync / runtime

Empowering everyone to build asynchronous software

Home Page:https://docs.rs/runtime

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Panicked at 'the main future has panicked: Canceled'

zyctree opened this issue · comments

The following codes will panick,

#[runtime::main]
async fn main() {
    let () = futures::future::pending().await;
}

it outputs

thread 'main' panicked at 'the main future has panicked: Canceled', src/libcore/result.rs:1084:5

it is the case, where all my codes are running in runtime::spawn,
so i need futures::future::pending() to not exit the whole program

however, i tried it in runtime_tokio, it didn't panick

#[runtime::main(runtime_tokio::Tokio)]
async fn main() {
    let () = futures::future::pending().await;
}

it succeeds to loop infinitely.

The problem may be about runtime::enter,

    let (tx, rx) = futures::channel::oneshot::channel();

    let fut = async move {
        let t = fut.await;
        let _ = tx.send(t);
    };

in this case, fut is never ready, but tx dropped, and rx got a Cancel

one solution is to clone the waker to Pending, like

struct EndlessPending(Option<Waker>);

impl Future for EndlessPending {
    type Output = ();
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
        self.as_mut().0 = Some(cx.waker().clone());
        Poll::Pending
    }
}