Skip to content

Make GDI+ and encoder init thread-safe; add concurrency tests - #14896

Draft
KlausLoeffelmann wants to merge 1 commit into
dotnet:mainfrom
KlausLoeffelmann:Fix14884_gdiplus
Draft

Make GDI+ and encoder init thread-safe; add concurrency tests#14896
KlausLoeffelmann wants to merge 1 commit into
dotnet:mainfrom
KlausLoeffelmann:Fix14884_gdiplus

Conversation

@KlausLoeffelmann

@KlausLoeffelmann KlausLoeffelmann commented Aug 17, 2026

Copy link
Copy Markdown
Member

This pull request improves thread safety and concurrency handling in GDI+ initialization and image encoder caching. It introduces proper locking and volatile access patterns to prevent race conditions when these resources are accessed concurrently, and adds new tests to verify correct concurrent behavior.

Fixes #14884.

Thread safety and concurrency improvements:

  • Added locking and volatile access to GdiPlusInitialization.EnsureInitialized and IsInitialized, ensuring GDI+ is initialized safely under concurrent access. [1] [2]
  • Introduced locking and volatile access to the encoder cache in ImageCodecInfoHelper, preventing race conditions when building or accessing the encoder list. [1] [2] [3]

Testing enhancements:

  • Added ImageConcurrencyTests.cs to verify that concurrent first use of GDI+ initialization and encoder cache works correctly without failures or race conditions.
Microsoft Reviewers: Open in CodeFlow

* Introduce thread-safe initialization in GdiPlusInitialization and ImageCodecInfoHelper using locks and Volatile.Read/Write to prevent race conditions during concurrent first use.
* Move encoder creation logic to a new CreateEncoders method.
* Add ImageConcurrencyTests.cs to verify thread safety of GDI+ and encoder cache initialization under parallel access.
@KlausLoeffelmann KlausLoeffelmann self-assigned this Aug 17, 2026
@KlausLoeffelmann KlausLoeffelmann added this to the Future milestone Aug 17, 2026
@dotnet-policy-service dotnet-policy-service Bot added the draft draft PR label Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Race condition in ImageCodecInfoHelper

1 participant