Skip to content

[Mono] Fix wait's returning false on non-runtime queued user APC's. #116001

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

Merged
merged 5 commits into from
May 28, 2025

Conversation

lateralusX
Copy link
Member

Mono's internal interrupt handling uses user APC to break waits in case runtime needs to interrupt threads. If that happens, wait will return false. For API's like ManualResetEventSlim.Wait() with void return, this can cause issues if the wait is broken prematurely. If a user APC have been queued to the thread, not triggered by internal runtime interrupt handling, it would lead to unexpected side effects.

Fix makes sure runtime ignores returns from WaitForSingleObjectEx/WaitForMultipleObjectsEx due to APC completions not initialized by runtime.

Fix also adds a new runtime test to trigger this scenario for Mono runtime on Windows.

Fixes #115178.

@Copilot Copilot AI review requested due to automatic review settings May 26, 2025 18:32
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label May 26, 2025
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 ensures that waits aren’t broken by non-runtime queued user APCs in Mono on Windows and adds a new regression test for this scenario.

  • Introduce a new isolated test project and test (115178) to validate user APC behavior.
  • Change mono_win32_leave_* functions to return a boolean flag and update their implementations.
  • Update win32_wait_for_single_object_ex and win32_wait_for_multiple_objects_ex to loop on WAIT_IO_COMPLETION, adjust timeouts, and ignore non-runtime APCs.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/tests/baseservices/threading/regressions/115178/115178.csproj Add a process-isolated test project for the new regression.
src/tests/baseservices/threading/regressions/115178/115178.cs Implement new regression test for user APC–interrupted waits.
src/mono/mono/utils/mono-threads.h Change prototypes of leave functions to return gboolean.
src/mono/mono/utils/mono-threads-windows.c Update leave implementations to return alert status.
src/mono/mono/utils/mono-os-wait-win32.c Add looping logic around WAIT_IO_COMPLETION and time accounting.
Comments suppressed due to low confidence (1)

src/mono/mono/utils/mono-os-wait-win32.c:65

  • [nitpick] The enter-wait macro uses 'interrupted' but the leave-wait uses 'alerted'; consider unifying on one term (e.g. 'alerted') to reduce cognitive load.
gboolean interrupted = FALSE;

@lateralusX lateralusX requested a review from BrzVlad as a code owner May 27, 2025 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mono: ManualResetEventSlim.Wait() unexpectedly returns without blocking
4 participants