Skip to content

[Impeller] Guard the GLES pipeline map with a mutex - #193746

Closed
leehack wants to merge 6 commits into
flutter:masterfrom
leehack:impeller-gles-pipeline-library-lock
Closed

leehack wants to merge 6 commits into
flutter:masterfrom
leehack:impeller-gles-pipeline-library-lock

Conversation

@leehack

@leehack leehack commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

PipelineLibraryGLES locks programs_ but not pipelines_. GetPipeline looks up and inserts into pipelines_ on the calling thread; only program compilation and linking run on the reactor. Flutter GPU calls HasPipeline and GetPipeline on the UI thread (RenderPass::GetOrCreatePipeline), and RemovePipelinesWithEntryPoint when a shader is reloaded, while Impeller inserts pipeline variants on the raster thread. A lookup can race an insert or a rehash.

This PR guards pipelines_ with its own mutex. The lock covers only the map accesses, not pipeline creation, and try_emplace makes concurrent requests for the same descriptor share one future. RemovePipelinesWithEntryPoint takes the pipelines lock and then the programs lock; no other path holds both, so the order cannot invert.

The new test, PipelineLibraryGLESTest.CanLookUpPipelinesWhileCreatingPipelines, looks up an existing pipeline on one thread while the test thread, which is the GLES reactor thread, creates 128 variants.

Verified on master d03768e, Linux x64, OpenGL ES on Mesa under Xvfb, as in CI:

Without the lock With the lock
New test, 40 runs 8 pass, 32 fail 40/40
New test under TSan, 10 runs pipelines_ race in 10/10 no pipelines_ race

The full Linux impeller_unittests run passes with the lock (run with an earlier version of the test in renderer_unittests.cc), and the format and clang-tidy checks are clean.

Related: the same race in PipelineLibraryMTL (#193721 / #193722) and in the sampler libraries (#193737 / #193738). This PR touches only GLES files, so it does not depend on either.

Fixes #193745

Pre-launch Checklist

Flutter GPU calls PipelineLibrary::HasPipeline and GetPipeline on the UI
thread (flutter::gpu::RenderPass::GetOrCreatePipeline) while Impeller
requests pipeline variants on the raster thread. PipelineLibraryGLES locked
programs_ but not pipelines_, and GetPipeline does its map lookup and
insert on the calling thread (only program creation is deferred to the
reactor), so a lookup could race an insertion or rehash.

Guard pipelines_ with a mutex. The lock covers only the map accesses, not
pipeline creation, and try_emplace makes concurrent requests for the same
descriptor share one future. RemovePipelinesWithEntryPoint takes the
pipelines lock before the programs lock; no other path holds both.

Add PipelineLibraryGLESTest.CanLookUpPipelinesWhileCreatingPipelines,
which looks up an existing pipeline on one thread while the reactor thread
creates new variants.
@github-actions github-actions Bot added engine flutter/engine related. See also e: labels. e: impeller Impeller rendering backend issues and features requests labels Oct 2, 2026
@leehack

leehack commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

This is currently in draft status because I can only cut two ready PRs at a time. Once those have been merged, I will move this PR to ready.
#193722
#193738

@leehack
leehack marked this pull request as ready for review October 6, 2026 10:00

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

Copy link
Copy Markdown
Contributor

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 introduces thread safety to the PipelineLibraryGLES class by guarding the pipelines_ map with a new pipelines_mutex_ mutex. Lock acquisitions have been added to GetPipeline, HasPipeline, and RemovePipelinesWithEntryPoint to prevent concurrent access issues. Additionally, a new concurrent unit test CanLookUpPipelinesWhileCreatingPipelines has been added to verify these changes. There are no review comments, and I have no feedback to provide.

@leehack

leehack commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

@bdero It's a similar pr as
#193722
#193738

It was in draft because of the two-ready PR limit; since one of them has been merged, I was able to make it ready now.

@gaaclarke

Copy link
Copy Markdown
Member

I think this will likely be unnecessary after #194022 lands. Can you please give that a look and see if it addresses your problem.

@gaaclarke
gaaclarke self-requested a review October 12, 2026 21:49
@leehack

leehack commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the pointer. I checked #194022 and it doesn't cover this one. GetPipeline still looks up and inserts into pipelines_ unguarded on the calling thread, and HasPipeline / RemovePipelinesWithEntryPoint are unchanged. I ran this PR's test on top of #194022 (885b3ee) under TSan and it still reports the race:

WARNING: ThreadSanitizer: data race
  Write of size 8 by main thread:
    impeller::PipelineLibraryGLES::GetPipeline(PipelineDescriptor, bool, bool)
  Previous read of size 8 by thread T24:
    impeller::PipelineLibraryGLES::HasPipeline(const PipelineDescriptor&)

The race in #193745 is between callers of the library (Flutter GPU on the UI thread and the raster thread), so it's independent of where compilation runs. Happy to rebase this on top of #194022 once it lands, or land first if you prefer.

@gaaclarke

Copy link
Copy Markdown
Member

Okay, yea just looked into this a bit more and noticed you are talking about the flutter-gpu usage. Let's try to land that other fix first since it's a pretty intensive change. I'll ping you on this PR when that lands. I don't like that fluttergpu is using the library differently. I would prefer that fluttergpu keeps a handle to a shader that gets resolved on the raster thread. (cc @bdero )

@gaaclarke

Copy link
Copy Markdown
Member

fyi #194022 has landed. I reached out to bdero asking about reworking this so that the threading model of flutter-gpu matches flutter. I'm not sure how fast that will progress though.

@gaaclarke gaaclarke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

rebase please

…ine-library-lock

# Conflicts:
#	engine/src/flutter/impeller/renderer/backend/gles/pipeline_library_gles.cc
#	engine/src/flutter/impeller/renderer/backend/gles/test/pipeline_library_gles_unittests.cc
@gaaclarke
gaaclarke requested review from bdero and gaaclarke October 19, 2026 19:18
@gaaclarke gaaclarke added the CICD Run CI/CD label Oct 19, 2026
@bdero

bdero commented Oct 19, 2026

Copy link
Copy Markdown
Member

Lemme take a quick look at this. I think this may be good to land in the meantime.

@bdero bdero left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM w/ #194022. Ultimately Flutter GPU needs to resolve pipelines on the raster thread so the UI thread stops touching the library at all. I have a draft patch for that, but this is safe to land in the meantime.

@gaaclarke

Copy link
Copy Markdown
Member

LGTM w/ #194022. Ultimately Flutter GPU needs to resolve pipelines on the raster thread so the UI thread stops touching the library at all. I have a draft patch for that, but this is safe to land in the meantime.

@bdero if that's anywhere close to being completed I'd prefer to land that. If not, let's file an issue for the removal of this mutex and put some TODO's in the code.

@gaaclarke

Copy link
Copy Markdown
Member

Closing this in lieu of #194882

@gaaclarke gaaclarke closed this Oct 21, 2026
@github-project-automation github-project-automation Bot moved this from 🤔 Needs Triage to ✅ Done in Flutter GPU Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD e: impeller Impeller rendering backend issues and features requests engine flutter/engine related. See also e: labels.

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

[Impeller] GLES: PipelineLibraryGLES pipeline map is accessed from two threads without a lock (Flutter GPU)

3 participants