diff --git a/lib/ui/ui_dart_state.cc b/lib/ui/ui_dart_state.cc index c478c760aac74..df5f74bd46c09 100644 --- a/lib/ui/ui_dart_state.cc +++ b/lib/ui/ui_dart_state.cc @@ -97,6 +97,7 @@ void UIDartState::DidSetIsolate() { void UIDartState::ThrowIfUIOperationsProhibited() { if (!UIDartState::Current()->IsRootIsolate()) { + Dart_EnterScope(); Dart_ThrowException( tonic::ToDart("UI actions are only available on root isolate.")); } diff --git a/testing/dart/isolate_test.dart b/testing/dart/isolate_test.dart index 87a8081a1a59c..c3330fb29d601 100644 --- a/testing/dart/isolate_test.dart +++ b/testing/dart/isolate_test.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'dart:isolate'; +import 'dart:ui'; import 'package:litetest/litetest.dart'; @@ -20,4 +21,14 @@ void main() { } expect(threw, true); }); + + test('UI isolate API throws in a background isolate', () async { + void callUiApi(void message) { + PlatformDispatcher.instance.onReportTimings = (_) {}; + } + final ReceivePort errorPort = ReceivePort(); + await Isolate.spawn(callUiApi, null, onError: errorPort.sendPort); + final List isolateError = await errorPort.first as List; + expect(isolateError[0], 'UI actions are only available on root isolate.'); + }); }