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

Documentation of watch::Sender implies that channel is closed forever when all receivers are dropped #4957

Closed
JanBeh opened this issue Aug 30, 2022 · 4 comments · Fixed by #4959
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. M-sync Module: tokio/sync T-docs Topic: documentation

Comments

@JanBeh
Copy link
Contributor

JanBeh commented Aug 30, 2022

Version
1.20.1

Platform
FreeBSD titanium 13.1-RELEASE-p1 FreeBSD 13.1-RELEASE-p1 GENERIC amd64

Description
Documentation of tokio::sync::watch::Sender says:

This method fails if the channel has been closed, which happens when every receiver has been dropped.

This may be interpreted as the method failing if at some point in past the channel has been closed because every receiver has been dropped. This isn't true, however, as the following code demonstrates:

fn main() {
    let (sender, receiver) = tokio::sync::watch::channel(());
    drop(receiver);
    assert!(sender.send(()).is_err());
    let _receiver = sender.subscribe();
    assert!(sender.send(()).is_ok());
}

Perhaps it would be better if it read something like:

This method fails if all receivers have been dropped and no new receiver for the sender has been created.

@JanBeh JanBeh added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Aug 30, 2022
@Darksonn Darksonn added T-docs Topic: documentation M-sync Module: tokio/sync labels Aug 30, 2022
@Darksonn
Copy link
Contributor

Darksonn commented Aug 30, 2022

Well, if you want to be precise, your suggestion implies that it cannot ever be closed once a sender has been created.

@JanBeh
Copy link
Contributor Author

JanBeh commented Aug 30, 2022

Well, if you want to be precise, your suggestion implies that it cannot ever be closed once a sender has been created.

Whether it can be closed or not depends on the meaning of the word, I would say. You may say it can be "closed", but then it can be "reopened".

My problem is that it's unclear how "the channel has been closed" is to be interpreted in English. It could be interpreted as "the channel was closed at some point in past" or as "the channel has been closed and is still in closed state". It's somewhat ambiguous due to different possible interpretations of the perfect tense, I would say.

Hence I would suggest clarifying it.

@Darksonn
Copy link
Contributor

I mean, I agree that we should clarify it.

@JanBeh
Copy link
Contributor Author

JanBeh commented Aug 30, 2022

Perhaps it should also be clarified what "failing" means (i.e. just returning Err or also failing to update the value). Currently the value doesn't get updated (see this comment to #4958). I would prefer if the Sender stored the value for future subscribers. In either case, the behavior could be better documented.

JanBeh added a commit to JanBeh/tokio that referenced this issue Aug 30, 2022
Current documentation of `sync::watch::Sender::send` may be intepreted
as the method failing if at some point in past the channel has been
closed because every receiver has been dropped. This isn't true,
however, as the channel could have been reopened by using
`sync::watch::Sender::subscribe`.

This fix clarifies the behavior. Moreover, it is noted that on failure,
the value isn't made available to future subscribers (but returned as
part of the `SendError`).

Fixes tokio-rs#4957.
JanBeh added a commit to JanBeh/tokio that referenced this issue Aug 30, 2022
sync: doc of watch::Sender::send improved

Current documentation of `sync::watch::Sender::send` may be intepreted
as the method failing if at some point in past the channel has been
closed because every receiver has been dropped. This isn't true,
however, as the channel could have been reopened by using
`sync::watch::Sender::subscribe`.

This fix clarifies the behavior. Moreover, it is noted that on failure,
the value isn't made available to future subscribers (but returned as
part of the `SendError`), and that the other send methods
(`send_if_modified`, `send_modify`, or `send_replace`) can be used to
always make a new value available for future receivers, even if no
receiver currently exists.

Fixes tokio-rs#4957.
Darksonn pushed a commit that referenced this issue Sep 1, 2022
Current documentation of `sync::watch::Sender::send` may be intepreted
as the method failing if at some point in past the channel has been
closed because every receiver has been dropped. This isn't true,
however, as the channel could have been reopened by using
`sync::watch::Sender::subscribe`.

This fix clarifies the behavior. Moreover, it is noted that on failure,
the value isn't made available to future subscribers (but returned as
part of the `SendError`).

Fixes #4957.
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 T-docs Topic: documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants