-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Make CancelToken
be able to cancel response streams from ResponseType.stream
requests
#2025
Comments
I agree, thanks for the reproduction. I think I have a solution but testing this is kinda hard. |
I've been investigated about the cause of the issue. When we request stream responses, the response body returns immediately. dio/dio/lib/src/transformers/sync_transformer.dart Lines 60 to 63 in bedcc54
When you get the response, the request flow is finished. Nothing in the queue was still awaiting, thus the cancel token is no longer effective (because the request is, by a specific aspect, ended). From the flow, we can say the behavior is as desired. Given the above fact, because you have already obtained the stream, you can use other mechanisms rather than cancel tokens to stop the stream reading. The issue is nothing related to #1953 and #2028. I understand everyone's issue is urgent to themselves, but referencing unrelated issues will cause redundant information for people who subscribed to those issues, and increase the complexibility for maintainers to find the root cause. Still, I've tried to improve this and managed to throw the cancel exception for the stream. |
CancelToken
be able to cancel response streams from ResponseType.stream
requests
* fixes #2025 This is a new approach that handles both IO & HTTP/2 adapters. I also moved the `receiveTimeout` handling for IO & HTTP2 to the new helper. ### New Pull Request Checklist - [x] I have read the [Documentation](https://pub.dev/documentation/dio/latest/) - [x] I have searched for a similar pull request in the [project](https://github.com/cfug/dio/pulls) and found none - [x] I have updated this branch with the latest `main` branch to avoid conflicts (via merge from master or rebase) - [x] I have added the required tests to prove the fix/feature I'm adding - [x] I have updated the documentation (if necessary) - [x] I have run the tests without failures - [x] I have updated the `CHANGELOG.md` in the corresponding package ### Additional context and info (if any) <!-- Provide more context and info about the PR. --> --------- Co-authored-by: Alex Li <[email protected]>
Package
dio
Version
5.3.3
Operating-System
Linux
Output of
flutter doctor -v
Dart Version
3.1.5
Steps to Reproduce
Run: https://github.com/tomekit/dio_cancel_issue_stream_get
or use below
main.dart
file:main.dart
Click middle "Start stream" button to start
streamed
download, then click bottom right: "Cancel" button. Nothing happens in the release mode. In the debug mode your IDE (in my case Android Studio) may (it doesn't happen consistently) capture uncaught exception.That's unexpected behavior, request doesn't get cancelled.
Click bottom left: "Start bytes" button to start
bytes
download, then click bottom right: "Cancel" button. DioException is thrown. That's expected behavior, request cancelling takes place.Expected Result
DioException
shall be thrown withtype
set toDioExceptionType.cancel
even if:responseType: ResponseType.stream
is used.Actual Result
DioException
isn't thrown when request withresponseType: ResponseType.stream
gets cancelled.Test app:
The text was updated successfully, but these errors were encountered: