diff --git a/dio/lib/src/adapters/io_adapter.dart b/dio/lib/src/adapters/io_adapter.dart index 65e9ac6cf..b1043716a 100644 --- a/dio/lib/src/adapters/io_adapter.dart +++ b/dio/lib/src/adapters/io_adapter.dart @@ -86,7 +86,10 @@ class IOHttpClientAdapter implements HttpClientAdapter { Stream? requestStream, Future? 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}) { + if (newClient) { + final client = _createHttpClient(); + return client..connectionTimeout = connectionTimeout; + } + return (_cachedHttpClient ??= _createHttpClient()) ..connectionTimeout = connectionTimeout; }