[PR #831] [MERGED] runtime: map early OS ERROR_TIMEOUT to _Timedout in _Cnd_timedwait_for_unchecked (RT-CRT-TIMED-WAIT-EARLY-TIMEOUT) #869

Closed
opened 2026-11-26 20:19:19 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/831
Author: @Maticcm
Created: 11/24/2026
Status: ✅ Merged
Merged: 11/24/2026
Merged by: @aslan2142

Base: main ← Head: fix/crt-timed-wait-early-timeout


📝 Commits (1)

  • bc9d0a5 runtime: map early OS ERROR_TIMEOUT to _Timedout in _Cnd_timedwait_for_unchecked

📊 Changes

2 files changed (+92 additions, -4 deletions)

View changed files

📝 runtime/src/crt_timed_wait.cpp (+25 -4)
📝 runtime/test/crt_timed_wait_test.cpp (+67 -0)

📄 Description

Claimed in the coordination issue as: RT-CRT-TIMED-WAIT-EARLY-TIMEOUT

What and why

Fixes intermittent crt_timed_wait failures on Windows where std::condition_variable::wait_for without a predicate returned std::cv_status::no_timeout early (observed at 199–200 ms on a 200 ms wait, failing in baseline -j 8 CTest and in 2/10 standalone runs on Windows 11).

runtime/src/crt_timed_wait.cpp implements _Cnd_timedwait_for_unchecked via msvcp140.dll!_Cnd_timedwait against a TIME_UTC deadline. Inside msvcp140.dll, if SleepConditionVariableSRW times out (ERROR_TIMEOUT) even a fraction of a millisecond before target, _Xtime_diff_to_millis2(target, &now) is still non-zero, so _Cnd_timedwait returns _Thrd_result::_Success. MSVC STL's std::condition_variable::wait_for (<condition_variable>) treats _Success as a notification/spurious wake and returns cv_status::no_timeout instead of checking steady_clock::now() < _Target and waiting out the deadline.

timedwait_for_impl now saves and clears LastError around _Cnd_timedwait (restoring the caller's LastError afterwards) and maps _Thrd_result::_Success with wait_error == ERROR_TIMEOUT back to _Thrd_result::_Timedout for _Cnd_timedwait_for_unchecked, while preserving checked semantics for _Cnd_timedwait_for. Added regression tests in runtime/test/crt_timed_wait_test.cpp covering 25 consecutive 10 ms unnotified wait_for calls, pre-existing ERROR_TIMEOUT across a notified wait, and LastError preservation on timeout.

Tested

  • scripts\build_runtime.ps1 (MSVC 19.50.35729.0, Windows SDK 10.0.26100.0, RelWithDebInfo): [260/260] incremental targets built cleanly (0 errors, 0 warnings).
  • runtime\build\crt_timed_wait_test.exe x 25 consecutive runs: 25/25 passed (RESULT: PASS, exit 0).
  • ctest --test-dir runtime/build -R "crt_timed_wait|vk_fence|d3d12_fence|d3d12_queue_wait|frame_pacer|xtaskqueue" --output-on-failure: 10/10 passed (100%).
  • python -I scripts/check_no_game_data.py runtime/src/crt_timed_wait.cpp runtime/test/crt_timed_wait_test.cpp: ok (2 file(s)).
  • python -m compileall -q make_game.py tools tests: exit 0.

GPUs tested: NVIDIA GeForce RTX 3080, driver 630.20, Windows 11 (CPU/CRT runtime change)

AI-assisted: yes

Known limitations: Tested on native Windows 11; preserves the msvcp140.dll!_Cnd_timedwait path for Wine 9 compatibility without touching _Mtx_t, not yet re-run under Wine 9 locally.

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a — no translator changes)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/831 **Author:** [@Maticcm](https://github.com/Maticcm) **Created:** 11/24/2026 **Status:** ✅ Merged **Merged:** 11/24/2026 **Merged by:** [@aslan2142](https://github.com/aslan2142) **Base:** `main` ← **Head:** `fix/crt-timed-wait-early-timeout` --- ### 📝 Commits (1) - [`bc9d0a5`](https://github.com/ubiquitarianism/leonida/commit/bc9d0a5f8f82ed31be2855bccf6ce44fe9c21f25) runtime: map early OS ERROR_TIMEOUT to _Timedout in _Cnd_timedwait_for_unchecked ### 📊 Changes **2 files changed** (+92 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/crt_timed_wait.cpp` (+25 -4) 📝 `runtime/test/crt_timed_wait_test.cpp` (+67 -0) </details> ### 📄 Description Claimed in the coordination issue as: RT-CRT-TIMED-WAIT-EARLY-TIMEOUT ### What and why Fixes intermittent `crt_timed_wait` failures on Windows where `std::condition_variable::wait_for` without a predicate returned `std::cv_status::no_timeout` early (observed at 199–200 ms on a 200 ms wait, failing in baseline `-j 8` CTest and in 2/10 standalone runs on Windows 11). `runtime/src/crt_timed_wait.cpp` implements `_Cnd_timedwait_for_unchecked` via `msvcp140.dll!_Cnd_timedwait` against a `TIME_UTC` deadline. Inside `msvcp140.dll`, if `SleepConditionVariableSRW` times out (`ERROR_TIMEOUT`) even a fraction of a millisecond before `target`, `_Xtime_diff_to_millis2(target, &now)` is still non-zero, so `_Cnd_timedwait` returns `_Thrd_result::_Success`. MSVC STL's `std::condition_variable::wait_for` (`<condition_variable>`) treats `_Success` as a notification/spurious wake and returns `cv_status::no_timeout` instead of checking `steady_clock::now() < _Target` and waiting out the deadline. `timedwait_for_impl` now saves and clears `LastError` around `_Cnd_timedwait` (restoring the caller's `LastError` afterwards) and maps `_Thrd_result::_Success` with `wait_error == ERROR_TIMEOUT` back to `_Thrd_result::_Timedout` for `_Cnd_timedwait_for_unchecked`, while preserving checked semantics for `_Cnd_timedwait_for`. Added regression tests in `runtime/test/crt_timed_wait_test.cpp` covering 25 consecutive 10 ms unnotified `wait_for` calls, pre-existing `ERROR_TIMEOUT` across a notified wait, and `LastError` preservation on timeout. ### Tested - `scripts\build_runtime.ps1` (MSVC 19.50.35729.0, Windows SDK 10.0.26100.0, RelWithDebInfo): `[260/260]` incremental targets built cleanly (0 errors, 0 warnings). - `runtime\build\crt_timed_wait_test.exe` x 25 consecutive runs: `25/25` passed (`RESULT: PASS`, exit 0). - `ctest --test-dir runtime/build -R "crt_timed_wait|vk_fence|d3d12_fence|d3d12_queue_wait|frame_pacer|xtaskqueue" --output-on-failure`: `10/10` passed (`100%`). - `python -I scripts/check_no_game_data.py runtime/src/crt_timed_wait.cpp runtime/test/crt_timed_wait_test.cpp`: `ok (2 file(s))`. - `python -m compileall -q make_game.py tools tests`: exit 0. GPUs tested: NVIDIA GeForce RTX 3080, driver 630.20, Windows 11 (CPU/CRT runtime change) AI-assisted: yes Known limitations: Tested on native Windows 11; preserves the `msvcp140.dll!_Cnd_timedwait` path for Wine 9 compatibility without touching `_Mtx_t`, not yet re-run under Wine 9 locally. ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a — no translator changes) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:19:19 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#869
No description provided.