Skip to content

Conversation

DSharifi
Copy link
Contributor

@DSharifi DSharifi commented Sep 5, 2025

Motivation

Closes #7281 and supersedes #7556.

changed has a documentation bug: its docs claimed it would error if and only if the channel was closed, but the implementation only returns an error when the channel is closed AND there are no unseen values.

Solution

  • The documentation on fallibility of changed has been updated to reflect its implementation.
  • has_changed's documentation has been updated to better highlight its difference with changed, as these methods fail on different conditions.
  • The watch module documentation has been updated to reflect the difference between changed and has_changed.
  • Integration tests have been added to test the intended behavior of changed and has_changed for closed channels.

@github-actions github-actions bot added the R-loom-sync Run loom sync tests on this PR label Sep 5, 2025
@ADD-SP ADD-SP added A-tokio Area: The main tokio crate M-sync Module: tokio/sync labels Sep 6, 2025
Copy link
Member

@ADD-SP ADD-SP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good to me. I'll wait a few days before merging, in case others are concerned about the behavior change.

@Darksonn
Copy link
Contributor

Darksonn commented Sep 8, 2025

Honestly, I'm somewhat concerned about this. Yes, the watch channel is kind of broken, but it's also used widely and I think lots of people out there rely on all sorts quirks of its behavior. I'm wary of changing it.

@ADD-SP
Copy link
Member

ADD-SP commented Sep 8, 2025

@Darksonn 's concern is valid. At least, we can just fix the incorrect document of changed.

@DSharifi
Copy link
Contributor Author

DSharifi commented Sep 8, 2025

Thanks for the feedback @Darksonn. I understand the concern about breaking changes if downstream users depend on quirks of the watch API.

I'm on the fence if it's a change that is likely of breaking downstream users:

  1. has_changed erroring on unseen values while changed doesn't creates an inconsistent API that might cause confusion and bugs.
  2. For a user to break on the change of has_changed erroring for unseen values, they would need to be calling has_changed repeatedly without ever calling methods that mark values as seen. Effectively using has_changed as a sync check to see if the channel is closed without ever marking values as seen. I do see that we don't have any sync method, for example closed, to explicitly check for channel closures synchronously.

If we're still concerned about breaking the above use case, I'm happy to revise the PR and only keep the documentation fix and regression tests.

@ADD-SP
Copy link
Member

ADD-SP commented Sep 9, 2025

This is precisely why I'm so cautious about adding new features to tokio crate, the bar of changing/fixing the behavior of tokio is significantly higher than tokio-util.

@ADD-SP
Copy link
Member

ADD-SP commented Sep 9, 2025

Given that behavioral changes in tokio is always sensitive, I recommend the following approach. I prioritize backward compatibility and documentation clarity, even if it deviates from ideal principles of interface design.

  • The existing functionality of has_changed should remain unchanged.
  • The document of changed should be fixed to reflect the existing behavior.
  • Highlight different behavior between the has_changed and changed.

Here are my rationales

  • The has_changed currently works as documented. More importantly, downstream may already rely on this specific behavior to detect the closed sender. While this is not a reasonable use case from my perspective, no dedicated method is available even for now, so changing this behavior now would likely introduce breaking changes for them.
  • The current documentation makes it difficult to quickly distinguish between these two methods. We should high light it to help downstream use these interfaces correctly.

@ADD-SP ADD-SP added the S-waiting-on-author Status: awaiting some action (such as code changes) from the PR or issue author. label Sep 11, 2025
@ADD-SP
Copy link
Member

ADD-SP commented Sep 17, 2025

Hi @DSharifi , are you still working on this PR?

@DSharifi
Copy link
Contributor Author

Hi @DSharifi , are you still working on this PR?

Hey @ADD-SP, been a bit busy last week.

I'll update the PR tonight by reverting the behavioral changes, and instead update the documentation like you and @Darksonn suggested in the comments.

@DSharifi DSharifi requested a review from ADD-SP September 19, 2025 21:02
@DSharifi DSharifi changed the title fix: do not return Err(RecvError) in watch::Receiver::has_changed for unseen values docs: update watch documentation has_changed and changed Sep 19, 2025
@DSharifi DSharifi changed the title docs: update watch documentation has_changed and changed docs(watch): update documentation for has_changed and changed Sep 19, 2025
DSharifi and others added 2 commits September 20, 2025 12:58
Copy link
Member

@ADD-SP ADD-SP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ADD-SP ADD-SP changed the title docs(watch): update documentation for has_changed and changed sync: clarify the behavior of tokio::sync::watch::Recevier Sep 20, 2025
@ADD-SP ADD-SP changed the title sync: clarify the behavior of tokio::sync::watch::Recevier sync: clarify the behavior of tokio::sync::watch::Receiver Sep 20, 2025
@ADD-SP ADD-SP merged commit b9b5324 into tokio-rs:master Sep 20, 2025
89 checks passed
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 M-sync Module: tokio/sync R-loom-sync Run loom sync tests on this PR S-waiting-on-author Status: awaiting some action (such as code changes) from the PR or issue author.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tokio::sync::watch::Receiver does not adhere to the specification

3 participants