[flutter_tools] allow adb to fail to unforward without crashing (#57173)

This commit is contained in:
Jonah Williams
2020-05-14 12:27:01 -07:00
committed by GitHub
parent bb49a9b056
commit a801496701
3 changed files with 221 additions and 105 deletions

View File

@@ -214,7 +214,7 @@ class AndroidDevice extends Device {
AdbLogReader _logReader;
AdbLogReader _pastLogReader;
_AndroidDevicePortForwarder _portForwarder;
AndroidDevicePortForwarder _portForwarder;
List<String> adbCommandForDevice(List<String> args) {
return <String>[getAdbPath(globals.androidSdk), '-s', id, ...args];
@@ -697,7 +697,12 @@ class AndroidDevice extends Device {
}
@override
DevicePortForwarder get portForwarder => _portForwarder ??= _AndroidDevicePortForwarder(this);
DevicePortForwarder get portForwarder => _portForwarder ??= AndroidDevicePortForwarder(
processManager: globals.processManager,
logger: globals.logger,
deviceId: id,
adbPath: globals.androidSdk.adbPath,
);
static final RegExp _timeRegExp = RegExp(r'^\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}', multiLine: true);
@@ -1088,10 +1093,22 @@ class AdbLogReader extends DeviceLogReader {
}
}
class _AndroidDevicePortForwarder extends DevicePortForwarder {
_AndroidDevicePortForwarder(this.device);
/// A [DevicePortForwarder] implemented for Android devices that uses adb.
class AndroidDevicePortForwarder extends DevicePortForwarder {
AndroidDevicePortForwarder({
@required ProcessManager processManager,
@required Logger logger,
@required String deviceId,
@required String adbPath,
}) : _deviceId = deviceId,
_adbPath = adbPath,
_logger = logger,
_processUtils = ProcessUtils(logger: logger, processManager: processManager);
final AndroidDevice device;
final String _deviceId;
final String _adbPath;
final Logger _logger;
final ProcessUtils _processUtils;
static int _extractPort(String portString) {
return int.tryParse(portString.trim());
@@ -1103,18 +1120,24 @@ class _AndroidDevicePortForwarder extends DevicePortForwarder {
String stdout;
try {
stdout = processUtils.runSync(
device.adbCommandForDevice(<String>['forward', '--list']),
stdout = _processUtils.runSync(
<String>[
_adbPath,
'-s',
_deviceId,
'forward',
'--list',
],
throwOnError: true,
).stdout.trim();
} on ProcessException catch (error) {
globals.printError('Failed to list forwarded ports: $error.');
_logger.printError('Failed to list forwarded ports: $error.');
return ports;
}
final List<String> lines = LineSplitter.split(stdout).toList();
for (final String line in lines) {
if (!line.startsWith(device.id)) {
if (!line.startsWith(_deviceId)) {
continue;
}
final List<String> splitLine = line.split('tcp:');
@@ -1142,13 +1165,15 @@ class _AndroidDevicePortForwarder extends DevicePortForwarder {
@override
Future<int> forward(int devicePort, { int hostPort }) async {
hostPort ??= 0;
final List<String> forwardCommand = <String>[
'forward',
'tcp:$hostPort',
'tcp:$devicePort',
];
final RunResult process = await processUtils.run(
device.adbCommandForDevice(forwardCommand),
final RunResult process = await _processUtils.run(
<String>[
_adbPath,
'-s',
_deviceId,
'forward',
'tcp:$hostPort',
'tcp:$devicePort',
],
throwOnError: true,
);
@@ -1195,15 +1220,25 @@ class _AndroidDevicePortForwarder extends DevicePortForwarder {
@override
Future<void> unforward(ForwardedPort forwardedPort) async {
final List<String> unforwardCommand = <String>[
'forward',
'--remove',
'tcp:${forwardedPort.hostPort}',
];
await processUtils.run(
device.adbCommandForDevice(unforwardCommand),
throwOnError: true,
final String tcpLine = 'tcp:${forwardedPort.hostPort}';
final RunResult runResult = await _processUtils.run(
<String>[
_adbPath,
'-s',
_deviceId,
'forward',
'--remove',
tcpLine,
],
throwOnError: false,
);
// The port may have already been unforwarded, for example if there
// are multiple attach process already connected.
if (runResult.exitCode == 0 || runResult
.stderr.contains("listener '$tcpLine' not found")) {
return;
}
runResult.throwException('Process exited abnormally:\n$runResult');
}
@override