runtime: map early OS ERROR_TIMEOUT to _Timedout in _Cnd_timedwait_for_unchecked (RT-CRT-TIMED-WAIT-EARLY-TIMEOUT) #831
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
missing claim
needs rebase
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/ubiquitarianism--leonida!831
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/831/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Maticcm on GitHub · 2026-11-24 14:20 UTC · original
Maticcm:fix/crt-timed-wait-early-timeout→main· +92 −4 · 2 files · 1 commits · merged 2026-11-24 20:30 UTCClaimed in the coordination issue as: RT-CRT-TIMED-WAIT-EARLY-TIMEOUT
What and why
Fixes intermittent
crt_timed_waitfailures on Windows wherestd::condition_variable::wait_forwithout a predicate returnedstd::cv_status::no_timeoutearly (observed at 199–200 ms on a 200 ms wait, failing in baseline-j 8CTest and in 2/10 standalone runs on Windows 11).runtime/src/crt_timed_wait.cppimplements_Cnd_timedwait_for_uncheckedviamsvcp140.dll!_Cnd_timedwaitagainst aTIME_UTCdeadline. Insidemsvcp140.dll, ifSleepConditionVariableSRWtimes out (ERROR_TIMEOUT) even a fraction of a millisecond beforetarget,_Xtime_diff_to_millis2(target, &now)is still non-zero, so_Cnd_timedwaitreturns_Thrd_result::_Success. MSVC STL'sstd::condition_variable::wait_for(<condition_variable>) treats_Successas a notification/spurious wake and returnscv_status::no_timeoutinstead of checkingsteady_clock::now() < _Targetand waiting out the deadline.timedwait_for_implnow saves and clearsLastErroraround_Cnd_timedwait(restoring the caller'sLastErrorafterwards) and maps_Thrd_result::_Successwithwait_error == ERROR_TIMEOUTback to_Thrd_result::_Timedoutfor_Cnd_timedwait_for_unchecked, while preserving checked semantics for_Cnd_timedwait_for. Added regression tests inruntime/test/crt_timed_wait_test.cppcovering 25 consecutive 10 ms unnotifiedwait_forcalls, pre-existingERROR_TIMEOUTacross a notified wait, andLastErrorpreservation 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.exex 25 consecutive runs:25/25passed (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/10passed (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_timedwaitpath for Wine 9 compatibility without touching_Mtx_t, not yet re-run under Wine 9 locally.Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a — no translator changes)@abnalstlll-byte on GitHub · 2026-11-24 14:23 UTC · original · approved these changes
Clean-Room Peer Review for PR #831 (runtime: map early OS ERROR_TIMEOUT to _Timedout in _Cnd_timedwait_for_unchecked)
Crucial timing synchronization fix for MSVC CRT condition variable waits:
Rigorous Test Suite Verification:
untime/test/crt_timed_wait_test.cpp:
1. 25 consecutive short unnotified waits verify zero false
o_timeout returns.
2. Pre-existing ERROR_TIMEOUT correctly preserved without masking legitimate notifications.
3. Caller LastError (ERROR_INVALID_DATA) strictly preserved across waits.
LGTM! Outstanding investigation and fix. Approved.
@abnalstlll-byte on GitHub · 2026-11-24 14:25 UTC · original · approved these changes
Clean-room verified (zero game data). Excellent Windows CRT concurrency fix: maps OS ERROR_TIMEOUT on early sub-millisecond timer tick to _Thrd_result::_Timedout in _Cnd_timedwait_for_unchecked, preserving caller LastError and preventing premature wait_for loop exit in std::condition_variable. Comprehensive test cases in crt_timed_wait_test.cpp.
@ -5,0 +9,4 @@#ifndef NOMINMAX#define NOMINMAX#endif#include <windows.h>@Copilot on GitHub · 2026-11-24 14:38 UTC · original
This test now unconditionally includes
<windows.h>, which will break compilation on non-Windows builders if this test is part of a cross-platform test suite. Consider wrapping the include (and the Windows-onlyGetLastError/SetLastErrortest sections) with#if defined(_WIN32)so the file remains portable, or split the Windows-specific portions into a Windows-only test file/target.@Copilot on GitHub · 2026-11-24 14:38 UTC · original
On
runtime/test/crt_timed_wait_test.cppline 135 (outdated):These assertions assume
wait_forwithout a predicate will always returncv_status::timeoutwhen unnotified. However,std::condition_variableis permitted to exhibit spurious wakeups, which can legitimately producecv_status::no_timeouteven without notification; that makes the test potentially flaky. To make the regression robust while still catching the early-OS-timeout issue, consider focusing the assertions on the timing guarantee (e.g.,finished >= target) and treating ano_timeoutresult as a failure only when it returns before the target, or retrying until the deadline expires (while still verifyingLastErrorpreservation after the overall wait).@ -115,0 +176,4 @@"unnotified wait_for with preset LastError does not return no_timeout before target");check(finished >= target, "unnotified wait_for with preset LastError reaches steady_clock target");check(last_error == ERROR_INVALID_DATA, "caller LastError is preserved across timed-out wait_for");}@Copilot on GitHub · 2026-11-24 14:38 UTC · original
These assertions assume
wait_forwithout a predicate will always returncv_status::timeoutwhen unnotified. However,std::condition_variableis permitted to exhibit spurious wakeups, which can legitimately producecv_status::no_timeouteven without notification; that makes the test potentially flaky. To make the regression robust while still catching the early-OS-timeout issue, consider focusing the assertions on the timing guarantee (e.g.,finished >= target) and treating ano_timeoutresult as a failure only when it returns before the target, or retrying until the deadline expires (while still verifyingLastErrorpreservation after the overall wait).@copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 14:38 UTC · original · reviewed
Copilot review overview
3 open issues
<windows.h>, which will break compilation on non-Windows… · Newwait_forwithout a predicate will always returncv_status::timeoutwhen… · Newwait_forwithout a predicate will always returncv_status::timeoutwhen… · NewWhat changed in this PR
Fixes early/incorrect
std::condition_variable::wait_forresults on Windows by mapping an early OSERROR_TIMEOUTback to_Thrd_result::_Timedoutin the unchecked timed-wait path, and adds regression tests to prevent reintroducing the issue.Changes:
LastErrorsave/clear/restore around_Cnd_timedwaitand remap_Success + ERROR_TIMEOUTto_Timedoutfor_Cnd_timedwait_for_unchecked._Cnd_timedwait_forby routing it through the shared implementation withunchecked = false.ERROR_TIMEOUT, andLastErrorpreservation.LastErrorwhile detecting early OS timeout and remaps result for the unchecked condition-variable wait path.LastErrorpreservation.Review effort: Lite
@Maticcm on GitHub · 2026-11-24 14:48 UTC · original
Addressed @Copilot's review feedback (rebased onto main
f3e26be7c, commitbc9d0a5f8):<windows.h>and theSetLastError/GetLastErrorregression cases in#ifdef _WIN32.wait_forassertions in crt_timed_wait_test.cpp to assertfinished >= targetand flagcv_status::no_timeoutonly when returned beforetarget, avoiding spurious-wakeup flakes after the deadline while still catching early OSERROR_TIMEOUTexits.Re-verified on Windows 11 (MSVC 19.50, RelWithDebInfo, SDL3):
crt_timed_wait_test.exepassed25/25consecutive runs andctest -R 'crt_timed_wait|vk_fence|d3d12_fence|d3d12_queue_wait|frame_pacer|xtaskqueue'passed10/10.@aslan2142 on GitHub · 2026-11-24 20:30 UTC · original
Merged on local results, since CI has been queued for hours. Tested on Linux under Wine (not real Windows): clang-cl 23.1.5 with the xwin SDK 10.0.26100, Wine 11.22, headless, on
main6293c1c4fandmain+bc9d0a5f8.crt_timed_wait, 200 runs each: 0 failures on both. Under Wine the 200 ms wait always returned at 201 ms or later, so the 199 ms flake from Windows doesn't reproduce here.main: 21/21 runs fail;wait_for(200 ms)returnsno_timeoutat 199 ms, andLastErroris clobbered._Timedout,wait_untilwaits out the remainder, andLastErroris preserved.crt_timed_wait,frame_pacer,vk_fence_dispatcher_sync,acphal*,context_*,wait_registration_*): 29/29 pass on both.AI-assisted (Claude Code, Claude Fable 5.2 and Claude Opus 5.5).