-
Notifications
You must be signed in to change notification settings - Fork 47
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
Deprecate Channel.empty/consumeOne #157
Comments
I stumbled over this when adding As a first step though, I would still opt to make |
Opened a PR for fixing |
Enforcing the `tryConsumeOne`/`consumeAll` API avoids potential API misuse in multiple-reader scenarios or when `Channel.close()` is called outside of the reader context.
Enforcing the `tryConsumeOne`/`consumeAll` API avoids potential API misuse in multiple-reader scenarios or when `Channel.close()` is called outside of the reader context.
First off, here's a simple way to consume a channel that's correct (at least at the API level):
The API also has
consumeOne()
andempty()
. I guess this is the obvious way to use them:There's an obvious TOCTOU between the
empty()
and theconsumeOne()
with multiple consumers, as warned by the docs forempty()
:But there's still a TOCTOU in the single-reader case because
empty()
doesn't block if there's nothing in the channel but the channel hasn't been closed yet. Here's a PoC:This crashes with
Task terminated with uncaught exception: Attempt to consume from an empty channel.
I guess
empty()
could be changed to a blocking call, but honestly I think it's best to just deprecate it andconsumeOne()
. As long asempty()
exists, people will be confused into using it like for Phobos ranges, butwhile(tryConsumeOne(x))
is simpler and better.consumeOne()
is only correct in special cases when something like this is better:enforce(tryConsumeOne(x), "Protocol XYZZY requires a single x here")
The text was updated successfully, but these errors were encountered: