Repository navigation
[SchedulerBinding] SchedulerBinding.instance.scheduleTask is jammed when Priority is idle #73766
Copy link
Copy link
Closed
Labels
P1High-priority issues at the top of the work listHigh-priority issues at the top of the work lista: animationAnimation APIsAnimation APIscustomer: money (g3)found in release: 3.19Found to occur in 3.19Found to occur in 3.19found in release: 3.21Found to occur in 3.21Found to occur in 3.21frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer versionteam-frameworkOwned by Framework teamOwned by Framework teamtriaged-frameworkTriaged by Framework teamTriaged by Framework team
Description
I found SchedulerBinding.instance.scheduleTask can be stucked in a specific scene.
Here is my example code
const tag = "ScheduleTest";
class ScheduleTestPage1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: Container(
width: 50,
height: 50,
child: CircularProgressIndicator(),
),
),
FlatButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) {
return ScheduleTestPage2();
})).then((value) {
SchedulerBinding.instance.scheduleTask(() {
print("$tag>>>>>>scheduleTask1");
}, Priority.touch);
Future.delayed(Duration(seconds: 1), () {
SchedulerBinding.instance.scheduleTask(() {
print("$tag>>>>>>scheduleTask2");
}, Priority.touch);
});
});
},
child: Text("click me "))
],
),
);
}
}
class ScheduleTestPage2 extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _ScheduleTest2State();
}
}
class _ScheduleTest2State extends State<ScheduleTestPage2> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
width: 100,
height: 50,
child: FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text("Back"),
),
),
),
);
}
@override
void dispose() {
super.dispose();
SchedulerBinding.instance.scheduleTask(() {
print("$tag>>>>>> call ScheduleTestPage2 dispose");
// do something such as Exception . throw Exception("Exception Test");
}, Priority.idle);
}
}
After debug the SchedulerBinding, I found the task was not been executed because of these code.
flutter/packages/flutter/lib/src/scheduler/binding.dart
Lines 1173 to 1177 in 80f15dc
| bool defaultSchedulingStrategy({ required int priority, required SchedulerBinding scheduler }) { | |
| if (scheduler.transientCallbackCount > 0) | |
| return priority >= Priority.animation.value; | |
| return true; | |
| } |
The transientCallbackCount will be increased by Ticker in SingleTickerProviderStateMixin.didChangeDependencies
flutter/packages/flutter/lib/src/scheduler/ticker.dart
Lines 90 to 99 in 80f15dc
| set muted(bool value) { | |
| if (value == muted) | |
| return; | |
| _muted = value; | |
| if (value) { | |
| unscheduleTick(); | |
| } else if (shouldScheduleTick) { | |
| scheduleTick(); | |
| } | |
| } |
I draw a timeline might explain the issue.

