Skip to content

Fix safe publication of the Control callback queue - #15311

Open
LeafShi1 wants to merge 1 commit into
dotnet:mainfrom
LeafShi1:Fix_15308_safe_publication_of_the_Control_callback_queue
Open

LeafShi1 wants to merge 1 commit into
dotnet:mainfrom
LeafShi1:Fix_15308_safe_publication_of_the_Control_callback_queue

Conversation

@LeafShi1

@LeafShi1 LeafShi1 commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

Fixes #15308

Root cause

The callback queue is lazily initialized under an instance lock, but readers access the queue reference without acquiring that initialization lock. Consequently, the initialization lock alone does not establish safe publication to those readers.

Additionally, the disposal path reads the queue's Count outside the queue lock, even though other threads may modify the queue.

Proposed change

  • Add volatile to the callback queue field so other threads can safely access the initialized queue
  • Remove the queue-count check outside the lock, the count is already checked inside the lock

Customer Impact

  • Addresses a potential concurrency issue when marshaling callbacks to the UI thread

Regression?

  • No

Risk

  • Minimal

Test environment(s)

  • .net 12.0.100-alpha.1.26480.102
Microsoft Reviewers: Open in CodeFlow

Copilot AI 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.

🟢 Approval recommended

The narrow changes address the identified synchronization gaps while preserving existing queue locking and callback behavior.

No open issues 🎉

What changed in this PR

This PR addresses #15308 by tightening synchronization around the WinForms Control callback queue used to marshal work to the UI thread.

Changes:

  • Marks the lazily initialized queue reference volatile for safe publication.
  • Removes the unlocked queue-count check during disposal handling, retaining the check inside the queue lock.
File Description
src/​System.Windows.Forms/​System/​Windows/​Forms/​Control.cs Ensures safe queue publication and removes an unsynchronized count read.

Review effort: Balanced

@codecov

codecov Bot commented Oct 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 37.84861%. Comparing base (c37f09d) to head (6ffa6fc).

Additional details and impacted files
@@              Coverage Diff              @@
##                main      #15311   +/-   ##
=============================================
  Coverage   37.84861%   37.84861%           
=============================================
  Files            246         246           
  Lines           9789        9789           
  Branches        1031        1031           
=============================================
  Hits            3705        3705           
  Misses          5920        5920           
  Partials         164         164           
Flag Coverage Δ
Debug 37.84861% <ø> (ø)
production 40.04608% <ø> (ø)
test 20.64923% <ø> (ø)
unit 40.04608% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

missing lock in Control.InvokeMarshaledCallbacks

2 participants