-
-
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
fix: in the stream mode, request cannot be canceled #1947
Conversation
Thanks for your PR! Do you have any additional information for the root problem? |
import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';
import 'package:dio/dio.dart';
void main() async {
final dio = Dio();
clientStream(dio, '1');
clientStream(dio, '2');
}
Future clientStream(Dio dio, String name) async {
try {
final token = CancelToken();
Timer(const Duration(seconds: 3), () {
token.cancel("----Canceled");
});
const url = "https://hacker-news.firebaseio.com/v0/topstories.json";
Response<ResponseBody> rs = await dio.get<ResponseBody>(
url,
options: Options(
headers: {
"Authorization": 'vhdrjb token"',
"Accept": "text/event-stream",
"Cache-Control": "no-cache",
},
responseType: ResponseType.stream,
), // set responseType to `stream`
cancelToken: token,
);
rs.data?.stream
.transform(unit8Transformer)
.transform(const Utf8Decoder())
.transform(const LineSplitter())
.listen((event) {
if (event.startsWith("event:")) {
print("$name $event isCancelled:${token.isCancelled}");
}
if (event.startsWith("data:")) {
print(
"$name ${event.substring(0, 5)} isCancelled:${token.isCancelled}");
}
});
} catch (e) {
print('error $e');
}
}
StreamTransformer<Uint8List, List<int>> unit8Transformer =
StreamTransformer.fromHandlers(
handleData: (data, sink) {
sink.add(List<int>.from(data));
},
);
Run the code with This PR does not reuse _cachedHttpClient for stream type requests, but creates a new httpclient, and closes the newly created httpclient when using CancelToken. The processing of stream types here is the same as in 5.1.2. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you have a reproducible example, please make it a test, we need tests to avoid further breakages.
HttpClient _configHttpClient(Duration? connectionTimeout, | ||
{bool newClient = false}) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are not well-formatted (with trailing commas).
HttpClient _configHttpClient(Duration? connectionTimeout, | ||
{bool newClient = false}) { | ||
if (newClient) { | ||
final client = _createHttpClient(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we create new clients here, what about the closing after the client has been used?
@@ -227,7 +238,13 @@ class IOHttpClientAdapter implements HttpClientAdapter { | |||
); | |||
} | |||
|
|||
HttpClient _configHttpClient(Duration? connectionTimeout) { | |||
HttpClient _configHttpClient(Duration? connectionTimeout, | |||
{bool newClient = false}) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer forceNotCachedClient
for better understanding, or someone else can have better namings.
I am also wondering if there is an upstream issue for this problem. We |
Maybe this is the source of the problem? dart-lang/sdk#51267 |
Converting this to a draft until we get progress with the implementation and tests. |
Should be covered by #2032. |
New Pull Request Checklist
main
branch to avoid conflicts (via merge from master or rebase)CHANGELOG.md
in the corresponding packageAdditional context and info (if any)