Because of _taskQueue.isEmpty is not true, so the remain task will not have chance to be executed :(
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
P1High-priority issues at the top of the work listHigh-priority issues at the top of the work lista: animationAnimation APIsAnimation APIscustomer: money (g3)found in release: 3.19Found to occur in 3.19Found to occur in 3.19found in release: 3.21Found to occur in 3.21Found to occur in 3.21frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer versionteam-frameworkOwned by Framework teamOwned by Framework teamtriaged-frameworkTriaged by Framework teamTriaged by Framework team

Activity
pedromassangocode commentedon Jan 12, 2021
Hi @SixSheeppp

As the docs says:
In your code you have a
CircularProgressIndicatorwhich is aProgressIndicatorbut one of your tasks has a priority bellowanimationwhich means it will only run once the ProgressIndicator is done and that is preventing all your tasks to run.You have to choices: override
schedulingStrategyor change your task withPriority.idleto some priority higher or equals toPriority.animation.I do think the issue is still valid since even the first task with a priority higher than
animationis not running.flutter doctor -v
[✓] Flutter (Channel master, 1.26.0-2.0.pre.295, on Mac OS X 10.15.7 19H2 darwin-x64, locale en) • Flutter version 1.26.0-2.0.pre.295 at /Users/pedromassango/dev/SDKs/flutter_master • Framework revision c19fdad097 (4 hours ago), 2021-01-11 22:19:02 -0500 • Engine revision 0ed964d6b7 • Dart version 2.12.0 (build 2.12.0-212.0.dev) [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2) • Android SDK at /Users/pedromassango/Library/Android/sdk • Platform android-30, build-tools 30.0.2 • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495) • All Android licenses accepted. [!] Xcode - develop for iOS and macOS (Xcode 12.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 12.1, Build version 12A7403 ! CocoaPods 1.9.3 out of date (1.10.0 is recommended). CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side. Without CocoaPods, plugins will not work on iOS or macOS. For more info, see https://flutter.dev/platform-plugins To upgrade see https://guides.cocoapods.org/using/getting-started.html#installation for instructions. [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 4.1) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495) [✓] IntelliJ IDEA Community Edition (version 2020.3.1) • IntelliJ at /Applications/IntelliJ IDEA CE.app • Flutter plugin version 52.1.5 • Dart plugin version 203.6912 [✓] VS Code (version 1.52.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.18.1 [✓] Connected device (3 available) • sdk gphone x86 64 (mobile) • emulator-5554 • android-x64 • Android 11 (API 30) (emulator) • macOS (desktop) • macos • darwin-x64 • Mac OS X 10.15.7 19H2 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 87.0.4280.141 ! Doctor found issues in 1 category. Process finished with exit code 0SixSheeppp commentedon Jan 12, 2021
My current soluation is to change the Task prioiry to
animation.noinskit commentedon Feb 23, 2022
I'm hitting a similar issue - once I schedule a task with Priority.idle in my app, no tasks (even with Priority.animation or Priority.touch) get executed. The schedulingStrategy callback is not even called for them from then on.
Is it the same as this issue?
If the SchedulingBinding scheduler can easily get stuck forever, shouldn't this get a higher priority?
danagbemava-nc commentedon Mar 21, 2024
I see only one output of the
ScheduleTest>>>>>>scheduleTask1on the latest versions of stable & master.Updating labels.
updated sample
flutter doctor -v
jiahaog commentedon Jul 2, 2024
This is affecting
customer: money. See b/348139405 for more details.lauweijie commentedon Jul 2, 2024
I believe this is what is happening:
scheduleTask()starts the event loop by calling_ensureEventLoopCallback()if the task queue is empty (i.e. isFirstTask). The assumption is that if there are existing tasks in the queue, the event loop should still be alive._ensureEventLoopCallback()in turn calls_runTasks().runTasks()callshandleEventLoopCallback()and if the return value istrue, keeps the event loop alive by calling_ensureEventLoopCallback(). HencehandleEventLoopCallback()should only returnfalsewhen there are no more events in the task queue, as the dartdoc suggests.I think the root cause lies with
handleEventLoopCallback()returningfalseeven though there are still tasks in the queue.The highest priority task is returned from the queue. It is passed into
schedulingStrategy()to determine whether the task should run. The default strategy returns false if the priority isPriority.idleandtransientCallbackCount > 0. When this happens, the function returnsfalseand the event loop is stopped, even though the task queue is non-empty.When a new task is later scheduled, even if it now has a high priority,
_ensureEventLoopCallbackis not called because the task queue is non-empty.Henceforth, the event loop is never triggered for the rest of the app lifecycle, which causes subsequent scheduled tasks to no longer be run.
If the analysis above is correct, this issue could be fixed by changing
return false;toreturn true;here.lauweijie commentedon Jul 2, 2024
Duplicate issue: #82016.
There were some attempts there for a PR (#116422) but was abandoned.
jiahaog commentedon Jul 2, 2024
cc @goderbauer who might be able to help here.
jiahaog commentedon Jul 2, 2024
@lauweijie please let us know if the priority here is right as well. If we have a workaround, we can reduce the priority.
Fix scheduler event loop being stuck due to task with Priority.idle (#…
Fix scheduler event loop being stuck due to task with Priority.idle (f…
github-actions commentedon Jul 17, 2024
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.