Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dwds/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
is set, the expression compiler worker is started with
`--[no-]deprecated-js-interop`. This requires a Dart SDK that supports the
flag.
- Fix the extension backend and debug service intermittently failing to start
with "Address already in use" by letting the OS pick the port instead of
probing for one on `[::1]` first.

## 27.1.4

Expand Down
28 changes: 13 additions & 15 deletions dwds/lib/src/utilities/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,26 @@ Future<int> findUnusedPort() async {
return port;
}

/// Finds unused port and binds a new http server to it.
/// Binds a new http server to [hostname].
///
/// Retries a few times to recover from errors due to
/// another thread or process opening the same port.
/// Starts by trying to bind to [port] if specified.
/// Tries [port] first if specified, then falls back to port 0 so the OS picks a
/// port that is free for this exact bind. A port from [findUnusedPort] is only
/// known to be free on `[::1]`. It can still be held over IPv4, which makes the
/// dual-stack `[::]` bind used for `'any'` fail with "Address already in use".
///
/// Retries a few times to recover from transient bind errors.
Future<HttpServer> startHttpServer(String hostname, {int? port}) async {
HttpServer? httpServer;
final retries = 5;
var i = 0;
var foundPort = port ?? await findUnusedPort();
while (i < retries) {
i++;
const retries = 5;
var requestedPort = port ?? 0;
for (var attempt = 1; ; attempt++) {
try {
httpServer = await HttpMultiServer.bind(hostname, foundPort);
return await HttpMultiServer.bind(hostname, requestedPort);
} on SocketException {
if (i == retries) rethrow;
if (attempt == retries) rethrow;
}
if (httpServer != null || i == retries) return httpServer!;
foundPort = await findUnusedPort();
requestedPort = 0;
await Future<void>.delayed(const Duration(milliseconds: 100));
}
return httpServer!;
}

/// Handles [requests] using [handler].
Expand Down
32 changes: 32 additions & 0 deletions dwds/test/utilities/server_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2026, 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.

@TestOn('vm')
library;

import 'package:dwds/src/utilities/server.dart';
import 'package:test/test.dart';

void main() {
group('startHttpServer', () {
for (final hostname in ['any', 'localhost']) {
test('binds an OS-assigned port on $hostname', () async {
final server = await startHttpServer(hostname);
addTearDown(server.close);
expect(server.port, isNot(0));
});

test('falls back to another port on $hostname when the requested port is '
'taken', () async {
final holder = await startHttpServer(hostname);
addTearDown(holder.close);

final server = await startHttpServer(hostname, port: holder.port);
addTearDown(server.close);
expect(server.port, isNot(0));
expect(server.port, isNot(holder.port));
});
}
});
}
Loading