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.
This commit is contained in:
Jonathan Lau
2024-07-03 07:59:20 +08:00
committed by GitHub
parent 348fdeebb3
commit 5698944196
2 changed files with 11 additions and 14 deletions

View File

@@ -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

View File

@@ -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));