-
-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 | ||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer |
||
if (newClient) { | ||
final client = _createHttpClient(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
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).