From b68e8dd6daca06ce57279d38e2d1ea23f5e03d57 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Tue, 1 Nov 2022 17:19:24 -0700 Subject: [PATCH] Enter a scope before calling Dart APIs in ThrowIfUIOperationsProhibited Fixes https://github.com/flutter/flutter/issues/114138 --- lib/ui/ui_dart_state.cc | 1 + testing/dart/isolate_test.dart | 11 +++++++++++ 2 files changed, 12 insertions(+) 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.'); + }); }