Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 1, 2025

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:

  1. User APCs would not execute during wait operations
  2. The Test_wait_interrupted_user_apc test was failing and had to be disabled for NativeAOT
  3. Applications relying on APC-based async operations would not work correctly

Changes Made

1. Added WAIT_IO_COMPLETION constant

Added the missing WAIT_IO_COMPLETION constant (0x000000C0) to Interop.Threading.cs to handle APC completion results.

2. Enabled alertable waits

Modified all wait operations to use alertable waits by changing the alertable parameter from FALSE to TRUE in:

  • WaitForMultipleObjectsEx calls (both NATIVEAOT and CoreCLR paths)
  • RhCompatibleReentrantWaitAny calls in NativeAOT reentrant scenarios
  • SignalObjectAndWait calls in SignalAndWaitCore

3. Implemented APC retry logic

Added proper handling for WAIT_IO_COMPLETION results with timeout adjustment:

  • Uses Environment.TickCount64 for precise timing calculations
  • Calculates elapsed time and adjusts remaining timeout on each retry
  • Returns WAIT_TIMEOUT when the original timeout period expires
  • Maintains proper thread state management throughout retries

4. Fixed SignalAndWaitCore retry logic

Corrected the retry logic in SignalAndWaitCore to prevent multiple signaling:

  • Calls SignalObjectAndWait only once for the initial signal and wait
  • Uses WaitForSingleObjectEx for subsequent retries after APC completion
  • Added WaitForSingleObjectEx interop definition to support this functionality

5. Re-enabled disabled test

Removed the ActiveIssue annotation from Test_wait_interrupted_user_apc to re-enable the test that validates APC behavior during waits.

Testing

  • ✅ All 586 System.Threading tests pass with 0 errors and 0 failures
  • ✅ Custom timeout verification shows correct wait behavior (~100ms waits complete accurately)
  • ✅ Both single and multiple wait handle scenarios work correctly
  • ✅ Full library build completes successfully with no warnings or errors

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.

Copy link
Contributor

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

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] WaitHandle doesn't do alertable waits Implement alertable waits for WaitHandle to support APC handling in NativeAOT Aug 1, 2025
@Copilot Copilot AI requested a review from jkotas August 1, 2025 02:06
Copilot finished work on behalf of jkotas August 1, 2025 02:06
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
@Copilot Copilot AI requested a review from jkotas August 1, 2025 02:38
Copilot finished work on behalf of jkotas August 1, 2025 02:38
@Copilot Copilot AI requested a review from jkotas August 1, 2025 03:08
Copilot finished work on behalf of jkotas August 1, 2025 03:08
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot finished work on behalf of jkotas August 1, 2025 03:35
@jkotas
Copy link
Member

jkotas commented Aug 1, 2025

/azp run runtime-nativeaot-outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@Copilot Copilot AI requested a review from jkotas August 1, 2025 04:51
@jkotas jkotas marked this pull request as ready for review August 1, 2025 05:07
@Copilot Copilot AI review requested due to automatic review settings August 1, 2025 05:07
Copy link
Contributor

@Copilot Copilot AI left a 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_COMPLETION results 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_COMPLETION constant and WaitForSingleObjectEx interop 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

…ed Thread.Interrupt

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
@Copilot Copilot AI requested a review from jkotas August 1, 2025 22:23
Copilot finished work on behalf of jkotas August 1, 2025 22:23
@jkotas
Copy link
Member

jkotas commented Aug 1, 2025

/azp run runtime-nativeaot-outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jkotas
Copy link
Member

jkotas commented Aug 2, 2025

/ba-g Linux Arm32 DeadLetter - Linux coverage is not relevant for this PR

@jkotas jkotas merged commit fae90a6 into main Aug 2, 2025
160 of 167 checks passed
@jkotas jkotas deleted the copilot/fix-118233 branch August 2, 2025 06:20
@github-actions github-actions bot locked and limited conversation to collaborators Sep 1, 2025
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.

WaitHandle doesn't do alertable waits
4 participants