hle(posix): fix wait-all rollbacks, waitable timer rearms, and work cancellation #499

Merged
Sheffy007 merged 1 commit from upstream-pr/499 into main 2026-11-28 01:33:02 +00:00
Sheffy007 commented 2026-11-27 22:18:24 +00:00 (Migrated from github.com)

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


What this changes

Closes #490
Refs #456

Fixes three Win32 synchronisation and callback-cancellation bugs in crates/victory-hle/src/host_posix:

  • Waitable::give_back & sync::wait_registered (host_posix.rs, host_posix/sync.rs, victory-platform):
    • Leave manual-reset events untouched when rolling back a partial WaitForMultipleObjects(..., wait_all = 1) take (since try_take never consumed them), preserving any concurrent ResetEvent.
    • Bump EPOCH and wake concurrent WaitForMultipleObjects waiters whenever a rolled-back auto-reset Event, Semaphore, or freed Mutex becomes available again, while advancing the rolling-back waiter's seen epoch when no external signal intervened so it sleeps cleanly instead of spinning.
  • TimerObject / SetWaitableTimer / CancelWaitableTimer (host_posix/system.rs):
    • Replace per-arm std::thread::spawn + unlocked generation checks with a single lazy worker thread per TimerObject synchronised under Mutex<TimerState> + Condvar, retiring cleanly on Drop (CloseHandle) and rejecting null due or negative period_ms.
    • Serialising event.reset(), event.set(), re-arm, and cancellation under state prevents a preempted worker from an older arm from signalling a re-armed or cancelled timer early.
  • WaitForThreadpoolWorkCallbacks (host_posix/system.rs):
    • Forward cancel != 0 (fCancelPendingCallbacks) to PoolObject::wait_idle (matching WaitForThreadpoolTimerCallbacks).

How it was tested

  • cargo test --target x86_64-apple-darwin -p victory-hle --lib (including new tests for wait-all rollback wakeups, manual-reset preservation, waitable timer re-arm/cancel/close, and WaitForThreadpoolWorkCallbacks(..., 1))
  • cargo clippy --target x86_64-apple-darwin -p victory-platform --all-targets -- -D warnings
  • cargo test -p victory-hle -p victory -p victory-gpu -p victory-metal -p victory-shader -p xtask
  • cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo xtask policy (399 files ok)

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md
  • New diagnostics are behind a VICTORY_* variable and documented

cc @Olima34

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5533821604 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/499). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T22:18:24Z. --- ## What this changes Closes #490 Refs #456 Fixes three Win32 synchronisation and callback-cancellation bugs in `crates/victory-hle/src/host_posix`: - **`Waitable::give_back` & `sync::wait_registered` (`host_posix.rs`, `host_posix/sync.rs`, `victory-platform`)**: - Leave manual-reset events untouched when rolling back a partial `WaitForMultipleObjects(..., wait_all = 1)` take (since `try_take` never consumed them), preserving any concurrent `ResetEvent`. - Bump `EPOCH` and wake concurrent `WaitForMultipleObjects` waiters whenever a rolled-back auto-reset `Event`, `Semaphore`, or freed `Mutex` becomes available again, while advancing the rolling-back waiter's `seen` epoch when no external signal intervened so it sleeps cleanly instead of spinning. - **`TimerObject` / `SetWaitableTimer` / `CancelWaitableTimer` (`host_posix/system.rs`)**: - Replace per-arm `std::thread::spawn` + unlocked `generation` checks with a single lazy worker thread per `TimerObject` synchronised under `Mutex<TimerState> + Condvar`, retiring cleanly on `Drop` (`CloseHandle`) and rejecting null `due` or negative `period_ms`. - Serialising `event.reset()`, `event.set()`, re-arm, and cancellation under `state` prevents a preempted worker from an older arm from signalling a re-armed or cancelled timer early. - **`WaitForThreadpoolWorkCallbacks` (`host_posix/system.rs`)**: - Forward `cancel != 0` (`fCancelPendingCallbacks`) to `PoolObject::wait_idle` (matching `WaitForThreadpoolTimerCallbacks`). ## How it was tested - [x] `cargo test --target x86_64-apple-darwin -p victory-hle --lib` (including new tests for wait-all rollback wakeups, manual-reset preservation, waitable timer re-arm/cancel/close, and `WaitForThreadpoolWorkCallbacks(..., 1)`) - [x] `cargo clippy --target x86_64-apple-darwin -p victory-platform --all-targets -- -D warnings` - [x] `cargo test -p victory-hle -p victory -p victory-gpu -p victory-metal -p victory-shader -p xtask` - [x] `cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings` - [x] `cargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warnings` - [x] `cargo xtask policy` (399 files ok) ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] Guesses about undocumented console behaviour are marked in the code and in `docs/native-host.md` - [x] New diagnostics are behind a `VICTORY_*` variable and documented cc @Olima34
Olima34 commented 2026-11-27 22:20:05 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T22:20:05Z.


