Skip to content

Commit b65c90a

Browse files
zichanggcommit-bot@chromium.org
authored andcommitted
[dart:io] Fix _connecting counting in HttpClient
Resolves comments in previous cl and fix a problem where _connecting decrements twice. Bug: #34477 Change-Id: Icf5a98f4cf9e3f6ca582fe69f41074d482e00f0e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/148841 Commit-Queue: Zichang Guo <[email protected]> Reviewed-by: Jonas Termansen <[email protected]>
1 parent bd3b329 commit b65c90a

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

sdk/lib/_http/http_impl.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,7 +2081,6 @@ class _ConnectionTarget {
20812081
if (connectionTimeout != null) {
20822082
socketFuture = socketFuture.timeout(connectionTimeout, onTimeout: () {
20832083
_socketTasks.remove(task);
2084-
_connecting--;
20852084
task.cancel();
20862085
return null;
20872086
});
@@ -2092,13 +2091,13 @@ class _ConnectionTarget {
20922091
// is completed with 'null' by the onTimeout callback above. In this
20932092
// case, propagate a SocketException as specified by the
20942093
// HttpClient.connectionTimeout docs.
2094+
_connecting--;
20952095
if (socket == null) {
20962096
assert(connectionTimeout != null);
20972097
throw new SocketException(
20982098
"HTTP connection timed out after ${connectionTimeout}, "
20992099
"host: ${host}, port: ${port}");
21002100
}
2101-
_connecting--;
21022101
socket.setOption(SocketOption.tcpNoDelay, true);
21032102
var connection =
21042103
new _HttpClientConnection(key, socket, client, false, context);
@@ -2119,7 +2118,6 @@ class _ConnectionTarget {
21192118
return new _ConnectionInfo(connection, proxy);
21202119
}
21212120
}, onError: (error) {
2122-
_connecting--;
21232121
_socketTasks.remove(task);
21242122
_checkPending();
21252123
throw error;

sdk_nnbd/lib/_http/http_impl.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,7 +2080,6 @@ class _ConnectionTarget {
20802080
if (connectionTimeout != null) {
20812081
socketFuture = socketFuture.timeout(connectionTimeout, onTimeout: () {
20822082
_socketTasks.remove(task);
2083-
_connecting--;
20842083
task.cancel();
20852084
return null;
20862085
});
@@ -2091,13 +2090,13 @@ class _ConnectionTarget {
20912090
// is completed with 'null' by the onTimeout callback above. In this
20922091
// case, propagate a SocketException as specified by the
20932092
// HttpClient.connectionTimeout docs.
2093+
_connecting--;
20942094
if (socket == null) {
20952095
assert(connectionTimeout != null);
20962096
throw new SocketException(
20972097
"HTTP connection timed out after ${connectionTimeout}, "
20982098
"host: ${host}, port: ${port}");
20992099
}
2100-
_connecting--;
21012100
socket.setOption(SocketOption.tcpNoDelay, true);
21022101
var connection =
21032102
new _HttpClientConnection(key, socket, client, false, context);
@@ -2118,7 +2117,6 @@ class _ConnectionTarget {
21182117
return new _ConnectionInfo(connection, proxy);
21192118
}
21202119
}, onError: (error) {
2121-
_connecting--;
21222120
_socketTasks.remove(task);
21232121
_checkPending();
21242122
throw error;

tests/standalone/io/http_client_connect_test.dart

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -285,19 +285,27 @@ void testMaxConnectionsPerHost(int connectionCap, int connections) {
285285
});
286286
}
287287

288-
void testMaxConnectionsWithFailure() async {
289-
var client = new HttpClient();
288+
Future<void> testMaxConnectionsWithFailure() async {
289+
// When DNS lookup failed, counter for connecting doesn't decrement which
290+
// prevents the following connections.
291+
final client = HttpClient();
290292
client.maxConnectionsPerHost = 1;
291293
try {
292-
await client.getUrl(Uri.parse('http://mydomainismissing'));
293-
} catch (e) {}
294+
await client.getUrl(Uri.parse('http://domain.invalid'));
295+
} catch (e) {
296+
if (e is! SocketException) {
297+
Expect.fail("Unexpected exception $e is thrown");
298+
}
299+
}
294300
try {
295-
await client.getUrl(Uri.parse('http://mydomainismissing'));
301+
await client.getUrl(Uri.parse('http://domain.invalid'));
302+
Expect.fail("Calls exceed client's maxConnectionsPerHost should throw "
303+
"exceptions as well");
296304
} catch (e) {
297-
return;
305+
if (e is! SocketException) {
306+
Expect.fail("Unexpected exception $e is thrown");
307+
}
298308
}
299-
300-
Expect.fail('second call should also fail');
301309
}
302310

303311
void main() {

tests/standalone_2/io/http_client_connect_test.dart

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -283,19 +283,27 @@ void testMaxConnectionsPerHost(int connectionCap, int connections) {
283283
});
284284
}
285285

286-
void testMaxConnectionsWithFailure() async {
287-
var client = new HttpClient();
286+
Future<void> testMaxConnectionsWithFailure() async {
287+
// When DNS lookup failed, counter for connecting doesn't decrement which
288+
// prevents the following connections.
289+
final client = HttpClient();
288290
client.maxConnectionsPerHost = 1;
289291
try {
290-
await client.getUrl(Uri.parse('http://mydomainismissing'));
291-
} catch (e) {}
292+
await client.getUrl(Uri.parse('http://domain.invalid'));
293+
} catch (e) {
294+
if (e is! SocketException) {
295+
Expect.fail("Unexpected exception $e is thrown");
296+
}
297+
}
292298
try {
293-
await client.getUrl(Uri.parse('http://mydomainismissing'));
299+
await client.getUrl(Uri.parse('http://domain.invalid'));
300+
Expect.fail("Calls exceed client's maxConnectionsPerHost should throw "
301+
"exceptions as well");
294302
} catch (e) {
295-
return;
303+
if (e is! SocketException) {
304+
Expect.fail("Unexpected exception $e is thrown");
305+
}
296306
}
297-
298-
Expect.fail('second call should also fail');
299307
}
300308

301309
void main() {

0 commit comments

Comments
 (0)