Skip to content

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

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-overlapped-test-crash
Draft

Fix race condition in System.Threading.Overlapped tests causing IOException on Windows ARM64#127493
Copilot wants to merge 2 commits intomainfrom
copilot/fix-overlapped-test-crash

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 28, 2026

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

…revent premature finalization

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/233433e7-d3f0-411c-a574-0bfd57ff9e9e

Co-authored-by: mangod9 <61718172+mangod9@users.noreply.github.com>
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.

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.

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

2 participants