@@ -2219,25 +2219,10 @@ class _ConnectionTarget {
2219
2219
Future socketFuture = task.socket;
2220
2220
final Duration ? connectionTimeout = client.connectionTimeout;
2221
2221
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);
2227
2223
}
2228
2224
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.
2234
2225
_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
- }
2241
2226
socket.setOption (SocketOption .tcpNoDelay, true );
2242
2227
var connection =
2243
2228
new _HttpClientConnection (key, socket, client, false , context);
@@ -2258,6 +2243,20 @@ class _ConnectionTarget {
2258
2243
return new _ConnectionInfo (connection, proxy);
2259
2244
}
2260
2245
}, 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
+ }
2261
2260
_socketTasks.remove (task);
2262
2261
_checkPending ();
2263
2262
throw error;
0 commit comments