hawkw / mycelium

🍄 an alleged 'operating system'
https://mycelium.elizas.website
MIT License
537 stars 19 forks source link

`WaitCell::wait` does not tolerate spurious polls #449

Closed hawkw closed 1 year ago

hawkw commented 1 year ago

This test fails:

#[test]
fn wait_spurious_poll() {
    use tokio_test::{assert_pending, assert_ready_ok, task};

    let cell = Arc::new(WaitCell::new());
    let mut task = task::spawn({
        let cell = cell.clone();
        async move { cell.wait().await }
    });

    assert_pending!(task.poll(), "first poll should be pending");
    assert_pending!(task.poll(), "second poll should be pending");

    cell.wake();

    assert_ready_ok!(task.poll(), "should have been woken");
}

The second poll should return Poll::Pending, because the WaitCell has not yet been woken. However:

running 1 test
thread 'sync::wait_cell::tests::spurious_poll' panicked at 'ready; value = Ok(()); second poll should be pending', maitake/src/sync/wait_cell.rs:432:9
stack backtrace:
   0: rust_begin_unwind
             at /rustc/7820b62d20bc548096d4632a3487987308cb4b5d/library/std/src/panicking.rs:579:5
   1: core::panicking::panic_fmt
             at /rustc/7820b62d20bc548096d4632a3487987308cb4b5d/library/core/src/panicking.rs:64:14
   2: maitake::sync::wait_cell::tests::spurious_poll
   3: maitake::sync::wait_cell::tests::spurious_poll::{{closure}}
             at ./src/sync/wait_cell.rs:422:24
   4: core::ops::function::FnOnce::call_once
             at /rustc/7820b62d20bc548096d4632a3487987308cb4b5d/library/core/src/ops/function.rs:250:5
   5: core::ops::function::FnOnce::call_once
             at /rustc/7820b62d20bc548096d4632a3487987308cb4b5d/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
test sync::wait_cell::tests::spurious_poll ... FAILED

failures:

failures:
    sync::wait_cell::tests::spurious_poll

Shoutout to @jamesmunns for catching this oen!