Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Awaiting on WatchStream channel gets output twice #3655

Closed
reza-ebrahimi opened this issue Mar 27, 2021 · 2 comments · Fixed by #3914
Closed

Awaiting on WatchStream channel gets output twice #3655

reza-ebrahimi opened this issue Mar 27, 2021 · 2 comments · Fixed by #3914
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-sync Module: tokio/sync

Comments

@reza-ebrahimi
Copy link

Version

[dependencies]
tokio = { version = "1.1.0", features = ["full"] }
tokio-stream = { version = "0.1.5", features = ["sync"] }

Platform

$ uname -a
Linux dev 5.4.0-67-generic #75~18.04.1-Ubuntu SMP Tue Feb 23 19:17:50 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Description
Awaiting on WatchStream in a loop, will get the last value twice, While were sending once.

Testcode:

#[tokio::main]
async fn main() {
    let (tx, rx) = watch::channel("hello");

    let mut stream = WatchStream::new(rx).map(|payload| {
        println!("{}", payload);
    });

    let task = tokio::spawn(async move {
        loop {
          stream.next().await;
        }
    });

    // Send goodbye just once
    tx.send("goodbye").unwrap();

    task.await.ok();
}

Output:

goodbye
goodbye
@reza-ebrahimi reza-ebrahimi added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Mar 27, 2021
@Darksonn Darksonn added the M-sync Module: tokio/sync label Mar 27, 2021
@Darksonn
Copy link
Contributor

I don't think this can be fixed without exposing new methods on the watch receiver in Tokio. E.g. we might expose a version method. This would involve changing send to increment version before the write lock is released, as it is otherwise not possible to reliably get the current version of the value returned by borrow(). We could also add a method similar to changed() that returns a reference to the actual value.

Thoughts @carllerche ?

@nightkr

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-sync Module: tokio/sync
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants