-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Stream's timeout
doesn't interrupt asynchronous generator functions.
#59856
Comments
timeout
doesn't doesn't interrupt asynchronous generator functions.timeout
doesn't interrupt asynchronous generator functions.
Moved this to the SDK, although its possible it is actually a language issue. Not sure if the spec specifies this behavior. |
This is specified behavior. The The It's a little anyone that a cancel can't even interrupt an A thing we could do is to give (But changing the API of |
Hi @lrhn, Thank you very much for taking the time to answer me. It makes total sense: we should not interrupt the code. I think the main problem here is: if we are consuming a stream and need to cancel it, we need to know if this stream comes from a StreamController or from an As you suggested, we could have something like: extension CancellableStream<T> on Stream<T> {
Stream<T> toCancellable() {
final controller = StreamController<T>();
final subscription = listen(
controller.add,
onError: controller.addError,
onDone: controller.close,
cancelOnError: true,
);
controller.onCancel = () {
subscription.cancel();
controller.close();
};
return controller.stream;
}
} But yet, not intuitive for those who are not aware of what the problem is. Maybe the best solution is to have something similar to Kotlin's isActive. The user could check if the stream was canceled in the Stream<String> _streamFromGenerator() async* {
for (;;) {
final r1 = await expensiveOperation1();
if (!isActive) {
break;
}
final r2 = await expensiveOperation2();
if (!isActive) {
break;
}
yield "Result: $r1, $r2";
}
} When the subscription is canceled, the stream is "detached" from the At this point, not sure what would be the best intervention. I think it is, at least, worth mentioning this Feel free to close this issue if you believe it is appropriate to do so. |
In a private project that I work on, we are listening to new data from a server through a stream. If no data comes within a few seconds, we need to abort the operation and show an error message to the user.
To achieve this, we tried to use the Stream's timeout function but, to our surprise, no exception is thrown. If the server doesn't emit any new data, the operation gets stuck.
An example code that reproduces this behavior:
The timeout will be ignored and this code will take one day to complete. In our case, it could take forever.
It turned out that a timeout works after the daley is completed. If we change the previous code to:
The output will be:
Note that this exception will only be thrown after 4 seconds.
We managed to implement the expected behavior using StreamController instead of a generator. Still, I think that it is a bug or something that can lead developers to errors.
The text was updated successfully, but these errors were encountered: