From b057b5dab7aa77599c7cb64eb599cdd2fd15b68c Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Wed, 15 Aug 2018 15:12:55 -0700 Subject: [PATCH] Wait for Dart VM initialization before the letting the service isolate constructor access the VM object. The service isolate creation callback may occur on a background thread before the call the Dart_Initialize within the DartVM construtor can finish. We store pointers to various snapshots within the DartVM object. These snapshots are necessary for to successfully create the service isolate. The isolate creation callback access the global object within the ForProcessIfInitialized method. This method can return null if the VM object has not been initialized. This leads to the service protocol failing to start in a non-deterministic manner. This patch moves the creation and access of the DartVM object within a critical section. --- runtime/dart_vm.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/runtime/dart_vm.cc b/runtime/dart_vm.cc index c9ee5f920035d..b263914538217 100644 --- a/runtime/dart_vm.cc +++ b/runtime/dart_vm.cc @@ -257,6 +257,7 @@ fml::RefPtr DartVM::ForProcess(Settings settings) { } static std::once_flag gVMInitialization; +static std::mutex gVMMutex; static fml::RefPtr gVM; fml::RefPtr DartVM::ForProcess( @@ -264,6 +265,7 @@ fml::RefPtr DartVM::ForProcess( fml::RefPtr vm_snapshot, fml::RefPtr isolate_snapshot, fml::RefPtr shared_snapshot) { + std::lock_guard lock(gVMMutex); std::call_once(gVMInitialization, [settings, // vm_snapshot, // isolate_snapshot, // @@ -296,6 +298,7 @@ fml::RefPtr DartVM::ForProcess( } fml::RefPtr DartVM::ForProcessIfInitialized() { + std::lock_guard lock(gVMMutex); return gVM; }