Revert "Remove race conditions involving finding available ports (#18488)" (#18521)

This reverts commit 7750872251.
This commit is contained in:
Ian Hickson
2018-06-15 15:37:20 -07:00
committed by GitHub
parent 7750872251
commit 0fa5ba43a1
22 changed files with 242 additions and 226 deletions

View File

@@ -15,6 +15,7 @@ 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';
@@ -843,7 +844,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));
@@ -854,30 +855,16 @@ class _AndroidDevicePortForwarder extends DevicePortForwarder {
}
@override
Future<int> forward(int devicePort, {int hostPort}) async {
hostPort ??= 0;
final RunResult process = await runCheckedAsync(device.adbCommandForDevice(
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(
<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;
}