Remove race conditions involving finding available ports (#18698)

This is an attempt to reland #18488 with less breakage on macOS.
This commit is contained in:
Ian Hickson
2018-06-27 16:44:28 -07:00
committed by GitHub
parent ed2376a7fc
commit 35ad2a786d
25 changed files with 258 additions and 273 deletions

View File

@@ -15,7 +15,6 @@ import '../base/common.dart' show throwToolExit;
import '../base/file_system.dart';
import '../base/io.dart';
import '../base/logger.dart';
import '../base/port_scanner.dart';
import '../base/process.dart';
import '../base/process_manager.dart';
import '../base/utils.dart';
@@ -844,7 +843,7 @@ class _AndroidDevicePortForwarder extends DevicePortForwarder {
final int devicePort = _extractPort(splitLine[2]);
// Failed, skip.
if ((hostPort == null) || (devicePort == null))
if (hostPort == null || devicePort == null)
continue;
ports.add(new ForwardedPort(hostPort, devicePort));
@@ -855,16 +854,30 @@ class _AndroidDevicePortForwarder extends DevicePortForwarder {
}
@override
Future<int> forward(int devicePort, { int hostPort }) async {
if ((hostPort == null) || (hostPort == 0)) {
// Auto select host port.
hostPort = await portScanner.findAvailablePort();
}
await runCheckedAsync(device.adbCommandForDevice(
Future<int> forward(int devicePort, {int hostPort}) async {
hostPort ??= 0;
final RunResult process = await runCheckedAsync(device.adbCommandForDevice(
<String>['forward', 'tcp:$hostPort', 'tcp:$devicePort']
));
if (process.stderr.isNotEmpty)
process.throwException('adb returned error:\n${process.stderr}');
if (process.exitCode != 0) {
if (process.stdout.isNotEmpty)
process.throwException('adb returned error:\n${process.stdout}');
process.throwException('adb failed without a message');
}
if (hostPort == 0) {
if (process.stdout.isEmpty)
process.throwException('adb did not report forwarded port');
hostPort = int.tryParse(process.stdout) ?? (throw 'adb returned invalid port number:\n${process.stdout}');
} else {
if (process.stdout.isNotEmpty)
process.throwException('adb returned error:\n${process.stdout}');
}
return hostPort;
}

View File

@@ -169,10 +169,10 @@ class AndroidWorkflow extends DoctorValidator implements Workflow {
}
Future<LicensesAccepted> get licensesAccepted async {
LicensesAccepted status = LicensesAccepted.unknown;
LicensesAccepted status;
void _onLine(String line) {
if (licenseAccepted.hasMatch(line)) {
if (status == null && licenseAccepted.hasMatch(line)) {
status = LicensesAccepted.all;
} else if (licenseCounts.hasMatch(line)) {
final Match match = licenseCounts.firstMatch(line);
@@ -196,22 +196,22 @@ class AndroidWorkflow extends DoctorValidator implements Workflow {
);
process.stdin.write('n\n');
final Future<void> output = process.stdout
.transform(const Utf8Decoder(allowMalformed: true))
.transform(const LineSplitter())
.listen(_onLine)
.asFuture<void>(null);
.transform(const Utf8Decoder(allowMalformed: true))
.transform(const LineSplitter())
.listen(_onLine)
.asFuture<void>(null);
final Future<void> errors = process.stderr
.transform(const Utf8Decoder(allowMalformed: true))
.transform(const LineSplitter())
.listen(_onLine)
.asFuture<void>(null);
.transform(const Utf8Decoder(allowMalformed: true))
.transform(const LineSplitter())
.listen(_onLine)
.asFuture<void>(null);
try {
await Future.wait<void>(<Future<void>>[output, errors]).timeout(const Duration(seconds: 30));
} catch (TimeoutException) {
printTrace('Intentionally killing ${androidSdk.sdkManagerPath}');
processManager.killPid(process.pid);
}
return status;
return status ?? LicensesAccepted.unknown;
}
/// Run the Android SDK manager tool in order to accept SDK licenses.