Skip to content

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

Closed

Description

@dhartglassMSFT

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.

Failed in (1):

Console Log: Console Log
Source: runtime-coreclr libraries-pgo / net11.0-windows-Release-arm64-defaultpgo-Windows.11.Arm64.Open / System.Threading.Overlapped.Tests

Failed tests:

- net11.0-windows-Release-arm64-defaultpgo-Windows.11.Arm64.Open
  - System.Threading.Overlapped.Tests

Error Message:

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.

Activity

  1. added
    blocking-clean-ci-optional
    on Apr 23, 2026
  2. added
    untriaged
    on Apr 23, 2026
  3. dotnet-policy-service commented on Apr 23, 2026

    @dotnet-policy-service
    Contributor

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

  4. mangod9 commented on Apr 28, 2026

    @mangod9
    Member

    Investigation Summary

    Symptoms

    • Platform: Windows ARM64, Release, defaultpgo (TieredPGO=1)
    • Error: IOException: The handle is invalid at EventWaitHandle.Set()
    • Exit code: 0xE0434352 (CLR unhandled exception)
    • Frequency: 1 occurrence in 1 month — rare, non-reproducible
    • Test unchanged since: August 2022 (Fixes for Overlapped tests #73439)

    Code Flow

    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);
    }
    
    This ensures helper (and its ManualResetEvent) stays rooted for the entire duration of the callback chain. ```
  5. mangod9 commented on Apr 28, 2026

    @mangod9
    Member

    @copilot can you make a fix based on the comments?

  6. removed
    untriaged
    on Apr 29, 2026
  7. added this to the 11.0.0 milestone on Apr 29, 2026
  8. locked and limited conversation to collaborators on Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions