Skip to content
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

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions dio/lib/src/adapters/io_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ class IOHttpClientAdapter implements HttpClientAdapter {
Stream<Uint8List>? requestStream,
Future<void>? cancelFuture,
) async {
final httpClient = _configHttpClient(options.connectTimeout);
final httpClient = _configHttpClient(
options.connectTimeout,
newClient: options.responseType == ResponseType.stream,
);
final reqFuture = httpClient.openUrl(options.method, options.uri);

late HttpClientRequest request;
Expand All @@ -107,7 +110,15 @@ class IOHttpClientAdapter implements HttpClientAdapter {
}

if (cancelFuture != null) {
cancelFuture.whenComplete(() => request.abort());
cancelFuture.whenComplete(() async {
// Abort request
request.abort();

if (options.responseType == ResponseType.stream) {
// Close the HttpClient, in the stream mode, there is a problem that cannot be canceled
httpClient.close(force: true);
}
});
}

// Set Headers
Expand Down Expand Up @@ -227,7 +238,13 @@ class IOHttpClientAdapter implements HttpClientAdapter {
);
}

HttpClient _configHttpClient(Duration? connectionTimeout) {
HttpClient _configHttpClient(Duration? connectionTimeout,
{bool newClient = false}) {
Comment on lines +241 to +242
Copy link
Member

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).

Copy link
Member

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.

if (newClient) {
final client = _createHttpClient();
Copy link
Member

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?

return client..connectionTimeout = connectionTimeout;
}

return (_cachedHttpClient ??= _createHttpClient())
..connectionTimeout = connectionTimeout;
}
Expand Down