From 2f3dd2c1e2b3c2b8f4275f4822883bc501ce81a8 Mon Sep 17 00:00:00 2001 From: Andrew Davies Date: Wed, 15 Aug 2018 11:12:30 -0700 Subject: [PATCH] [frdb] Adds function to await Dart VM's (#20597) In the event that no Dart VM's exist or are connectable, will instead await for a new Dart VM to show up. Will no longer return null when calling `getMainIsolatesByPattern`, instead returning either a list of isolates or timing out. --- .../lib/src/fuchsia_remote_connection.dart | 79 +++++++++++++------ 1 file changed, 54 insertions(+), 25 deletions(-) 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 42783eb4c1..e8b2559070 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 @@ -239,29 +239,73 @@ class FuchsiaRemoteConnection { _pollDartVms = false; } + /// Helper method for [getMainIsolatesByPattern]. + /// + /// Called when either there are no Isolates that exist that match + /// `pattern`, or there are not yet any active Dart VM's on the system + /// (possible when the Isolate we're attempting to connect to is in the only + /// instance of the Dart VM and its service port has not yet opened). + Future> _waitForMainIsolatesByPattern([ + Pattern pattern, + Duration timeout = _kIsolateFindTimeout, + ]) async { + final Completer> completer = + new Completer>(); + _onDartVmEvent.listen( + (DartVmEvent event) async { + if (event.eventType == DartVmEventType.started) { + _log.fine('New VM found on port: ${event.servicePort}. Searching ' + 'for Isolate: $pattern'); + final DartVm vmService = await _getDartVm(event.uri.port); + final List result = await vmService + .getMainIsolatesByPattern(pattern) + .timeout(timeout); + if (result.isNotEmpty) { + if (!completer.isCompleted) { + completer.complete(result); + } else { + _log.warning('Found more than one Dart VM containing Isolates ' + 'that match the pattern "$pattern".'); + } + } + } + }, + onDone: () { + if (!completer.isCompleted) { + _log.warning('Terminating isolate search for "$pattern"' + ' before timeout reached.'); + } + }, + ); + return completer.future.timeout(timeout); + } + /// Returns all Isolates running `main()` as matched by the [Pattern]. /// - /// In the current state this is not capable of listening for an - /// Isolate to start up. The Isolate must already be running. + /// If there are no live Dart VM's or the Isolate cannot be found, waits until + /// either `timeout` is reached, or a Dart VM starts up with a name that + /// matches `pattern`. Future> getMainIsolatesByPattern( Pattern pattern, [ Duration timeout = _kIsolateFindTimeout, ]) async { + // If for some reason there are no Dart VM's that are alive, wait for one to + // start with the Isolate in question. if (_dartVmPortMap.isEmpty) { - return null; + _log.fine('No live Dart VMs found. Awaiting new VM startup'); + return _waitForMainIsolatesByPattern(pattern, timeout); } // Accumulate a list of eventual IsolateRef lists so that they can be loaded // simultaneously via Future.wait. final List>> isolates = >>[]; for (PortForwarder fp in _dartVmPortMap.values) { - final DartVm vmService = await _getDartVm(fp.port); + final DartVm vmService = await _getDartVm(fp.port).timeout(timeout); isolates.add(vmService.getMainIsolatesByPattern(pattern)); } - final Completer> completer = - new Completer>(); - final List result = - await Future.wait(isolates).then((List> listOfLists) { + final List result = await Future.wait(isolates) + .timeout(timeout) + .then((List> listOfLists) { final List> mutableListOfLists = new List>.from(listOfLists) ..retainWhere((List list) => list.isNotEmpty); @@ -285,24 +329,9 @@ class FuchsiaRemoteConnection { // TODO(awdavies): Set this up to handle multiple Isolates per Dart VM. if (result.isEmpty) { _log.fine('No instance of the Isolate found. Awaiting new VM startup'); - _onDartVmEvent.listen( - (DartVmEvent event) async { - if (event.eventType == DartVmEventType.started) { - _log.fine('New VM found on port: ${event.servicePort}. Searching ' - 'for Isolate: $pattern'); - final DartVm vmService = await _getDartVm(event.uri.port); - final List result = - await vmService.getMainIsolatesByPattern(pattern); - if (result.isNotEmpty) { - completer.complete(result); - } - } - }, - ); - } else { - completer.complete(result); + return _waitForMainIsolatesByPattern(pattern, timeout); } - return completer.future.timeout(timeout); + return result; } /// Returns a list of [FlutterView] objects.