From 17bcea458b7f98ffeed96cc89db07a23396489b8 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Wed, 2 Nov 2022 19:14:38 -0700 Subject: [PATCH] Enter a scope before calling Dart APIs in ThrowIfUIOperationsProhibited (flutter/engine#37226) --- engine/src/flutter/lib/ui/ui_dart_state.cc | 1 + engine/src/flutter/testing/dart/isolate_test.dart | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/engine/src/flutter/lib/ui/ui_dart_state.cc b/engine/src/flutter/lib/ui/ui_dart_state.cc index c478c760aa..df5f74bd46 100644 --- a/engine/src/flutter/lib/ui/ui_dart_state.cc +++ b/engine/src/flutter/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/engine/src/flutter/testing/dart/isolate_test.dart b/engine/src/flutter/testing/dart/isolate_test.dart index 87a8081a1a..c3330fb29d 100644 --- a/engine/src/flutter/testing/dart/isolate_test.dart +++ b/engine/src/flutter/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.'); + }); }