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

Closed
opened 2026-11-27 22:11:29 +00:00 by github-bot · 2 comments
Owner

@⁠Sheffy007 on GitHub · 2026-11-27 22:08 UTC · original

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 #460):

  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** on GitHub · 2026-11-27 22:08 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/490) ## 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 #460): 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 22:11:29 +00:00
Author
Owner

@⁠Sheffy007 on GitHub · 2026-11-27 22:08 UTC · original

/claim

**@⁠Sheffy007** on GitHub · 2026-11-27 22:08 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/490#issuecomment-7229057042) /claim
Author
Owner

@⁠github-actions[bot] on GitHub · 2026-11-27 22:08 UTC · original

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

**@⁠github-actions[bot]** on GitHub · 2026-11-27 22:08 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/490#issuecomment-7229059502) Claimed by @⁠Sheffy007. Open a draft pull request with `Refs #490` in its description within 24 hours, or the claim expires.
This discussion has been locked. Commenting is limited to contributors.
No description provided.