-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Why does Stream.first
need to wait for the subscription cancel?
#34685
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
Comments
First of all, the example should not wait 5 seconds as written. That's an implementation bug - it should deliver its yield value to the stream listener before checking whether it's been cancelled, so you have a window to pause or cancel immediately when you receive an event to prevent the async function from continuing work. That said, you could just put the delay into a finally block, then you'd see the the same behavior even if everybody follows the spec. The reason for waiting for the cancel to finish is really that we prefer any operation that cancels a stream to wait for the stream cancel. It ensures the user that the stream operation is completely done and its resources are released, which makes code less prone to race conditions. You could argue that we should then also propagate an error during the cancel. |
Do we have tracking for it? I'm a little confused by what the next statements mean...
The yield does deliver the value immediately, and at that moment it hasn't been canceled.
I think I understand this to mean that before continuing work after a The rest of the explanation for waiting for the cancel is satisfying to me. Should we close this issue or turn it into a tracking issue for changing the implementation of |
Yes, that is a correct summary, at least for some implementations - I'm not sure all implementations act the same way wrt. The way it's stated, the event of the That's how the language currently specifies the behavior. I'll close this, and check if we have an open issue for the current behavior. |
sdk/sdk/lib/async/stream.dart
Lines 1167 to 1173 in 3dcb5d1
sdk/sdk/lib/async/stream_pipe.dart
Lines 58 to 65 in 3dcb5d1
We don't handle or forward errors from
cancelFuture
- why do we need to wait for it at all before we can return the first value? This leads to some confusing behavior like:This was added when
StreamSubscription.cancel
started returning aFuture
in https://codereview.chromium.org//18915008 - but a scan of the comments didn't help me find the reasoning for APIs likefirst
orelementAt
to wait for the cancelation to be finished.cc @lrhn
The text was updated successfully, but these errors were encountered: