From 6ffa6fc67e2d97f4167d3f5a74f20ed85c4f646c Mon Sep 17 00:00:00 2001 From: "Leaf Shi (BEYONDSOFT CONSULTING INC)" Date: Mon, 26 Oct 2026 17:19:50 +0800 Subject: [PATCH] Fix safe publication of the Control callback queue --- src/System.Windows.Forms/System/Windows/Forms/Control.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/System.Windows.Forms/System/Windows/Forms/Control.cs b/src/System.Windows.Forms/System/Windows/Forms/Control.cs index 0e1175dee4a..0f33328cfc3 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Control.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Control.cs @@ -280,7 +280,8 @@ public unsafe partial class Control : private byte _requiredScaling; private TRACKMOUSEEVENT _trackMouseEvent; private LayoutEventArgs? _cachedLayoutEventArgs; - private Queue? _threadCallbackList; + // Publish the lazily initialized queue to readers that do not take the initialization lock. + private volatile Queue? _threadCallbackList; // For keeping track of our ui state for focus and keyboard cues. Using a member // variable here because we hit this a lot @@ -3776,7 +3777,7 @@ private unsafe void WaitForWaitHandle(WaitHandle waitHandle) throw new InvalidAsynchronousStateException(SR.ThreadNoLongerValid); } - if (IsDisposed && _threadCallbackList is not null && _threadCallbackList.Count > 0) + if (IsDisposed && _threadCallbackList is not null) { lock (_threadCallbackList) {