-
Notifications
You must be signed in to change notification settings - Fork 1k
Application.ThreadContext.FromId reads from s_contextHash while another thread may be writing #13246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@Olina-Zhang - could your team please work on a unit test or a ScratchProject repro that exercises the relevant code for this issue. Please share your private branch when done. |
@Tanya-Solyanik Tried to repro with that relevant code from the Application.ThreadContext.FromId method, but cannot, no any error after simulating a race condition between a reader and a writer. |
@Olina-Zhang why does your sample app have On the main branch, winforms/src/System.Windows.Forms/System/Windows/Forms/Application.ThreadContext.cs Line 25 in 8ae5137
Likewise in v9.0.3: winforms/src/System.Windows.Forms/src/System/Windows/Forms/Application.ThreadContext.cs Line 25 in 5bf1bff
|
I mean, ConcurrentDictionary may be a good way to fix the bug, but you cannot reproduce the error if you use that in the sample app. |
@KalleOlaviNiemitalo are you hitting this? Are you willing to propose a fix PR? If not, I'll look into fixing this when I've got a few cycles. @Olina-Zhang don't worry about a repro here. |
No I am not hitting this. And won't make a PR. |
Thanks for the clarification. I'll take care of it when I can. |
.NET version
9.0.3
Did it work in .NET Framework?
Yes
Did it work in any of the earlier releases of .NET Core or .NET 5+?
I guess it would have worked in .NET 7, because #8161 was not merged there.
Issue description
The Application.ThreadContext.FromId method reads from the
s_contextHash
dictionary without locking. This can cause an error if another thread writes to the dictionary at the same time.winforms/src/System.Windows.Forms/src/System/Windows/Forms/Application.ThreadContext.cs
Lines 513 to 521 in 5bf1bff
Writers do
lock (s_tcInternalSyncObject)
but this reader doesn't. AFAICT, the callers of Application.ThreadContext.FromId don't lock it either.Before #8161,
s_contextHash
used to be a Hashtable, which is safe for one writer in parallel with multiple readers. Dictionary<TKey, TValue> is not safe for that use.Steps to reproduce
Found by source code inspection, not reproduced.
The text was updated successfully, but these errors were encountered: