Skip to content

Commit eb4d6c1

Browse files
Gustl22loic-sharmamattkae
authored
[Windows] Fix PostPlatformThreadTask cancelling tasks before they run (flutter#193412)
FlutterDesktopEnginePostPlatformThreadTask aggregate-initialized its Context as a temporary and passed it to std::make_shared, which copied it into the shared allocation. When the temporary was destroyed at the end of the statement, its destructor saw invoked == false and called on_cancel immediately, even though the task was still queued and would run later. Give Context a constructor so std::make_shared builds it in place, and disallow copying it with FML_DISALLOW_COPY_AND_ASSIGN. Any future change that copies the context now fails to compile instead of silently cancelling the task. Add PostPlatformThreadTaskNotCancelledWhenRun, which verifies that on_cancel is called neither when the task is posted nor after it has run and the engine is destroyed. Fixes flutter#193411 For transparency: PR was partially created with Claude. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [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 in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [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]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **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 [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [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 [flutter/website]: https://github.com/flutter/website [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 --------- Co-authored-by: Loïc Sharma <[email protected]> Co-authored-by: Matthew Kosarek <[email protected]>
1 parent edacdd1 commit eb4d6c1

2 files changed

Lines changed: 48 additions & 6 deletions

File tree

‎engine/src/flutter/shell/platform/windows/flutter_windows.cc‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <memory>
1414
#include <vector>
1515

16+
#include "flutter/fml/macros.h"
1617
#include "flutter/shell/platform/common/client_wrapper/include/flutter/plugin_registrar.h"
1718
#include "flutter/shell/platform/common/incoming_message_dispatcher.h"
1819
#include "flutter/shell/platform/common/path_utils.h"
@@ -261,20 +262,24 @@ void FlutterDesktopEnginePostPlatformThreadTask(FlutterDesktopEngineRef engine,
261262
void* user_data) {
262263
FML_DCHECK(callback) << "Callback must not be null";
263264
struct Context {
264-
VoidCallback callback;
265-
VoidCallback on_cancel;
266-
void* user_data;
267-
bool invoked = false;
265+
Context(VoidCallback callback, VoidCallback on_cancel, void* user_data)
266+
: callback(callback), on_cancel(on_cancel), user_data(user_data) {}
268267

269268
~Context() {
270269
if (!invoked && on_cancel) {
271270
on_cancel(user_data);
272271
}
273272
}
273+
274+
VoidCallback callback;
275+
VoidCallback on_cancel;
276+
void* user_data;
277+
bool invoked = false;
278+
279+
FML_DISALLOW_COPY_AND_ASSIGN(Context);
274280
};
275281

276-
auto context =
277-
std::make_shared<Context>(Context{callback, on_cancel, user_data});
282+
auto context = std::make_shared<Context>(callback, on_cancel, user_data);
278283
EngineFromHandle(engine)->task_runner()->PostTask([context]() {
279284
context->invoked = true;
280285
context->callback(context->user_data);

‎engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,43 @@ TEST_F(WindowsTest, PostPlatformThreadTaskCancelledOnEngineDestroy) {
535535
EXPECT_TRUE(captures.cancel_called);
536536
}
537537

538+
// Verify that the cancel callback is not invoked for a task that runs.
539+
TEST_F(WindowsTest, PostPlatformThreadTaskNotCancelledWhenRun) {
540+
auto& context = GetContext();
541+
WindowsConfigBuilder builder(context);
542+
543+
EnginePtr engine{builder.RunHeadless()};
544+
ASSERT_NE(engine, nullptr);
545+
546+
struct Captures {
547+
bool callback_called = false;
548+
bool cancel_called = false;
549+
} captures;
550+
551+
FlutterDesktopEnginePostPlatformThreadTask(
552+
engine.get(),
553+
[](void* user_data) {
554+
static_cast<Captures*>(user_data)->callback_called = true;
555+
},
556+
[](void* user_data) {
557+
static_cast<Captures*>(user_data)->cancel_called = true;
558+
},
559+
&captures);
560+
561+
EXPECT_FALSE(captures.cancel_called);
562+
563+
while (!captures.callback_called) {
564+
PumpMessage();
565+
}
566+
567+
// Ensures that the cancel callback is not called,
568+
// in case the engine was shut down after running the task.
569+
engine.reset();
570+
571+
EXPECT_TRUE(captures.callback_called);
572+
EXPECT_FALSE(captures.cancel_called);
573+
}
574+
538575
// Implicit view has the implicit view ID.
539576
TEST_F(WindowsTest, GetViewId) {
540577
auto& context = GetContext();

0 commit comments

Comments
 (0)