Skip to content

Shared .await affects polling the unrelated Future that is .awaited right after it #2934

@toni-rmc

Description

@toni-rmc

I'm using Windows 10, futures version 0.3.31 and same behavior is observed when using Tokio or async-std.

The following is simple dummy future implementation:

pub struct Dummy(pub u8);

impl Future for Dummy {
    type Output = ();

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        println!("Polling in dummy");
        if self.0 == 0 {
            println!("Dummy is ready");
            Poll::Ready(())
        } else {
            println!("Dummy is not ready");
            self.0 -= 1;
            Poll::Pending
        }
    }
}

Using this in async main e.g. with Tokio of course prints:

Polling in dummy
Dummy is not ready

and blocks.

But if i use Shared before like this:

    let s1 = async {};

    let shared = s1.shared();
    shared.await;

    let dummy = Dummy(1);
    dummy.await;

than dummy is polled twice and completes:

Polling in dummy
Dummy is not ready
Polling in dummy
Dummy is ready

Why is it affected by it?

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