Files
sdk/tests/standalone/io/http_close_stack_overflow_test.dart
T
Zichang Guo 18cfc0a17f [dart:io] Fix HttpClient close() calling itself.
Call close() on HttpClient will close all connections. Whenever a
connection is closed, it will notify HttpClient to close() again, which
is unnecessary. When there are a large amount of servers need to be
closed, it is likely overflow the stack.

Bug: https://github.com/dart-lang/sdk/issues/41247
Change-Id: I62afb1a60d3e4581aa102628e76f717c28031c2f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/141844
Commit-Queue: Zichang Guo <zichangguo@google.com>
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Siva Annamalai <asiva@google.com>
2020-08-28 21:58:08 +00:00

38 lines
1.1 KiB
Dart

// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
// Test that closing a large amount of servers will not lead to a stack
// overflow.
Future<void> main() async {
final max = 10000;
final servers = <ServerSocket>[];
for (var i = 0; i < max; i++) {
final server = await ServerSocket.bind("localhost", 0);
server.listen((Socket socket) {});
servers.add(server);
}
final client = HttpClient();
var got = 0;
for (var i = 0; i < max; i++) {
new Future(() async {
try {
final request = await client
.getUrl(Uri.parse("http://localhost:${servers[i].port}/"));
got++;
if (got == max) {
// Test that no stack overflow happens.
client.close(force: true);
for (final server in servers) {
server.close();
}
}
final response = await request.close();
response.drain();
} on HttpException catch (_) {}
});
}
}