Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot start vm_service for a flutter test. #53262

Closed
polina-c opened this issue Aug 17, 2023 · 3 comments
Closed

Cannot start vm_service for a flutter test. #53262

polina-c opened this issue Aug 17, 2023 · 3 comments
Assignees
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends.

Comments

@polina-c
Copy link
Contributor

Steps:

  1. Create counter application.
  2. Add a test file to the folder test with the code below
  3. Run the tests
  4. Observe both tests fail

Copy the test to a dart project and see the test succeed.

The test code:

import 'dart:developer';

import 'package:flutter_test/flutter_test.dart';

Future<Uri> _serviceUri() async {
  Uri? uri = (await Service.getInfo()).serverWebSocketUri;

  if (uri != null) return uri;

  uri = (await Service.controlWebServer(enable: true)).serverWebSocketUri;

  const timeout = Duration(seconds: 5);
  final stopwatch = Stopwatch()..start();

  while (uri == null) {
    if (stopwatch.elapsed > timeout) {
      throw StateError(
        'Could not start VM service.',
      );
    }
    await Future.delayed(const Duration(milliseconds: 1));
    uri = (await Service.getInfo()).serverWebSocketUri;
  }

  return uri;
}

void main() {
  test('Service can be started for flutter test.', () async {
    Uri? uri;

    uri = await _serviceUri();

    print(uri);
  });

  testWidgets('Service can be started for flutter widgets test.',
      (WidgetTester tester) async {
    Uri? uri;

    await tester.runAsync(() async => uri = await _serviceUri());

    print(uri);
  });
}


@mit-mit mit-mit added the area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. label Aug 18, 2023
@mkustermann
Copy link
Member

/cc @bkonyi @derekxu16

@bkonyi bkonyi self-assigned this Aug 18, 2023
@bkonyi
Copy link
Contributor

bkonyi commented Aug 18, 2023

So I've done some digging, and I think this might be a tricky one to resolve.

First off, I've always assumed that invoking Service.controlWebServer with enable = true actually started the VM service isolate if it wasn't already running. As it turns out, that's not actually the case, and the API only controls whether or not the VM service will accept HTTP / WebSocket requests.

Another important point to remember is that the creation of isolates is not controlled completely by the Dart VM, as the embedder is responsible for initializing much of an isolate's state. The service isolate is no exception, and the embedder has a say in how and if the service isolate is initialized.

In the standalone embedder (i.e., dart), the service isolate is always present in configurations that support it (i.e., not PRODUCT mode). The presence of --observe or --enable-vm-service is used to set the value of _autoStart, causing the web server to start here. If the HTTP server is not automatically started, invoking Service.controlWebServer(enable: true) causes the HTTP server to be initialized, making the VM service accessible.

On the other hand, the Flutter engine takes a different approach. Instead of always initializing the service isolate and leaving the HTTP server disabled by default, if --enable-vm-service isn't provided the service isolate simply isn't started. This means that invoking Service.controlWebServer(enable: true) has no effect in Flutter or Flutter tests if --enable-vm-service wasn't provided at launch.

Unfortunately, I don't think resolving this issue will be as simple as always starting the service isolate in the Flutter engine as that will lead to performance regressions in benchmarks that don't explicitly pass --enable-vm-service.

We could potentially change flutter_tester to always start the service isolate in a similar fashion to the standalone VM. This would still impact memory usage (and likely startup times), but would only impact tests instead of all Flutter configurations that support the VM service.

@bkonyi
Copy link
Contributor

bkonyi commented Aug 18, 2023

I'm going to close this issue and move discussion over to flutter/flutter#132865 as this is not a Dart SDK issue.

@bkonyi bkonyi closed this as completed Aug 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends.
Projects
None yet
Development

No branches or pull requests

4 participants