Skip to content

Commit 238a164

Browse files
lrhnwhesse
authored andcommitted
Fix type bug in dart:io HTTP client code.
This would have been caught if the static type had not been made `Future<dynamic>`. Fixes #44895 BUG= http://dartbug.com/44895 TEST= standalone/io/regress_44895 Change-Id: I237c552fdb42943b395352a7232b34ab5488ac6b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/184261 Reviewed-by: Nate Bosch <[email protected]> Commit-Queue: Lasse R.H. Nielsen <[email protected]>
1 parent cc15e74 commit 238a164

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

sdk/lib/_http/http_impl.dart

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,25 +2219,10 @@ class _ConnectionTarget {
22192219
Future socketFuture = task.socket;
22202220
final Duration? connectionTimeout = client.connectionTimeout;
22212221
if (connectionTimeout != null) {
2222-
socketFuture = socketFuture.timeout(connectionTimeout, onTimeout: () {
2223-
_socketTasks.remove(task);
2224-
task.cancel();
2225-
return null;
2226-
});
2222+
socketFuture = socketFuture.timeout(connectionTimeout);
22272223
}
22282224
return socketFuture.then((socket) {
2229-
// When there is a timeout, there is a race in which the connectionTask
2230-
// Future won't be completed with an error before the socketFuture here
2231-
// is completed with 'null' by the onTimeout callback above. In this
2232-
// case, propagate a SocketException as specified by the
2233-
// HttpClient.connectionTimeout docs.
22342225
_connecting--;
2235-
if (socket == null) {
2236-
assert(connectionTimeout != null);
2237-
throw new SocketException(
2238-
"HTTP connection timed out after ${connectionTimeout}, "
2239-
"host: ${host}, port: ${port}");
2240-
}
22412226
socket.setOption(SocketOption.tcpNoDelay, true);
22422227
var connection =
22432228
new _HttpClientConnection(key, socket, client, false, context);
@@ -2258,6 +2243,20 @@ class _ConnectionTarget {
22582243
return new _ConnectionInfo(connection, proxy);
22592244
}
22602245
}, onError: (error) {
2246+
// When there is a timeout, there is a race in which the connectionTask
2247+
// Future won't be completed with an error before the socketFuture here
2248+
// is completed with a TimeoutException by the onTimeout callback above.
2249+
// In this case, propagate a SocketException as specified by the
2250+
// HttpClient.connectionTimeout docs.
2251+
if (error is TimeoutException) {
2252+
assert(connectionTimeout != null);
2253+
_connecting--;
2254+
_socketTasks.remove(task);
2255+
task.cancel();
2256+
throw SocketException(
2257+
"HTTP connection timed out after ${connectionTimeout}, "
2258+
"host: ${host}, port: ${port}");
2259+
}
22612260
_socketTasks.remove(task);
22622261
_checkPending();
22632262
throw error;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import "dart:io";
6+
7+
void main() {
8+
final client = HttpClient();
9+
client.connectionTimeout = Duration.zero;
10+
// Should not throw a type error.
11+
client.openUrl(
12+
'get',
13+
Uri.parse(
14+
'https://localhost/',
15+
),
16+
);
17+
}

0 commit comments

Comments
 (0)