Make daemon server work on ipv6-only machines. (#144359)

Retry binding on ipv6 if binding on ipv4 failed.
This commit is contained in:
Lau Ching Jun 2024-02-29 15:10:50 -08:00 committed by GitHub
parent ee6111a7aa
commit f3ce9d2fcb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 115 additions and 5 deletions

View file

@ -73,7 +73,7 @@ class DaemonCommand extends FlutterCommand {
throwToolExit('Invalid port for `--listen-on-tcp-port`: $error');
}
await _DaemonServer(
await DaemonServer(
port: port,
logger: StdoutLogger(
terminal: globals.terminal,
@ -100,12 +100,14 @@ class DaemonCommand extends FlutterCommand {
}
}
class _DaemonServer {
_DaemonServer({
@visibleForTesting
class DaemonServer {
DaemonServer({
this.port,
required this.logger,
this.notifyingLogger,
});
@visibleForTesting Future<ServerSocket> Function(InternetAddress address, int port) bind = ServerSocket.bind,
}) : _bind = bind;
final int? port;
@ -115,8 +117,20 @@ class _DaemonServer {
// Logger that sends the message to the other end of daemon connection.
final NotifyingLogger? notifyingLogger;
final Future<ServerSocket> Function(InternetAddress address, int port) _bind;
Future<void> run() async {
final ServerSocket serverSocket = await ServerSocket.bind(InternetAddress.loopbackIPv4, port!);
ServerSocket? serverSocket;
try {
serverSocket = await _bind(InternetAddress.loopbackIPv4, port!);
} on SocketException {
logger.printTrace('Bind on $port failed with IPv4, retrying on IPv6');
}
// If binding on IPv4 failed, try binding on IPv6.
// Omit try catch here, let the failure fallthrough.
serverSocket ??= await _bind(InternetAddress.loopbackIPv6, port!);
logger.printStatus('Daemon server listening on ${serverSocket.port}');
final StreamSubscription<Socket> subscription = serverSocket.listen(

View file

@ -0,0 +1,96 @@
// Copyright 2014 The Flutter Authors. 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:async';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/commands/daemon.dart';
import 'package:test/fake.dart';
import '../../src/common.dart';
void main() {
testWithoutContext('binds on ipv4 normally', () async {
final FakeServerSocket socket = FakeServerSocket();
final BufferLogger logger = BufferLogger.test();
int bindCalledTimes = 0;
final List<Object?> bindAddresses = <Object?>[];
final List<int> bindPorts = <int>[];
final DaemonServer server = DaemonServer(
port: 123,
logger: logger,
bind: (Object? address, int port) async {
bindCalledTimes++;
bindAddresses.add(address);
bindPorts.add(port);
return socket;
},
);
await server.run();
expect(bindCalledTimes, 1);
expect(bindAddresses, <Object?>[InternetAddress.loopbackIPv4]);
expect(bindPorts, <int>[123]);
});
testWithoutContext('binds on ipv6 if ipv4 failed normally', () async {
final FakeServerSocket socket = FakeServerSocket();
final BufferLogger logger = BufferLogger.test();
int bindCalledTimes = 0;
final List<Object?> bindAddresses = <Object?>[];
final List<int> bindPorts = <int>[];
final DaemonServer server = DaemonServer(
port: 123,
logger: logger,
bind: (Object? address, int port) async {
bindCalledTimes++;
bindAddresses.add(address);
bindPorts.add(port);
if (address == InternetAddress.loopbackIPv4) {
throw const SocketException('fail');
}
return socket;
},
);
await server.run();
expect(bindCalledTimes, 2);
expect(bindAddresses, <Object?>[InternetAddress.loopbackIPv4, InternetAddress.loopbackIPv6]);
expect(bindPorts, <int>[123, 123]);
});
}
class FakeServerSocket extends Fake implements ServerSocket {
FakeServerSocket();
@override
int get port => 1;
bool closeCalled = false;
final StreamController<Socket> controller = StreamController<Socket>();
@override
StreamSubscription<Socket> listen(
void Function(Socket event)? onData, {
Function? onError,
void Function()? onDone,
bool? cancelOnError,
}) {
// Close the controller immediately for testing purpose.
scheduleMicrotask(() {
controller.close();
});
return controller.stream.listen(onData,
onError: onError, onDone: onDone, cancelOnError: cancelOnError);
}
@override
Future<ServerSocket> close() async {
closeCalled = true;
return this;
}
}