Skip to content

Commit 6656bd5

Browse files
authored
🔥 Do not detaches sockets / terminates client stream (#2173)
Fixes #2170
1 parent 777b29d commit 6656bd5

6 files changed

Lines changed: 37 additions & 19 deletions

File tree

‎dio/CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ See the [Migration Guide][] for the complete breaking changes list.**
55

66
## Unreleased
77

8-
*None.*
8+
- Remove sockets detach in `IOHttpClientAdapter`.
99

1010
## 5.4.2+1
1111

‎dio/lib/src/adapters/io_adapter.dart‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,6 @@ class IOHttpClientAdapter implements HttpClientAdapter {
212212
.map((e) => RedirectRecord(e.statusCode, e.method, e.location))
213213
.toList(),
214214
statusMessage: responseStream.reasonPhrase,
215-
onClose: () {
216-
responseStream.detachSocket().then((socket) => socket.destroy());
217-
},
218215
);
219216
}
220217

‎dio_test/lib/src/test/cancellation_tests.dart‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,17 @@ void cancellationTests(
113113
),
114114
);
115115
});
116+
117+
// Regression: https://github.com/cfug/dio/issues/2170
118+
test(
119+
'not closing sockets with requests that have same hosts',
120+
() async {
121+
final token = CancelToken();
122+
await expectLater(dio.get('/get', cancelToken: token), completes);
123+
expectLater(dio.get('/drip?duration=3'), completes);
124+
Future.delayed(const Duration(seconds: 1), token.cancel);
125+
},
126+
testOn: '!browser',
127+
);
116128
});
117129
}

‎dio_test/lib/src/test/download_tests.dart‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,12 @@ void downloadTests(
254254
]);
255255
await expectLater(
256256
dio.downloadUri(
257-
Uri.parse('/drip?delay=0&duration=6&numbytes=6').replace(
258-
queryParameters: {'count': '3', 'gap': '2'},
257+
Uri.parse('/drip').replace(
258+
queryParameters: {
259+
'delay': '0',
260+
'duration': '4',
261+
'numbytes': '2',
262+
},
259263
),
260264
p.join(tmp.path, 'download_timeout.md'),
261265
options: Options(receiveTimeout: Duration(seconds: 1)),
@@ -264,10 +268,16 @@ void downloadTests(
264268
);
265269

266270
// Throws nothing if it constantly gets response bytes.
267-
await dio.download(
268-
'https://github.com/cfug/flutter.cn/archive/refs/heads/main.zip',
269-
p.join(tmp.path, 'main.zip'),
270-
options: Options(receiveTimeout: Duration(seconds: 1)),
271+
await dio.downloadUri(
272+
Uri.parse('/drip').replace(
273+
queryParameters: {
274+
'delay': '0',
275+
'duration': '5',
276+
'numbytes': '5',
277+
},
278+
),
279+
p.join(tmp.path, 'download_timeout.md'),
280+
options: Options(receiveTimeout: Duration(seconds: 2)),
271281
);
272282
},
273283
// The download of the main.zip file can be slow,

‎plugins/http2_adapter/CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Unreleased
44

5-
*None.*
5+
- Remove client stream termination in `Http2Adapter`.
66

77
## 2.5.1
88

‎plugins/http2_adapter/lib/src/http2_adapter.dart‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ class Http2Adapter implements HttpClientAdapter {
123123
// Creates a new outgoing stream.
124124
final stream = transport.makeRequest(headers);
125125

126-
cancelFuture?.whenComplete(() {
127-
Future(() {
128-
stream.terminate();
126+
final hasRequestData = requestStream != null;
127+
if (hasRequestData) {
128+
cancelFuture?.whenComplete(() {
129+
stream.outgoingMessages.close();
129130
});
130-
});
131+
}
131132

132133
List<Uint8List>? list;
133-
final hasRequestData = requestStream != null;
134134
if (!excludeMethods.contains(options.method) && hasRequestData) {
135135
list = await requestStream.toList();
136136
requestStream = Stream.fromIterable(list);
@@ -145,7 +145,7 @@ class Http2Adapter implements HttpClientAdapter {
145145
requestStreamFuture = requestStreamFuture.timeout(
146146
sendTimeout,
147147
onTimeout: () {
148-
stream.terminate();
148+
stream.outgoingMessages.close();
149149
throw DioException.sendTimeout(
150150
timeout: sendTimeout,
151151
requestOptions: options,
@@ -195,7 +195,6 @@ class Http2Adapter implements HttpClientAdapter {
195195
);
196196
} else {
197197
responseSubscription.cancel().whenComplete(() {
198-
stream.terminate();
199198
responseSink.close();
200199
});
201200
}
@@ -277,7 +276,7 @@ class Http2Adapter implements HttpClientAdapter {
277276
onClose: () {
278277
responseSubscription.cancel();
279278
responseSink.close();
280-
stream.terminate();
279+
stream.outgoingMessages.close();
281280
},
282281
);
283282
}

0 commit comments

Comments
 (0)