MrGunflame / big-gaming

GNU General Public License v3.0
0 stars 0 forks source link

Waking the waker when returning a `Poll::Ready` or waking multiple times triggers UB #328

Closed MrGunflame closed 3 months ago

MrGunflame commented 3 months ago

Waking a waker when returning Poll::Ready causes the task to be polled after being deallocated.

A future that triggers the problem:

fn foo() -> impl Future<Output = ()> {
    poll_fn(|cx| {
        cx.waker().wake_by_ref();
        Poll::Ready(())
    })
}

Waking the waker multiple times causes the task to be scheduled multiple times. This can cause multiple threads to start polling the same task at the same time. It also causes the same task to be deallocated multiple times.

A future that triggers the problem:

fn foo() -> impl Future<Output = ()> {
    poll_fn(|cx| {
        cx.waker().wake_by_ref();
        cx.waker().wake_by_ref();
        Poll::Pending
    })
}