mirror of
https://github.com/flutter/flutter
synced 2024-08-25 02:46:31 +00:00
Make daemon server work on ipv6-only machines. (#144359)
Retry binding on ipv6 if binding on ipv4 failed.
This commit is contained in:
parent
ee6111a7aa
commit
f3ce9d2fcb
|
@ -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(
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue