Skip to content

Fix race condition in System.Threading.Overlapped tests causing IOException on Windows ARM64 - #127493

Merged
VSadov merged 2 commits into
mainfrom
copilot/fix-overlapped-test-crash
Jun 12, 2026
Merged

VSadov merged 2 commits into
mainfrom
copilot/fix-overlapped-test-crash

Conversation

Copilot AI commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor

Under PGO stress on Windows ARM64, PackPosTest and PackPosTest1 would intermittently crash with IOException: The handle is invalid at EventWaitHandle.Set() inside the IO completion callback.

Description

Root cause: The two-stage callback chain queues a second NativeOverlapped* that holds the only reference to helper as an unmanaged pointer. The GC cannot trace through unmanaged pointers, so after helper.Wait() returns the JIT is free to report helper as dead — allowing the finalizer to close the ManualResetEvent OS handle before the second callback fires and calls _event.Set().

Fix: Add GC.KeepAlive(helper) after helper.Wait() in both PackPosTest and PackPosTest1:

Assert.True(ThreadPool.UnsafeQueueNativeOverlapped(nativeOverlapped));
Assert.True(helper.Wait());
GC.KeepAlive(helper);  // prevent finalization before second IOCP callback fires

This extends helper's managed lifetime past Wait(), ensuring the ManualResetEvent handle remains valid for the entire callback chain. The bug is latent since the test was written; it surfaced only under defaultpgo stress timing on ARM64.

Fixes #127366

Copilot AI requested review from Copilot and removed request for Copilot April 28, 2026 06:14
Copilot AI changed the title [WIP] Fix overlapped test failure on Windows ARM64 due to IOException Fix race condition in System.Threading.Overlapped tests causing IOException on Windows ARM64 Apr 28, 2026
Copilot AI requested a review from mangod9 April 28, 2026 06:15
@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.

@VSadov
VSadov enabled auto-merge (squash) April 30, 2026 00:17
@VSadov

VSadov commented Jun 12, 2026

Copy link
Copy Markdown
Member

/ba-g unrelated failure and fixed by now

@VSadov
VSadov merged commit 6dbc49a into main Jun 12, 2026
99 of 102 checks passed
@VSadov
VSadov deleted the copilot/fix-overlapped-test-crash branch June 12, 2026 01:34
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 17, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
…eption on Windows ARM64 (#127493)

Under PGO stress on Windows ARM64, `PackPosTest` and `PackPosTest1`
would intermittently crash with `IOException: The handle is invalid` at
`EventWaitHandle.Set()` inside the IO completion callback.

## Description

**Root cause:** The two-stage callback chain queues a second
`NativeOverlapped*` that holds the only reference to `helper` as an
unmanaged pointer. The GC cannot trace through unmanaged pointers, so
after `helper.Wait()` returns the JIT is free to report `helper` as dead
— allowing the finalizer to close the `ManualResetEvent` OS handle
before the second callback fires and calls `_event.Set()`.

**Fix:** Add `GC.KeepAlive(helper)` after `helper.Wait()` in both
`PackPosTest` and `PackPosTest1`:

```csharp
Assert.True(ThreadPool.UnsafeQueueNativeOverlapped(nativeOverlapped));
Assert.True(helper.Wait());
GC.KeepAlive(helper);  // prevent finalization before second IOCP callback fires
```

This extends `helper`'s managed lifetime past `Wait()`, ensuring the
`ManualResetEvent` handle remains valid for the entire callback chain.
The bug is latent since the test was written; it surfaced only under
defaultpgo stress timing on ARM64.

Fixes #127366

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: mangod9 <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 18, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test failure: System.Threading.Overlapped.Tests crash with IOException handle is invalid on Windows ARM64

4 participants