From 56989441964c12674a688170678e434494ba2d01 Mon Sep 17 00:00:00 2001 From: Jonathan Lau Date: Wed, 3 Jul 2024 07:59:20 +0800 Subject: [PATCH] Fix scheduler event loop being stuck due to task with Priority.idle (#151168) `handleEventLoopCallback()` should return `true` rather than `false` when there are tasks in the queue but the highest priority task is not run because of the `schedulingStrategy`. If `false` is returned when the queue still has tasks, the event loop stops, causing existing and new tasks to never be scheduled. For full context, see https://github.com/flutter/flutter/issues/73766#issuecomment-2202427256. Fixes #73766 and #82016. --- packages/flutter/lib/src/scheduler/binding.dart | 13 +++++-------- packages/flutter/test/scheduler/scheduler_test.dart | 12 ++++++------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/packages/flutter/lib/src/scheduler/binding.dart b/packages/flutter/lib/src/scheduler/binding.dart index 17d927c5aa..af06999b65 100644 --- a/packages/flutter/lib/src/scheduler/binding.dart +++ b/packages/flutter/lib/src/scheduler/binding.dart @@ -517,14 +517,11 @@ mixin SchedulerBinding on BindingBase { /// Execute the highest-priority task, if it is of a high enough priority. /// - /// Returns true if a task was executed and there are other tasks remaining - /// (even if they are not high-enough priority). + /// Returns false if the scheduler is [locked], or if there are no tasks + /// remaining. /// - /// Returns false if no task was executed, which can occur if there are no - /// tasks scheduled, if the scheduler is [locked], or if the highest-priority - /// task is of too low a priority given the current [schedulingStrategy]. - /// - /// Also returns false if there are no tasks remaining. + /// Returns true otherwise, including when no task is executed due to priority + /// being too low. @visibleForTesting @pragma('vm:notify-debugger-on-exception') bool handleEventLoopCallback() { @@ -561,7 +558,7 @@ mixin SchedulerBinding on BindingBase { } return _taskQueue.isNotEmpty; } - return false; + return true; } int _nextFrameCallbackId = 0; // positive diff --git a/packages/flutter/test/scheduler/scheduler_test.dart b/packages/flutter/test/scheduler/scheduler_test.dart index 7afed98aeb..8ed939d735 100644 --- a/packages/flutter/test/scheduler/scheduler_test.dart +++ b/packages/flutter/test/scheduler/scheduler_test.dart @@ -73,13 +73,13 @@ void main() { strategy.allowedPriority = 100; for (int i = 0; i < 3; i += 1) { - expect(scheduler.handleEventLoopCallback(), isFalse); + expect(scheduler.handleEventLoopCallback(), isTrue); } expect(executedTasks.isEmpty, isTrue); strategy.allowedPriority = 50; for (int i = 0; i < 3; i += 1) { - expect(scheduler.handleEventLoopCallback(), i == 0 ? isTrue : isFalse); + expect(scheduler.handleEventLoopCallback(), isTrue); } expect(executedTasks, hasLength(1)); expect(executedTasks.single, equals(80)); @@ -87,7 +87,7 @@ void main() { strategy.allowedPriority = 20; for (int i = 0; i < 3; i += 1) { - expect(scheduler.handleEventLoopCallback(), i < 2 ? isTrue : isFalse); + expect(scheduler.handleEventLoopCallback(), isTrue); } expect(executedTasks, hasLength(2)); expect(executedTasks[0], equals(23)); @@ -99,7 +99,7 @@ void main() { scheduleAddingTask(5); scheduleAddingTask(97); for (int i = 0; i < 3; i += 1) { - expect(scheduler.handleEventLoopCallback(), i < 2 ? isTrue : isFalse); + expect(scheduler.handleEventLoopCallback(), isTrue); } expect(executedTasks, hasLength(2)); expect(executedTasks[0], equals(99)); @@ -108,7 +108,7 @@ void main() { strategy.allowedPriority = 10; for (int i = 0; i < 3; i += 1) { - expect(scheduler.handleEventLoopCallback(), i < 2 ? isTrue : isFalse); + expect(scheduler.handleEventLoopCallback(), isTrue); } expect(executedTasks, hasLength(2)); expect(executedTasks[0], equals(19)); @@ -117,7 +117,7 @@ void main() { strategy.allowedPriority = 1; for (int i = 0; i < 4; i += 1) { - expect(scheduler.handleEventLoopCallback(), i < 3 ? isTrue : isFalse); + expect(scheduler.handleEventLoopCallback(), isTrue); } expect(executedTasks, hasLength(3)); expect(executedTasks[0], equals(5));