rustasync / runtime

Empowering everyone to build asynchronous software
https://docs.rs/runtime
Apache License 2.0
862 stars 28 forks source link

Panicked at 'the main future has panicked: Canceled' #107

Closed zyctree closed 5 years ago

zyctree commented 5 years ago

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.

zyctree commented 5 years ago

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
    }
}