hle(posix): fix WaitForMultipleObjects rollback wakes, SetWaitableTimer rearm races, and WaitForThreadpoolWorkCallbacks cancel #490

Closed
opened 2026-11-27 22:08:03 +00:00 by Sheffy007 · 2 comments
Sheffy007 commented 2026-11-27 22:08:03 +00:00 (Migrated from github.com)
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T22:08:03Z.


Summary

Three Win32 synchronisation and callback-cancellation bugs in crates/victory-hle/src/host_posix can wake threads prematurely, strand concurrent multi-object waiters, or run cancelled work callbacks after their caller freed their context (Refs #456):

  1. Waitable::give_back (crates/victory-hle/src/host_posix.rs:643-653):

    • When WaitForMultipleObjects(..., wait_all = 1) rolls back a partial take, give_back restores auto-reset Event, Semaphore, and Mutex objects without calling sync::signalled(), leaving any concurrent WaitForMultipleObjects(..., wait_all = 0) waiter asleep on EPOCH.
    • give_back also calls e.set() unconditionally on manual-reset events (which try_take never consumed), overwriting a concurrent ResetEvent.
  2. SetWaitableTimer / CancelWaitableTimer (crates/victory-hle/src/host_posix/system.rs:1363-1463):

    • TOCTOU race between while live() (generation check) and timer.event.set(): a preempted worker from a superseded SetWaitableTimer or CancelWaitableTimer can call timer.event.set() after a new SetWaitableTimer incremented generation and called timer.event.reset(), signalling the re-armed timer early before its new due time elapses.
    • Each SetWaitableTimer call also spawns a fresh OS thread that stays blocked in std::thread::sleep across cancel, re-arm, and CloseHandle.
  3. WaitForThreadpoolWorkCallbacks (crates/victory-hle/src/host_posix/system.rs:1765-1768):

    • Ignores _cancel (fCancelPendingCallbacks), passing false instead of cancel != 0 to PoolObject::wait_idle (unlike WaitForThreadpoolTimerCallbacks right below it), so pending SubmitThreadpoolWork callbacks still run after WaitForThreadpoolWorkCallbacks(work, 1) returns.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6655223436 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/490). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T22:08:03Z. --- ## Summary Three Win32 synchronisation and callback-cancellation bugs in `crates/victory-hle/src/host_posix` can wake threads prematurely, strand concurrent multi-object waiters, or run cancelled work callbacks after their caller freed their context (Refs #456): 1. **`Waitable::give_back` (`crates/victory-hle/src/host_posix.rs:643-653`)**: - When `WaitForMultipleObjects(..., wait_all = 1)` rolls back a partial take, `give_back` restores auto-reset `Event`, `Semaphore`, and `Mutex` objects without calling `sync::signalled()`, leaving any concurrent `WaitForMultipleObjects(..., wait_all = 0)` waiter asleep on `EPOCH`. - `give_back` also calls `e.set()` unconditionally on manual-reset events (which `try_take` never consumed), overwriting a concurrent `ResetEvent`. 2. **`SetWaitableTimer` / `CancelWaitableTimer` (`crates/victory-hle/src/host_posix/system.rs:1363-1463`)**: - TOCTOU race between `while live()` (`generation` check) and `timer.event.set()`: a preempted worker from a superseded `SetWaitableTimer` or `CancelWaitableTimer` can call `timer.event.set()` *after* a new `SetWaitableTimer` incremented `generation` and called `timer.event.reset()`, signalling the re-armed timer early before its new `due` time elapses. - Each `SetWaitableTimer` call also spawns a fresh OS thread that stays blocked in `std::thread::sleep` across cancel, re-arm, and `CloseHandle`. 3. **`WaitForThreadpoolWorkCallbacks` (`crates/victory-hle/src/host_posix/system.rs:1765-1768`)**: - Ignores `_cancel` (`fCancelPendingCallbacks`), passing `false` instead of `cancel != 0` to `PoolObject::wait_idle` (unlike `WaitForThreadpoolTimerCallbacks` right below it), so pending `SubmitThreadpoolWork` callbacks still run after `WaitForThreadpoolWorkCallbacks(work, 1)` returns.
Sheffy007 commented 2026-11-27 22:08:04 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T22:08:04Z.


/claim

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7229057042 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/490#issuecomment-7229057042). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T22:08:04Z. --- /claim
github-actions[bot] commented 2026-11-27 22:08:15 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by github-actions[bot] on 2026-11-27T22:08:15Z.


Claimed by @Sheffy007. Open a draft pull request with Refs #490 in its description within 24 hours, or the claim expires.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7229059502 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/490#issuecomment-7229059502). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-27T22:08:15Z. --- Claimed by @Sheffy007. Open a draft pull request with `Refs #490` in its description within 24 hours, or the claim expires.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS#490
No description provided.