Skip to content

Delete NativeOverlapped pool for Win32ThreadPool - #137800

Merged
jkotas merged 1 commit into
dotnet:mainfrom
jkotas:jkotas-fix-overlapped-allocation-race
Nov 19, 2026
Merged

jkotas merged 1 commit into
dotnet:mainfrom
jkotas:jkotas-fix-overlapped-allocation-race

Conversation

@jkotas

@jkotas jkotas commented Nov 18, 2026

Copy link
Copy Markdown
Member

The portable threadpool has no pool for NativeOverlapped like this and the lack of it was not identified as a bottleneck. There are pools both under (GCHandle, malloc) and above (FileStream and Sockets instances cache and reuse NativeOverlapped). Fix the race condition in the implementation of the superflous pool by deleting it.

The allocation / free of Win32 threadpool NativeOverlapped is still faster than allocation / free of portable threadpool NativeOverlapped even with the pool deleted.

Fixes #137737

The portable threadpool has no pool for NativeOverlapped like this and
the lack of it was not identified as a bottleneck. There are pools both
under (GCHandle, malloc) and above (FileStream and Sockets instances cache
and reuse NativeOverlapped). Fix the race condition in the
implementation of the superflous pool by deleting it.

The allocation / free of Win32 threadpool NativeOverlapped is still
faster than allocation / free of portable threadpool NativeOverlapped
even with the pool deleted.

Fixes dotnet#137737
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@jkotas
jkotas requested a review from VSadov November 18, 2026 03:52
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
See info in area-owners.md if you want to be subscribed.

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.

🔵 Needs a closer look

Immediate native-memory and GCHandle reclamation changes unsafe Windows I/O lifetimes, but concurrent allocation/completion, callback-time freeing, and PreAllocatedOverlapped reuse/disposal could not be validated in this environment.

No open issues 🎉

What changed in this PR

Removes the Windows thread pool’s NativeOverlapped pool to eliminate the allocation race described in #137737, replacing pooled reuse with direct allocation and cleanup.

Changes:

  • Removes the lock-free free list and shared metadata array.
  • Uses a per-allocation GCHandle for metadata and explicitly releases pinned handles and native memory.
  • Deletes reset logic used only by the removed pool.
File Description
src/​libraries/​System.Private.CoreLib/​src/​System/​Threading/​Win32ThreadPoolNativeOverlapped.OverlappedData.cs Removes pool-specific reset logic and its unused import.
src/​libraries/​System.Private.CoreLib/​src/​System/​Threading/​Win32ThreadPoolNativeOverlapped.cs Replaces pooling with direct native allocation and handle cleanup.

Review effort: Balanced

@VSadov VSadov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@jkotas
jkotas merged commit d30189e into dotnet:main Nov 19, 2026
103 of 105 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 19, 2026
@jkotas

jkotas commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

@jkotas
jkotas deleted the jkotas-fix-overlapped-allocation-race branch November 19, 2026 18:58
JulieLeeMSFT pushed a commit that referenced this pull request Nov 20, 2026
)

Backport of #137800 to release/11.0

/cc @jkotas

## Customer Impact

- [x] Customer reported
- [ ] Found internally

Intermittent crash in async I/O intensive NAOT compiled apps on Windows

The root cause is race condition in cache for native overlapped
instances used by Win32 thread pool (default for NAOT on Windows). The
fix is to delete the cache since it is not effective (there are other
caches in the layers above and below).

## Regression

- [ ] Yes
- [x] No

## Testing

Customer provided stress test passed

## Risk

Low

Co-authored-by: Jan Kotas <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Race allocating Win32ThreadPoolNativeOverlapped

3 participants