You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Test failure: System.Threading.Overlapped.Tests crash with IOException handle is invalid on Windows ARM64 #127366
It is possible this is not a jit issue, though I couldn't find occurrences in other pipelines
Bug description generated from AI: Summary:
Overlapped test crash with IOException when EventWaitHandle.Set fails in IO completion callback on Windows ARM64.
Unhandled exception. System.IO.IOException: The handle is invalid.
at System.Threading.EventWaitHandle.Set()
at AsyncHelper.Callback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* _overlapped)
at System.Threading.PortableThreadPool.IOCompletionPoller.Event.Invoke()
Stack Trace:
at System.Threading.EventWaitHandle.Set()
at AsyncHelper.Callback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* _overlapped)
at System.Threading.PortableThreadPool.IOCompletionPoller.Event.Invoke()
at System.Threading.ThreadPoolTypedWorkItemQueue.System.Threading.IThreadPoolWorkItem.Execute()
Analysis:
Race condition or premature disposal of event handle. Exit code 0xE0434352 = CLR unhandled exception. Under defaultpgo stress. No existing issue found.
The test (PackPosTest / PackPosTest1) uses a two-stage callback chain via IO completion ports:
Test method
│
├── 1. helper = new AsyncHelper() // creates ManualResetEvent
├── 2. callback = MyCallback(helper) // captures helper in delegate
├── 3. nativeOverlapped = ov.Pack(callback)
├── 4. ThreadPool.UnsafeQueueNativeOverlapped(nativeOverlapped)
├── 5. helper.Wait() // blocks on _event.WaitOne()
│
│ ──── IOCP delivers first callback (MyCallback) ────
│
├── 6. Creates NEW Overlapped, packs with helper.Callback
├── 7. ThreadPool.UnsafeQueueNativeOverlapped(nativeOverlapped2)
├── 8. MyCallback returns
│
│ ──── WINDOW: helper only reachable via nativeOverlapped2 (unmanaged ptr) ────
│ ──── GC may not see helper as rooted ────
│
│ ──── IOCP delivers second callback (helper.Callback) ────
│
└── 9. this._event.Set() ← CRASH: handle already finalized
Race Condition
Between steps 8 and 9:
MyCallback has returned — helper is no longer on any managed stack frame
helper is only reachable through the packed nativeOverlapped2, which is an unmanaged pointer that the GC
doesn't trace
If GC runs, it may finalize the ManualResetEvent inside helper, closing its OS handle
When the IO completion poller delivers the second callback, _event.Set() fails with "handle is invalid"
Meanwhile, the test thread is blocked in helper.Wait() (step 5), holding a reference to helper — but the JIT may
report helper as dead after the last managed use (the Wait() call itself), allowing the GC to collect it even while WaitOne() is blocking.
Why Now
This is a latent test bug that has existed since the test was written. It surfaced for the first time because:
PGO stress (defaultpgo) changes tiering/compilation timing, affecting when GC runs relative to the callback
chain
ARM64 has different GC pause characteristics
The timing window is extremely narrow — only 1 hit in a month of CI runs
Proposed Fix
Add GC.KeepAlive(helper) after Wait() in both PackPosTest and PackPosTest1 to prevent the JIT from reporting helper as dead before Wait() returns:
try{Assert.True(nativeOverlapped!=null);Assert.True(ThreadPool.UnsafeQueueNativeOverlapped(nativeOverlapped));Assert.True(helper.Wait());GC.KeepAlive(helper);}finally{Overlapped.Free(nativeOverlapped);}Thisensures helper (andits ManualResetEvent) stays rooted for the entire duration of the callback chain. ```
Activity
dotnet-policy-service commentedon Apr 23, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
See info in area-owners.md if you want to be subscribed.
mangod9 commentedon Apr 28, 2026
Investigation Summary
Symptoms
defaultpgo(TieredPGO=1)IOException: The handle is invalidatEventWaitHandle.Set()0xE0434352(CLR unhandled exception)Code Flow
The test (
PackPosTest/PackPosTest1) uses a two-stage callback chain via IO completion ports:Race Condition
Between steps 8 and 9:
MyCallbackhas returned —helperis no longer on any managed stack framehelperis only reachable through the packednativeOverlapped2, which is an unmanaged pointer that the GCdoesn't trace
ManualResetEventinsidehelper, closing its OS handle_event.Set()fails with "handle is invalid"Meanwhile, the test thread is blocked in
helper.Wait()(step 5), holding a reference tohelper— but the JIT mayreport
helperas dead after the last managed use (theWait()call itself), allowing the GC to collect it even whileWaitOne()is blocking.Why Now
This is a latent test bug that has existed since the test was written. It surfaced for the first time because:
defaultpgo) changes tiering/compilation timing, affecting when GC runs relative to the callbackchain
Proposed Fix
Add
GC.KeepAlive(helper)afterWait()in bothPackPosTestandPackPosTest1to prevent the JIT from reportinghelperas dead beforeWait()returns:mangod9 commentedon Apr 28, 2026
@copilot can you make a fix based on the comments?
Fix race condition in System.Threading.Overlapped tests causing IOExc…
Fix race condition in System.Threading.Overlapped tests causing IOExc…
Fix race condition in System.Threading.Overlapped tests causing IOExc…