Skip to content

Futures should be woken before calling Future::poll #73

@yoshuawuyts

Description

@yoshuawuyts

Problem

Following up on #71, we still have some issues. In #70 (comment) @SilverMira provided the following failing test case:

    #[test]
    fn cooperative_concurrency() {
        crate::runtime::block_on(async {
            let cpu_heavy = async move {
                // Simulating a CPU-heavy task that runs for 10 second and yields occasionally
                for _ in 0..100 {
                    std::thread::sleep(std::time::Duration::from_millis(100));
                    futures_lite::future::yield_now().await;
                }
                true
            };
            let timeout = async move {
                crate::time::Timer::after(crate::time::Duration::from_secs(3))
                    .wait()
                    .await;
                false
            };
            let mut future_group = futures_concurrency::future::FutureGroup::<
                Pin<Box<dyn std::future::Future<Output = bool>>>,
            >::new();
            future_group.insert(Box::pin(cpu_heavy));
            future_group.insert(Box::pin(timeout));
            let result = future_group.next().await;
            assert_eq!(result, Some(false), "cpu_heavy task should have timed out");
        });
    }

From talking with @pchickey, it sounds like we are currently not calling the wakers before re-polling, which is causing the test to fail. We should write a patch to fix that.

Proposed Solution

The solution @pchickey is proposing is to add a non-blocking version of Reactor::block_on_pollables which calls wasi::io::poll::poll on every async pollable with a waker registered. We need a variation in the behavior that adds an additional pollable to the wasi poll list, so that wasi::io::poll::poll does not block and immediately returns a ready list, and wakes all of the associated wakers. We should insert that call right below the following line:

https://github.com/yoshuawuyts/wstd/blob/90bb588c4f10030e2ac6fcdb13860d7da1a62b8d/src/runtime/block_on.rs#L40

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions