runtime: map early OS ERROR_TIMEOUT to _Timedout in _Cnd_timedwait_for_unchecked (RT-CRT-TIMED-WAIT-EARLY-TIMEOUT) #831
No reviewers
No assignees
Labels
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
No project
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
No due date set.
Reference
leonida/mandy!831
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/crt-timed-wait-early-timeout"
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?
Pull request successfully merged and closed
Claimed 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)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.
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.
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.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).