Thanks @Sheffy007. This may explain a freeze we couldn't pin down. In about 1 in 3 traced world runs on the preview branch, the game froze for 30 s at the first world frames until the title's own watchdog fired, with no GPU error and no command buffer in flight. Samples during the freeze show 46–47 title threads in WaitForSingleObject, 6 in SleepConditionVariableCS, and none in the host. It was timing-sensitive: much more frequent with VICTORY_TRACE_GPU_FAILS=1 (5 of 14) than without it (0 of 9).

A lost wake-up in a wait-all rollback, or a timer re-arm signalled early or late by a stale worker, fits that profile better than anything on the GPU side.

We'll add #499 to our next runs with the switch that made the freeze frequent, and post the freeze count with and without it here.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7229238727 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/499#issuecomment-7229238727). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-27T22:20:05Z. --- Thanks @Sheffy007. This may explain a freeze we couldn't pin down. In about 1 in 3 traced world runs on the preview branch, the game froze for 30 s at the first world frames until the title's own watchdog fired, with no GPU error and no command buffer in flight. Samples during the freeze show 46–47 title threads in `WaitForSingleObject`, 6 in `SleepConditionVariableCS`, and none in the host. It was timing-sensitive: much more frequent with `VICTORY_TRACE_GPU_FAILS=1` (5 of 14) than without it (0 of 9). A lost wake-up in a wait-all rollback, or a timer re-arm signalled early or late by a stale worker, fits that profile better than anything on the GPU side. We'll add #499 to our next runs with the switch that made the freeze frequent, and post the freeze count with and without it here.
Olima34 commented 2026-11-28 01:32:58 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-28T01:32:58Z.

Review: APPROVED


Check report (helping the maintainer clear the queue):

  • CI: check and macos-scripts green on 313c3fab.

  • Read: the whole diff.

    • Wait-all rollback: give_back no longer re-sets a manual-reset event (a concurrent ResetEvent is kept), says whether it restored anything, and a restore bumps the epoch and wakes other multi-waiters without waking the caller itself (no busy spin).
    • ReleaseMutex signals only when the mutex is actually free.
    • Waitable timers: one worker per timer, with re-arm, cancel and close serialised under the timer's state. A superseded deadline can't fire, and closing retires the worker. Null due or a negative period now fail with ERROR_INVALID_PARAMETER, as on Windows.
    • WaitForThreadpoolWorkCallbacks honours its cancel flag.
    • Each of these has a test.
  • Local check on current main (226645f9) merged with #454, this PR and #507: victory-hle tests pass on x86_64-apple-darwin, Windows clippy (-D warnings) clean, cargo xtask policy ok.

  • Game runs, from one of our test sessions (one windowed 300 s run each, Series S, preview b3c1a1b0 + #459 + #513, VICTORY_TRACE_GPU_FAILS=1 VICTORY_PASSES_PER_BUFFER=1):

    tree GPU errors end frames
    control 0 clean 2555
    + #499 + #483 0 clean 2432
    + #499 + #483 + #507 0 clean 2432

    No regression and no measurable gain. The freeze this may explain didn't happen in any of these runs, with or without it, so these runs can't confirm the fix.

Thanks @Sheffy007!

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6094913633 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/499#pullrequestreview-6094913633). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-28T01:32:58Z. **Review:** APPROVED --- Check report (helping the maintainer clear the queue): - **CI:** `check` and `macos-scripts` green on 313c3fab. - **Read:** the whole diff. - Wait-all rollback: `give_back` no longer re-sets a manual-reset event (a concurrent `ResetEvent` is kept), says whether it restored anything, and a restore bumps the epoch and wakes other multi-waiters without waking the caller itself (no busy spin). - `ReleaseMutex` signals only when the mutex is actually free. - Waitable timers: one worker per timer, with re-arm, cancel and close serialised under the timer's state. A superseded deadline can't fire, and closing retires the worker. Null `due` or a negative period now fail with `ERROR_INVALID_PARAMETER`, as on Windows. - `WaitForThreadpoolWorkCallbacks` honours its cancel flag. - Each of these has a test. - **Local check on current main (226645f9) merged with #454, this PR and #507:** `victory-hle` tests pass on x86_64-apple-darwin, Windows clippy (`-D warnings`) clean, `cargo xtask policy` ok. - **Game runs, from one of our test sessions** (one windowed 300 s run each, Series S, preview b3c1a1b0 + #459 + #513, `VICTORY_TRACE_GPU_FAILS=1 VICTORY_PASSES_PER_BUFFER=1`): | tree | GPU errors | end | frames | |---|---|---|---| | control | 0 | clean | 2555 | | + #499 + #483 | 0 | clean | 2432 | | + #499 + #483 + #507 | 0 | clean | 2432 | No regression and no measurable gain. The freeze this may explain didn't happen in any of these runs, with or without it, so these runs can't confirm the fix. Thanks @Sheffy007!
Sign in to join this conversation.
No reviewers
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!499
No description provided.