Reduce lock contention in socket on Windows#124997
Conversation
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
|
@copilot, revert the unrelated "rollup" changes |
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR reduces lock contention in the Windows socket async path by changing DynamicWinsockMethods.GetMethods from a globally locked List<T> to a copy-on-write volatile array with a lock-free read path.
Changes:
- Replace
List<DynamicWinsockMethods>+lock(list)withvolatile DynamicWinsockMethods[]for lock-free reads. - Add a dedicated
Lock(s_methodTableLock) and a slow path that double-checks and publishes a new array snapshot.
src/libraries/System.Net.Sockets/src/System/Net/Sockets/DynamicWinsockMethods.cs
Outdated
Show resolved
Hide resolved
…cWinsockMethods.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@copilot please invoke the code-review skill and post the analysis/results as a comment on this PR. |
🤖 Copilot Code Review — DynamicWinsockMethods lock contention fixHolistic AssessmentMotivation: The original code acquired a global Approach: The copy-on-write volatile array pattern is well-established in this codebase (e.g., Summary: ✅ LGTM. The change is a clean, minimal, correct application of a well-known lock-free read / copy-on-write pattern. No correctness, thread-safety, or behavioral issues found. Detailed Findings✅ Thread safety — Volatile read + COW is correctThe fast path in ✅ Double-check under lock — Correct
✅ Lock antipattern fix — GoodThe original ✅ Copy-on-write mechanics — Correct
✅
|
src/libraries/System.Net.Sockets/src/System/Net/Sockets/DynamicWinsockMethods.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Sockets/src/System/Net/Sockets/DynamicWinsockMethods.cs
Outdated
Show resolved
Hide resolved
|
/ba-g unrelated wasm failures |
Description
Lock contention in
DynamicWinsockMethods:GetMethods()was called under a globallock(s_methodTable)on every socket's first async operation, serializing all concurrent callers even after the table was fully populated.Changes
DynamicWinsockMethods.cs— ReplaceList<T>+lock(s_methodTable)with a lock-free read path over avolatile DynamicWinsockMethods[]:Lock s_methodTableLock, double-checks under the lock, then publishes a new copy-on-write array via volatile storelock(collection)antipattern; the old code locked on theList<T>instance itselfOriginal prompt
Created from Copilot CLI via the copilot delegate command.
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.