Skip to content

[CP-stable][win32] Fix overflow in TaskRunnerWindow.#182864

Merged
auto-submit[bot] merged 1 commit intoflutter:flutter-3.41-candidate.0from
flutteractionsbot:cp-stable-f6b0d4359ba6695b07e2f533375b3ad45a30a00f
Feb 26, 2026
Merged

[CP-stable][win32] Fix overflow in TaskRunnerWindow.#182864
auto-submit[bot] merged 1 commit intoflutter:flutter-3.41-candidate.0from
flutteractionsbot:cp-stable-f6b0d4359ba6695b07e2f533375b3ad45a30a00f

Conversation

@flutteractionsbot
Copy link

@flutteractionsbot flutteractionsbot commented Feb 24, 2026

Issue Link:

#182501

Impact Description:

Flutter Windows apps have high CPU even when idle.

Changelog Description:

flutter/182501 Reduce CPU utilization of idle Flutter Windows apps

Workaround:

None

Risk:

What is the risk level of this cherry-pick?

  • Low
  • Medium
  • High

Test Coverage:

Are you confident that your fix is well-tested by automated tests?

  • Yes
  • No

Validation Steps:

Run the app on a Windows device and check CPU utilization. This issue reproduces especially on low-end devices, or, devices with high CPU utilization.

Fixes flutter#182501

Right now empty `TaskRunner::ProcessTasks()` returns
`TaskTimePoint::max() - now` (relative time). This causes problems,
because the value is later used in `TaskRunnerWindow` to schedule timer
at `std::chrono::high_resolution_clock::now() + relative_time`). This of
course overflows, because the new `now()` in `TaskRunnerWindow` is
higher than the value used to calculate original delta inside
`TaskRunner`.

`TaskRunnerWindow` already had special case where it checks for
`std::chrono::nanoseconds::max()`. In this PR the behavior of
`TaskRunnerWindow::SetTimer` has been updated to check for overflow
rather than excepcting a special value.

*If you had to change anything in the [flutter/tests] repo, include a
link to the migration guide as per the [breaking change policy].*

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
@flutteractionsbot flutteractionsbot added the cp: review Cherry-picks in the review queue label Feb 24, 2026
@flutteractionsbot
Copy link
Author

@loic-sharma please fill out the PR description above, afterwards the release team will review this request.

@flutter-dashboard
Copy link

This pull request was opened from and to a release candidate branch. This should only be done as part of the official Flutter release process. If you are attempting to make a regular contribution to the Flutter project, please close this PR and follow the instructions at Tree Hygiene for detailed instructions on contributing to Flutter.

Reviewers: Use caution before merging pull requests to release branches. Ensure the proper procedure has been followed.

@github-actions github-actions bot added engine flutter/engine related. See also e: labels. platform-windows Building on or for Windows specifically a: desktop Running on desktop labels Feb 24, 2026
@loic-sharma loic-sharma requested a review from knopp February 24, 2026 22:10
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a potential overflow issue in the Windows task runner. The changes involve modifying task_runner.cc to handle empty task queues more safely and updating task_runner_window.cc to prevent overflow when scheduling timers with large delay values. A corresponding unit test is also added to verify the fix for the empty task queue scenario. My review includes a suggestion to improve the robustness and portability of the fix in task_runner_window.cc.

Comment on lines +188 to +192
auto now = std::chrono::high_resolution_clock::now();
auto remaining_to_max =
std::chrono::nanoseconds::max() - now.time_since_epoch();
when = std::min(when, remaining_to_max);
timer_thread_.ScheduleAt(now + when);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While this fix prevents the overflow, its correctness relies on implementation-defined behavior of std::chrono::high_resolution_clock. The calculation std::chrono::nanoseconds::max() - now.time_since_epoch() assumes that now.time_since_epoch() can be safely converted to nanoseconds and that std::chrono::nanoseconds::max() is a reasonable proxy for the maximum duration of high_resolution_clock. This might not be true on all platforms and could lead to precision issues.

A more portable and robust approach is to directly use std::chrono::high_resolution_clock::time_point::max() to determine the scheduling limit. This avoids assumptions about clock implementation details and makes the intent clearer.

Consider this alternative implementation:

  auto now = std::chrono::high_resolution_clock::now();
  auto max_time = std::chrono::high_resolution_clock::time_point::max();
  if (when > max_time - now) {
    timer_thread_.ScheduleAt(max_time);
  } else {
    timer_thread_.ScheduleAt(now + when);
  }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@knopp This seems like a reasonable follow-up improvement, but since you verified this works as expected on Windows I'm fine skipping this for the cherrypick.

@loic-sharma loic-sharma self-requested a review February 25, 2026 00:19
@reidbaker
Copy link
Contributor

reidbaker commented Feb 25, 2026

I have rerun the failing test. If this can get an approval and have passing tests I can land this today midday otherwise it will be in next weeks release.

@reidbaker
Copy link
Contributor

Hey @knopp are you able to give this a lgtm as another domain expert?

@loic-sharma
Copy link
Member

@reidbaker FYI, @knopp created the pull request, I'm the reviewer. (I suspect wires got crossed since I requested the cherry-pick 😄).

@reidbaker reidbaker added the autosubmit Merge PR when tree becomes green via auto submit App label Feb 26, 2026
@reidbaker
Copy link
Contributor

Yeah I didnt catch that Autosubmit added

@auto-submit auto-submit bot merged commit 802fc7d into flutter:flutter-3.41-candidate.0 Feb 26, 2026
162 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 2, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 3, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 3, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: desktop Running on desktop autosubmit Merge PR when tree becomes green via auto submit App cp: review Cherry-picks in the review queue engine flutter/engine related. See also e: labels. platform-windows Building on or for Windows specifically

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants