- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.2k
Implement alertable waits for WaitHandle to support APC handling in NativeAOT #118256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas | 
Co-authored-by: jkotas <[email protected]>
        
          
                src/libraries/System.Private.CoreLib/src/System/Threading/WaitHandle.Windows.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                src/libraries/System.Private.CoreLib/src/System/Threading/WaitHandle.Windows.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                src/libraries/System.Private.CoreLib/src/System/Threading/WaitHandle.Windows.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                src/libraries/System.Private.CoreLib/src/System/Threading/WaitHandle.Windows.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
      Co-authored-by: jkotas <[email protected]>
        
          
                src/libraries/System.Private.CoreLib/src/System/Threading/WaitHandle.Windows.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                src/libraries/System.Private.CoreLib/src/System/Threading/WaitHandle.Windows.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                src/libraries/System.Private.CoreLib/src/System/Threading/WaitHandle.Windows.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                src/libraries/System.Private.CoreLib/src/System/Threading/WaitHandle.Windows.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                src/libraries/System.Private.CoreLib/src/System/Threading/WaitHandle.Windows.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
      …Handle.Windows.cs
        
          
                src/libraries/System.Private.CoreLib/src/System/Threading/WaitHandle.Windows.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
      Co-authored-by: jkotas <[email protected]>
        
          
                src/libraries/System.Private.CoreLib/src/System/Threading/WaitHandle.Windows.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
      | /azp run runtime-nativeaot-outerloop | 
| Azure Pipelines successfully started running 1 pipeline(s). | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements alertable waits for WaitHandle operations in NativeAOT to properly support Asynchronous Procedure Call (APC) handling, fixing a critical gap where user APCs queued to waiting threads would not be processed.
Key Changes
- Enabled alertable waits: Changed all WaitHandle operations from non-alertable (FALSE) to alertable (TRUE) waits
- Added APC retry logic: Implemented timeout-aware retry mechanisms that handle WAIT_IO_COMPLETIONresults properly
- Fixed SignalAndWait behavior: Corrected retry logic to prevent multiple signaling operations
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description | 
|---|---|
| src/libraries/Common/src/Interop/Windows/Kernel32/Interop.Threading.cs | Added missing WAIT_IO_COMPLETIONconstant andWaitForSingleObjectExinterop definition | 
| src/libraries/System.Private.CoreLib/src/System/Threading/WaitHandle.Windows.cs | Implemented alertable waits with APC retry logic and timeout adjustment for both multi-object and signal-and-wait scenarios | 
| src/tests/baseservices/threading/regressions/115178/115178.cs | Re-enabled previously disabled test for APC handling in NativeAOT | 
        
          
                src/libraries/System.Private.CoreLib/src/System/Threading/WaitHandle.Windows.cs
          
            Show resolved
            Hide resolved
        
              
          
                src/libraries/System.Private.CoreLib/src/System/Threading/WaitHandle.Windows.cs
          
            Show resolved
            Hide resolved
        
      …ed Thread.Interrupt Co-authored-by: jkotas <[email protected]>
| /azp run runtime-nativeaot-outerloop | 
| Azure Pipelines successfully started running 1 pipeline(s). | 
| /ba-g Linux Arm32 DeadLetter - Linux coverage is not relevant for this PR | 
This PR implements alertable waits for WaitHandle operations to properly support Asynchronous Procedure Call (APC) handling in NativeAOT, addressing a critical gap in threading functionality.
Background
Previously, WaitHandle operations in NativeAOT used non-alertable waits (
alertable: false), which meant that user APCs queued to a waiting thread would not be processed. This caused issues where:Test_wait_interrupted_user_apctest was failing and had to be disabled for NativeAOTChanges Made
1. Added WAIT_IO_COMPLETION constant
Added the missing
WAIT_IO_COMPLETIONconstant (0x000000C0) toInterop.Threading.csto handle APC completion results.2. Enabled alertable waits
Modified all wait operations to use alertable waits by changing the
alertableparameter fromFALSEtoTRUEin:WaitForMultipleObjectsExcalls (both NATIVEAOT and CoreCLR paths)RhCompatibleReentrantWaitAnycalls in NativeAOT reentrant scenariosSignalObjectAndWaitcalls in SignalAndWaitCore3. Implemented APC retry logic
Added proper handling for
WAIT_IO_COMPLETIONresults with timeout adjustment:Environment.TickCount64for precise timing calculationsWAIT_TIMEOUTwhen the original timeout period expires4. Fixed SignalAndWaitCore retry logic
Corrected the retry logic in
SignalAndWaitCoreto prevent multiple signaling:SignalObjectAndWaitonly once for the initial signal and waitWaitForSingleObjectExfor subsequent retries after APC completionWaitForSingleObjectExinterop definition to support this functionality5. Re-enabled disabled test
Removed the
ActiveIssueannotation fromTest_wait_interrupted_user_apcto re-enable the test that validates APC behavior during waits.Testing
The implementation follows the same retry pattern used in CoreCLR's
Thread::DoAppropriateWaitWorker, ensuring consistency across runtime implementations.Fixes #118233.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.