From 61de5014b6bc6052984ac7e8ef11f2f171cb7f43 Mon Sep 17 00:00:00 2001 From: Andrew Davies Date: Fri, 27 Apr 2018 13:24:01 -0700 Subject: [PATCH] [frdp] Ignore stale ports. (#16944) When forwarding all ports, run a test connection to each Dart VM instance, stopping port forwarding if the connection fails. Also does the same for any batched calls that hit all Dart VM instances. --- .../examples/list_vms_and_flutter_views.dart | 3 +- .../lib/src/dart/dart_vm.dart | 9 ++- .../lib/src/fuchsia_remote_connection.dart | 73 ++++++++++++++----- .../test/fuchsia_remote_connection_test.dart | 10 ++- 4 files changed, 72 insertions(+), 23 deletions(-) diff --git a/packages/fuchsia_remote_debug_protocol/examples/list_vms_and_flutter_views.dart b/packages/fuchsia_remote_debug_protocol/examples/list_vms_and_flutter_views.dart index 987ec9dfd2..af6dea970a 100644 --- a/packages/fuchsia_remote_debug_protocol/examples/list_vms_and_flutter_views.dart +++ b/packages/fuchsia_remote_debug_protocol/examples/list_vms_and_flutter_views.dart @@ -31,7 +31,8 @@ Future main(List args) async { final String interface = args.length > 1 ? args[1] : ''; // Example ssh config path for the Fuchsia device after having made a local // build. - const String sshConfigPath = '../../out/release-x86-64/ssh-keys/ssh_config'; + const String sshConfigPath = + '../../../fuchsia/out/x64rel/ssh-keys/ssh_config'; final FuchsiaRemoteConnection connection = await FuchsiaRemoteConnection.connect(address, interface, sshConfigPath); print('On $address, the following Dart VM ports are running:'); diff --git a/packages/fuchsia_remote_debug_protocol/lib/src/dart/dart_vm.dart b/packages/fuchsia_remote_debug_protocol/lib/src/dart/dart_vm.dart index 5709adeaa0..0773dd42cb 100644 --- a/packages/fuchsia_remote_debug_protocol/lib/src/dart/dart_vm.dart +++ b/packages/fuchsia_remote_debug_protocol/lib/src/dart/dart_vm.dart @@ -10,7 +10,7 @@ import 'package:web_socket_channel/io.dart'; import '../common/logging.dart'; -const Duration _kConnectTimeout = const Duration(seconds: 30); +const Duration _kConnectTimeout = const Duration(seconds: 9); const Duration _kReconnectAttemptInterval = const Duration(seconds: 3); @@ -41,7 +41,14 @@ Future _waitAndConnect(Uri uri) async { socket = await WebSocket.connect(uri.toString()); peer = new json_rpc.Peer(new IOWebSocketChannel(socket).cast())..listen(); return peer; + } on HttpException catch (e) { + // This is a fine warning as this most likely means the port is stale. + _log.fine('$e: ${e.message}'); + await peer?.close(); + await socket?.close(); + rethrow; } catch (e) { + // Other unknown errors will be handled with reconnects. await peer?.close(); await socket?.close(); if (timer.elapsed < _kConnectTimeout) { diff --git a/packages/fuchsia_remote_debug_protocol/lib/src/fuchsia_remote_connection.dart b/packages/fuchsia_remote_debug_protocol/lib/src/fuchsia_remote_connection.dart index 52c8dafff9..5e6fdaae23 100644 --- a/packages/fuchsia_remote_debug_protocol/lib/src/fuchsia_remote_connection.dart +++ b/packages/fuchsia_remote_debug_protocol/lib/src/fuchsia_remote_connection.dart @@ -113,13 +113,13 @@ class FuchsiaRemoteConnection { /// those objects) will subsequently have its connection closed as well, so /// behavior for them will be undefined. Future stop() async { - for (PortForwarder fp in _forwardedVmServicePorts) { + for (PortForwarder pf in _forwardedVmServicePorts) { // Closes VM service first to ensure that the connection is closed cleanly // on the target before shutting down the forwarding itself. - final DartVm vmService = _dartVmCache[fp.port]; - _dartVmCache[fp.port] = null; + final DartVm vmService = _dartVmCache[pf.port]; + _dartVmCache[pf.port] = null; await vmService?.stop(); - await fp.stop(); + await pf.stop(); } _dartVmCache.clear(); _forwardedVmServicePorts.clear(); @@ -127,18 +127,46 @@ class FuchsiaRemoteConnection { /// Returns a list of [FlutterView] objects. /// - /// This is run across all connected DartVM connections that this class is + /// This is run across all connected Dart VM connections that this class is /// managing. Future> getFlutterViews() async { - final List views = []; if (_forwardedVmServicePorts.isEmpty) { - return views; + return []; } - for (PortForwarder fp in _forwardedVmServicePorts) { - final DartVm vmService = await _getDartVm(fp.port); - views.addAll(await vmService.getAllFlutterViews()); + final List> flutterViewLists = + await _invokeForAllVms>((DartVm vmService) async { + return await vmService.getAllFlutterViews(); + }); + final List results = flutterViewLists.fold>( + [], (List acc, List element) { + acc.addAll(element); + return acc; + }); + return new List.unmodifiable(results); + } + + // Calls all Dart VM's, returning a list of results. + // + // A side effect of this function is that internally tracked port forwarding + // will be updated in the event that ports are found to be broken/stale: they + // will be shut down and removed from tracking. + Future> _invokeForAllVms( + Future vmFunction(DartVm vmService)) async { + final List result = []; + final Set stalePorts = new Set(); + for (PortForwarder pf in _forwardedVmServicePorts) { + try { + final DartVm service = await _getDartVm(pf.port); + result.add(await vmFunction(service)); + } on HttpException { + await pf.stop(); + stalePorts.add(pf.port); + } } - return new List.unmodifiable(views); + // Clean up the ports after finished with iterating. + _forwardedVmServicePorts + .removeWhere((PortForwarder pf) => stalePorts.contains(pf.port)); + return result; } Future _getDartVm(int port) async { @@ -172,6 +200,16 @@ class FuchsiaRemoteConnection { _sshCommandRunner.interface, _sshCommandRunner.sshConfigPath); }))); + + // Filters out stale ports after connecting. Ignores results. + await _invokeForAllVms>( + (DartVm vmService) async { + final Map res = + await vmService.invokeRpc('getVersion'); + _log.fine('DartVM version check result: $res'); + return res; + }, + ); } /// Gets the open Dart VM service ports on a remote Fuchsia device. @@ -291,13 +329,14 @@ class _SshPortForwarder implements PortForwarder { ]); _log.fine("_SshPortForwarder running '${command.join(' ')}'"); final Process process = await _processManager.start(command); + final _SshPortForwarder result = new _SshPortForwarder._(address, + remotePort, localSocket, process, interface, sshConfigPath, isIpV6); process.exitCode.then((int c) { _log.fine("'${command.join(' ')}' exited with exit code $c"); }); - _log.fine( - 'Set up forwarding from ${localSocket.port} to $address port $remotePort'); - return new _SshPortForwarder._(address, remotePort, localSocket, process, - interface, sshConfigPath, isIpV6); + _log.fine('Set up forwarding from ${localSocket.port} ' + 'to $address port $remotePort'); + return result; } /// Kills the SSH forwarding command, then to ensure no ports are forwarded, @@ -328,8 +367,8 @@ class _SshPortForwarder implements PortForwarder { 'Shutting down SSH forwarding with command: ${command.join(' ')}'); final ProcessResult result = await _processManager.run(command); if (result.exitCode != 0) { - _log.warning( - 'Command failed:\nstdout: ${result.stdout}\nstderr: ${result.stderr}'); + _log.warning('Command failed:\nstdout: ${result.stdout}' + '\nstderr: ${result.stderr}'); } _localSocket.close(); } diff --git a/packages/fuchsia_remote_debug_protocol/test/fuchsia_remote_connection_test.dart b/packages/fuchsia_remote_debug_protocol/test/fuchsia_remote_connection_test.dart index b3f9cd79a9..a42c77102a 100644 --- a/packages/fuchsia_remote_debug_protocol/test/fuchsia_remote_connection_test.dart +++ b/packages/fuchsia_remote_debug_protocol/test/fuchsia_remote_connection_test.dart @@ -30,9 +30,9 @@ void main() { const String address = 'fe80::8eae:4cff:fef4:9247'; const String interface = 'eno1'; // Adds some extra junk to make sure the strings will be cleaned up. - when(mockRunner.run(typed(any))) - .thenAnswer((_) => new Future>.value( - ['123\n\n\n', '456 ', '789'])); + when(mockRunner.run(typed(any))).thenAnswer((_) => + new Future>.value( + ['123\n\n\n', '456 ', '789'])); when(mockRunner.address).thenReturn(address); when(mockRunner.interface).thenReturn(interface); int port = 0; @@ -100,8 +100,10 @@ void main() { mockPeerConnections.add(mp); uriConnections.add(uri); when(mp.sendRequest(typed(any), typed(any))) + // The local ports match the desired indices for now, so get the + // canned response from the URI port. .thenAnswer((_) => new Future>( - () => flutterViewCannedResponses[flutterViewIndex++])); + () => flutterViewCannedResponses[uri.port])); return mp; }); }