From 4431ca28b86b2e8efad4a6340e0eac07f5a09ac5 Mon Sep 17 00:00:00 2001 From: Jake Greenfield Date: Tue, 24 Mar 2026 21:30:27 -0400 Subject: [PATCH] fix Windows clock handling Address two separate but related issues: - Return value of `RtlQueryPerformanceFrequency` was used inconsistently; it returns a nonzero value to indicate success. - `timeoutToWindowsInterval` wasn't converting absolute deadlines back to the Windows epoch before passing them to the system, which meant that values (in the Unix epoch) were always in the distant past, so sleeps would return immediately. Fixes #31653 --- lib/std/Io/Threaded.zig | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/std/Io/Threaded.zig b/lib/std/Io/Threaded.zig index 3899af5adc487cbf5a22b551eb8baf03bce472b3..e8c9bddc08d3ea873b8e9401059b314e563e67ab 100644 --- a/lib/std/Io/Threaded.zig +++ b/lib/std/Io/Threaded.zig @@ -11448,7 +11448,7 @@ fn clockResolution(userdata: ?*anyopaque, clock: Io.Clock) Io.Clock.ResolutionEr // https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntddk/ns-ntddk-kuser_shared_data // https://www.geoffchappell.com/studies/windows/km/ntoskrnl/inc/api/ntexapi_x/kuser_shared_data/index.htm var qpf: windows.LARGE_INTEGER = undefined; - if (windows.ntdll.RtlQueryPerformanceFrequency(&qpf).toBool()) { + if (!windows.ntdll.RtlQueryPerformanceFrequency(&qpf).toBool()) { recoverableOsBugDetected(); return .zero; } @@ -17677,7 +17677,8 @@ fn timeoutToWindowsInterval(timeout: Io.Timeout) ?windows.LARGE_INTEGER { .duration => |d| nowWindows(clock).addDuration(d.raw), .deadline => |d| d.raw, }; - return @intCast(@max(@divTrunc(deadline.nanoseconds, 100), 0)); + const epoch_ns = std.time.epoch.windows * std.time.ns_per_s; + return @intCast(@max(@divTrunc(deadline.nanoseconds - epoch_ns, 100), 0)); }, .awake, .boot => { const duration = switch (timeout) { -- 2.54.0