Repository navigation
Reland "[android]: prewarms shaders when the context is created" - #196549
auto-submit[bot] merged 8 commits into
Conversation
|
fyi @zanderso since you've helped with this |
There was a problem hiding this comment.
Code Review
This pull request ensures that queued reactor operations are flushed when the OpenGL ES resource context is made current on Android, preventing potential deadlocks when pipelines are requested before the context is active. Additionally, default pipeline warming is now restricted to OpenGL ES, avoiding startup regressions on Vulkan. The review feedback highlights a data race in the test's CapturingTaskRunner due to lack of synchronization on tasks_, suggesting the addition of a mutex and the <mutex> header. It also recommends reducing the timeout in the test's IsReady helper to zero seconds to avoid unnecessary test delays.
| class CapturingTaskRunner final : public fml::BasicTaskRunner { | ||
| public: | ||
| void PostTask(const fml::closure& task) override { tasks_.push_back(task); } | ||
|
|
||
| /// Runs tasks, including ones posted while running, until none remain. | ||
| void RunAll() { | ||
| while (!tasks_.empty()) { | ||
| fml::closure task = std::move(tasks_.front()); | ||
| tasks_.pop_front(); | ||
| task(); | ||
| } | ||
| } | ||
|
|
||
| private: | ||
| std::deque<fml::closure> tasks_; | ||
| }; |
There was a problem hiding this comment.
The CapturingTaskRunner is not thread-safe. PostTask is called from the main test thread (when requesting pipelines), while RunAll is called from the io_thread. Since std::deque is not thread-safe, this concurrent access constitutes a data race, which can lead to undefined behavior or flaky tests. Please protect accesses to tasks_ using a std::mutex.
class CapturingTaskRunner final : public fml::BasicTaskRunner {
public:
void PostTask(const fml::closure& task) override {
std::lock_guard<std::mutex> lock(mutex_);
tasks_.push_back(task);
}
/// Runs tasks, including ones posted while running, until none remain.
void RunAll() {
while (true) {
fml::closure task;
{
std::lock_guard<std::mutex> lock(mutex_);
if (tasks_.empty()) {
break;
}
task = std::move(tasks_.front());
tasks_.pop_front();
}
task();
}
}
private:
std::mutex mutex_;
std::deque<fml::closure> tasks_;
};| #include <chrono> | ||
| #include <deque> | ||
| #include <memory> | ||
| #include <optional> |
There was a problem hiding this comment.
| bool IsReady( | ||
| const impeller::PipelineFuture<impeller::PipelineDescriptor>& future) { | ||
| return future.future.wait_for(std::chrono::seconds(1)) == | ||
| std::future_status::ready; | ||
| } |
There was a problem hiding this comment.
Using std::chrono::seconds(1) in IsReady causes EXPECT_FALSE(IsReady(...)) to block the test execution for a full second for each check. Since there are two such checks, this test will always take at least 2 seconds to run. Since the tasks are run synchronously on the io_thread using PostTaskSync, the futures are guaranteed to be ready immediately after the sync tasks complete. We can use a zero-duration timeout (e.g., std::chrono::seconds(0)) to perform a non-blocking check, which will make the test run instantly.
| bool IsReady( | |
| const impeller::PipelineFuture<impeller::PipelineDescriptor>& future) { | |
| return future.future.wait_for(std::chrono::seconds(1)) == | |
| std::future_status::ready; | |
| } | |
| bool IsReady( | |
| const impeller::PipelineFuture<impeller::PipelineDescriptor>& future) { | |
| return future.future.wait_for(std::chrono::seconds(0)) == | |
| std::future_status::ready; | |
| } |
There was a problem hiding this comment.
LGTM. I agree with the gemini bot comments, but I think they're nits. The way these tests currently run, the thread safety concern doesn't actually affect them.
I started some staging runs of the android_hardware_smoke_opengles_instrumented_tests just to confirm that they pass with these changes, I'll add another comment here with status when they finish
https://ci.chromium.org/b/8665220872637041921
https://ci.chromium.org/b/8665220866660467505
https://ci.chromium.org/b/8665220859157590625
|
My review agent is worried that the deadlock is not fixed yet:
The Cycle
If that makes sense, it also has some suggestions for a fix, but I'll hang onto those unless you need them. |
andywolff
left a comment
There was a problem hiding this comment.
Those staging runs I started all got stuck and hung, I agree with @zanderso, this doesn't fix it yet. I think this fixes the problem where shader compilation jobs were scheduled before the EGL context was ready, but it seems there's a remaining deadlock which Zach described
…tter#196069)" This reverts commit 8b6980b.
…O context is current Reproduces flutter#196453. Pipelines requested before the IO thread's resource context is current have their compile jobs' reactor operations stranded on the IO thread, so waiting on them from the raster thread deadlocks.
…ext is current Pipeline compile jobs posted to the IO thread before its resource context is current queue reactor operations that can't run yet. Nothing else is guaranteed to make the IO thread react afterwards, so the raster thread could wait on those pipelines forever. Fixes flutter#196453.
Warming every default pipeline was not a clear win on Vulkan, and may regress startup for apps that only use a few pipelines on their first frame. See flutter#196453.
06c500d to
9917bea
Compare
andywolff
left a comment
There was a problem hiding this comment.
I looked into the remaining deadlock @zanderso pointed out with my agent, and we think replacing weak_io_manager_future.get() in shutdown_safe_io_task_runner_ with a non-blocking ShellIOManager-lifetime guard would resolve it cleanly so IOTaskRunner never blocks waiting for Shell::Setup() on the Platform thread during startup.
You could use some kind of IO state struct: {bool io_manager_initialized = false; fml::WeakPtr<ShellIOManager> weak_io_manager; std::atomic<bool> canceled_before_init{false};}):
Set weak_io_manager and io_manager_initialized = true at the start of ShellSetupIOSubsystem.
Set canceled_before_init = true in ~Shell()'s IO teardown task.
Then in the ConditionalBasicTaskRunner predicate, check canceled_before_init before init and return weak_io_manager after init.
WDYT?
At first I thought we could approach it in a different way, by reordering the setup to avoid the startup wait, but that would deadlock itself in single-threaded tests, and you don't need it if you change the shutdown safe runner to not block on setup like I suggest here
|
Yea, I'm just looking if there is a safer place to put the warmup. That's potentially an easier fix. Sometime between where we have it and when a surface is made it's safe to execute. I just have to figure out where. AI is too keen to start tearing apart threading code. It doesn't have a healthy fear/respect of threading. |
…rce context is current" This reverts commit 66d3994.
…re the IO context is current" This reverts commit 76ff7e1.
The check asserted that the Vulkan swapchain didn't change the context's default color format after the default pipelines were warmed for it. The pipelines are only warmed for OpenGL ES now, where there is no swapchain to change it.
…xt is current Warming the default pipelines from SetupImpellerContext could deadlock startup in two ways, since the compile jobs are posted to the IO thread: * Their reactor operations were queued before the IO thread's resource context was current, after which nothing guaranteed they ran, so the raster thread could wait on the pipelines forever (flutter#196453). * They were posted to the shell's shutdown-safe IO task runner, whose tasks wait for the shell to be set up, ahead of the shell's IO setup. The IO thread then never set up, so the shell never got set up (flutter#196549 (comment)). PlatformViewAndroid::CreateResourceContext now starts the warm-up, on the IO thread during the shell's IO setup, once the resource context is current. The warm-up itself moves to AndroidContext::StartWarmingDefaultPipelines, which requests the pipelines on the raster thread, only for OpenGL ES, and only once per AndroidContext, so the platform views of spawned engines that share the context don't warm it again. Compile jobs posted from SetupImpellerContext couldn't start before the shell was set up anyway, so this doesn't delay compilation.
|
@andywolff please take a look, here's the changes since your last review:
|
|
Oh, I also ran the PR against the smoke test and they didn't hang on my device but they failed screenshots, probably from differences in the device. This extra hop to the raster thread will make the change slightly slower. Considering we are talking about ~160ms it should be comparatively small, but I didn't measure it before hand. |
andywolff
left a comment
There was a problem hiding this comment.
LGTM, and this passed on some staging runs
- https://ci.chromium.org/b/8665208196726182193 — SUCCESS
- https://ci.chromium.org/b/8665208190771742177 — SUCCESS
- https://ci.chromium.org/b/8665208184769169249 — SUCCESS
My review agents also flagged a couple of non-blocking items that might be worth following up on after this lands. First, because every OpenGL ES engine now starts compiling default shaders at startup, a short-lived or pre-warmed engine that gets torn down before warmup finishes can drop in-flight compile jobs after the EGL context has already been cleared and terminated. Those dropped jobs still issue OpenGL shader cleanup calls during destruction without checking whether an OpenGL context is current, which could trigger errors or debug assertions.
Second, they pointed out a few test coverage gaps. None of the unit tests actually exercise the resource context creation path that kicks off the warmup, since the test fixture uses a software surface and calls the warmup helper directly. On top of that, the pipeline deduplication test can't actually catch duplicate requests because the mock shader library generates a fresh unique ID on every lookup, and the default pipeline tests only stub the Vulkan backend rather than OpenGL ES.
|
This has a merge conflict with #196342. I'll revert this and reland it on monday fyi @andywolff |
|
Successfully created revert PR: #196575 |
…ed" (flutter#196575) Reverts: [Reland "[android]: prewarms shaders when the context is created"](flutter#196549) Initiated by: @gaaclarke Reason for reverting: merge conflict with flutter#196342 causing post submit failures ( [example](https://logs.chromium.org/logs/flutter/buildbucket/cr-buildbucket/8665197447632156673/+/u/build_ci_android_emulator_debug_x64_flutter_impeller_renderer_backend_vulkan:vulkan_android_unittests_flutter_impeller_renderer_backend_vulkan:vulkan_android_apk_unittests_flutter_impeller_toolkit_android:unittests_flutter_shell_platform_android:flutter_shell_native_unittests/stdout)) Original PR Author: @gaaclarke Reviewed By: @andywolff The original PR description is provided below: relands flutter#196069 ~~Additions since relanding:~~ 1) ~~A test has been added to duplicate the failure in the android test runner~~ 1) ~~A flush was added to the io thread when the io thread's context is set~~ 1) ~~The warmup is now limited to opengles since we got mixed results from vulkan.~~ Additions since relanding: 1) Removed vulkan branch (with pixel format assert) 1) Started pushing warming up to raster thread ## 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/CONTRIBUTING.ai.md [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
…ted" (flutter#196549)" This reverts commit 83d8e06.
…ew test delegate flutter#196342 added the pure virtual PlatformView::Delegate::OnPlatformViewCreatedDeferred, which the MockPlatformViewDelegate added to platform_view_android_unittests.cc by flutter#196549 didn't implement, so flutter_shell_native_unittests failed to compile.
…tter#196579) relands flutter#196549 It was previously reverted because it had a merge conflict. Changes since previous land: 1) Added missing mock method. ## 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/CONTRIBUTING.ai.md [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
flutter/flutter@1b95854...d8bd864 2026-11-16 [email protected] Roll Skia from f2790b0ea630 to 118eaff3deb4 (8 revisions) (flutter/flutter#196661) 2026-11-16 [email protected] [native_assets] Keep a dylib's install name in flutter test when its path does not fit (flutter/flutter#194802) 2026-11-16 [email protected] Reland "[android]: prewarms shaders when the context is created" (flutter/flutter#196579) 2026-11-16 [email protected] [flutter_tools] Bound Browser.close in Chromium.close (flutter/flutter#196211) 2026-11-16 [email protected] Roll Skia from e8ff7a4187ec to f2790b0ea630 (13 revisions) (flutter/flutter#196652) 2026-11-14 [email protected] Revert: Balance web_canvaskit_tests and web_skwasm_tests subshards by duration (flutter/flutter#196580) 2026-11-14 [email protected] Revert: Reland "[android]: prewarms shaders when the context is created" (flutter/flutter#196575) 2026-11-14 [email protected] [flutter_tools] Guard ExtensionManager initialization against concurrent disposal (flutter/flutter#196459) 2026-11-14 [email protected] [ImpellerC] Escape spaces in depfile paths (flutter/flutter#196555) 2026-11-14 [email protected] Reland "[android]: prewarms shaders when the context is created" (flutter/flutter#196549) 2026-11-14 [email protected] Balance web_canvaskit_tests and web_skwasm_tests subshards by duration (flutter/flutter#196260) 2026-11-13 [email protected] Safely return null on malformed stack trace line in StackFrame.fromStackTraceLine (flutter/flutter#194673) 2026-11-13 [email protected] Clarify that super.dispose() must be called synchronously (flutter/flutter#193416) 2026-11-13 [email protected] Consolidate exit-code parameters on runCommand with ExpectedExit (flutter/flutter#196479) 2026-11-13 [email protected] Support multi-view apps in widget test binding (flutter/flutter#194480) 2026-11-13 [email protected] Document using a ListView inside a Column (flutter/flutter#195145) 2026-11-13 [email protected] Add didChangeAlwaysUse24HourFormat to WidgetsBinding and update MediaQuery (flutter/flutter#196117) 2026-11-13 [email protected] Build local engine wasm configs on 32-core Linux bots (flutter/flutter#196284) 2026-11-13 [email protected] [Android] Fix Hybrid Composition resize deadlock (flutter/flutter#196255) 2026-11-13 [email protected] Roll Skia from 11f46cfe8838 to e8ff7a4187ec (9 revisions) (flutter/flutter#196557) 2026-11-13 [email protected] [Impeller] Fix SurfaceContextVK submission tracking and double HostBuffer reset (flutter/flutter#196064) 2026-11-13 [email protected] Manual roll Packages from 14f9933 to 015df97 (5 revisions) (flutter/flutter#196558) 2026-11-13 [email protected] Remove routes_transition_test from widgets (flutter/flutter#186620) 2026-11-13 [email protected] [flutter_tools] Replace TargetPlatform.simpleName with cpuArch (flutter/flutter#196264) 2026-11-13 [email protected] Add a deferred platform-view-created path to the shell and use it on Android (flutter/flutter#196342) 2026-11-13 [email protected] Optimize RenderSliverVariedExtentList and introduce protected itemExtentOf API in RenderSliverFixedExtentBoxAdaptor (flutter/flutter#192384) 2026-11-13 [email protected] Revert: [android]: prewarms shaders when the context is created (flutter/flutter#196468) If this roll has caused a breakage, revert this CL and set the roller to dry run mode using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC [email protected],[email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
relands #196069
Additions since relanding:A test has been added to duplicate the failure in the android test runnerA flush was added to the io thread when the io thread's context is setThe warmup is now limited to opengles since we got mixed results from vulkan.Additions since relanding:
Pre-launch Checklist
///).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. Comments from the
gemini-code-assistbot 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.