Skip to content

Commit 3997067

Browse files
authored
Fix overflow in TaskRunnerWindow. (#182822) (#5)
Fixes flutter/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 <!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> *Replace this paragraph with a description of what this PR is changing or adding, and why. Consider including before/after screenshots.* *List which issues are fixed by this PR. You must list at least one issue. An issue is not required if the PR fixes something trivial like a typo.* *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 - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] 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
1 parent 2f5fc80 commit 3997067

3 files changed

Lines changed: 16 additions & 10 deletions

File tree

engine/src/flutter/shell/platform/windows/task_runner.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ std::chrono::nanoseconds TaskRunner::ProcessTasks() {
6363
// Calculate duration to sleep for on next iteration.
6464
{
6565
std::lock_guard<std::mutex> lock(task_queue_mutex_);
66-
const auto next_wake = task_queue_.empty() ? TaskTimePoint::max()
67-
: task_queue_.top().fire_time;
68-
69-
return std::min(next_wake - now, std::chrono::nanoseconds::max());
66+
if (task_queue_.empty()) {
67+
return std::chrono::nanoseconds::max();
68+
} else {
69+
return task_queue_.top().fire_time - now;
70+
}
7071
}
7172
}
7273

engine/src/flutter/shell/platform/windows/task_runner_unittests.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ class TestTaskRunnerWindow : public TaskRunnerWindow {
121121
TestTaskRunnerWindow() : TaskRunnerWindow() {}
122122
};
123123

124+
TEST(TaskRunnerTest, EmptyTaskRunnerReturnsNanoSecondsMax) {
125+
TaskRunner runner(MockGetCurrentTime, [](const FlutterTask*) {});
126+
auto res = runner.ProcessTasks();
127+
EXPECT_EQ(res, std::chrono::nanoseconds::max());
128+
}
129+
124130
TEST(TaskRunnerTest, TaskRunnerWindowCoalescesWakeUpMessages) {
125131
class Delegate : public TaskRunnerWindow::Delegate {
126132
public:

engine/src/flutter/shell/platform/windows/task_runner_window.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,11 @@ void TaskRunnerWindow::ProcessTasks() {
185185
}
186186

187187
void TaskRunnerWindow::SetTimer(std::chrono::nanoseconds when) {
188-
if (when == std::chrono::nanoseconds::max()) {
189-
timer_thread_.ScheduleAt(
190-
std::chrono::time_point<std::chrono::high_resolution_clock>::max());
191-
} else {
192-
timer_thread_.ScheduleAt(std::chrono::high_resolution_clock::now() + when);
193-
}
188+
auto now = std::chrono::high_resolution_clock::now();
189+
auto remaining_to_max =
190+
std::chrono::nanoseconds::max() - now.time_since_epoch();
191+
when = std::min(when, remaining_to_max);
192+
timer_thread_.ScheduleAt(now + when);
194193
}
195194

196195
WNDCLASS TaskRunnerWindow::RegisterWindowClass() {

0 commit comments

Comments
 (0)