You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unexpected error 38 in std.os.windows.RtlGenRandom #23666
bugObserved behavior contradicts documented or intended behaviorcontributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.os-windowsMicrosoft Windowsstandard libraryThis issue involves writing Zig code for the standard library.
changed the title [-]Unexpected error 1453 in `std.os.windows.RtlGenRandom`[/-][+]Unexpected error 38 in `std.os.windows.RtlGenRandom`[/+]on Apr 26, 2025
My only other guess as to what might be happening is that calling this function causes a dll to be loaded (bcryptprimitives.dll, confirmed this with NtTrace), and that is what's failing in some way due to system resources running out, and that tangential error happens to be bubbling up here (however, still not sure how failing to load the dll would cause Reached the end of the file).
I'm not entirely sure that's how that would work, though, and not totally sure how to go about testing that theory.
After looking into this, IMO continuing to use std.os.windows.unexpectedError seems fairly appropriate here. Could maybe add a comment here like this for clarification:
// This function has been seen to fail in the wild with error code 38 (Reached the end of the file),
// but that happened while the system was running out of system resources and that failure
// has not been reproduced. Additionally, RtlGenRandom internally calls ProcessPrng which
// is documented to not be able to fail, so any error is truly unexpected.
I think this should be solved by swapping out our call of RtlGenRandom with ProcessPrng from bcryptPrimitives.dll which will avoid unnecessarily loading advapi32.dll.
Is there any lower level API from a different DLL that that calls into observable with procmon / NtTrace?
Although windows 7 is already older than our minimum supported windows, it wouldn't hurt to do a version check and call into RtlGenRandom when the target range is even wider than that. In that case I suggest mapping error code 38 to @panic with the justification that it's rare to hit and that's outside our minimum version supported anyway so we don't want it in the error set.
Is there any lower level API from a different DLL that that calls into observable with procmon / NtTrace?
No, ProcessPrng contains all the interesting implementation code.
Although windows 7 is already older than our minimum supported windows, it wouldn't hurt to do a version check and call into RtlGenRandom when the target range is even wider than that. In that case I suggest mapping error code 38 to @panic with the justification that it's rare to hit and that's outside our minimum version supported anyway so we don't want it in the error set.
I thought bcryptprimitives.dll was introduced in Windows 7? (And I don't think we need to go out of our way to support anything older than that.)
As far as I can tell from looking at the disassembly of the function (and what it calls), RtlGenRandom does not set the LastError value, so what is being reported is whatever stale value happens to be in there from a previous API call that does set it. Likely if you explicitly SetLastError before that call, that's what you will see reported instead of 38 or w/e. This is pretty normal for the Rtl* family of APIs (if it were to return an error code, it would do so via an NTSTATUS return instead).
bugObserved behavior contradicts documented or intended behaviorcontributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.os-windowsMicrosoft Windowsstandard libraryThis issue involves writing Zig code for the standard library.
Activity
[-]Unexpected error 1453 in `std.os.windows.RtlGenRandom`[/-][+]Unexpected error 38 in `std.os.windows.RtlGenRandom`[/+]alexrp commentedon Jun 4, 2025
It's not actually clear what this error means in this case, which makes mapping it to a Zig error difficult. @marler8997 @squeek502 any ideas?
squeek502 commentedon Jun 4, 2025
Not sure, it seems like it might be related to running out of system resources, as that CI run was also hitting errors like:
Not sure if it's helpful, but:
NTE_FAILalexrp commentedon Jun 4, 2025
I guess returning
error.SystemResourceswould be the way to go then. 🤷squeek502 commentedon Jun 4, 2025
If this StackOverflow answer is accurate then
RtlGenRandomcallsProcessPrngwhich is documented to not be able to fail 🙃squeek502 commentedon Jun 4, 2025
My only other guess as to what might be happening is that calling this function causes a dll to be loaded (
bcryptprimitives.dll, confirmed this with NtTrace), and that is what's failing in some way due to system resources running out, and that tangential error happens to be bubbling up here (however, still not sure how failing to load the dll would causeReached the end of the file).I'm not entirely sure that's how that would work, though, and not totally sure how to go about testing that theory.
squeek502 commentedon Jun 5, 2025
After looking into this, IMO continuing to use
std.os.windows.unexpectedErrorseems fairly appropriate here. Could maybe add a comment here like this for clarification:andrewrk commentedon Jun 11, 2025
I think this should be solved by swapping out our call of RtlGenRandom with ProcessPrng from bcryptPrimitives.dll which will avoid unnecessarily loading advapi32.dll.
Is there any lower level API from a different DLL that that calls into observable with procmon / NtTrace?
Although windows 7 is already older than our minimum supported windows, it wouldn't hurt to do a version check and call into RtlGenRandom when the target range is even wider than that. In that case I suggest mapping error code 38 to
@panicwith the justification that it's rare to hit and that's outside our minimum version supported anyway so we don't want it in the error set.alexrp commentedon Jun 11, 2025
No,
ProcessPrngcontains all the interesting implementation code.I thought
bcryptprimitives.dllwas introduced in Windows 7? (And I don't think we need to go out of our way to support anything older than that.)mlugg commentedon Jun 11, 2025
Not that it frankly means much, but Microsoft's docs do indicate that

ProcessPrngis available on Windows 7:andrewrk commentedon Jun 11, 2025
Right, I'm saying it would be a fairly minimal maintenance burden to spend a couple lines handling when earlier than Windows 7 is targeted.
relapids commentedon Jun 17, 2025
As far as I can tell from looking at the disassembly of the function (and what it calls), RtlGenRandom does not set the LastError value, so what is being reported is whatever stale value happens to be in there from a previous API call that does set it. Likely if you explicitly SetLastError before that call, that's what you will see reported instead of 38 or w/e. This is pretty normal for the Rtl* family of APIs (if it were to return an error code, it would do so via an NTSTATUS return instead).
std.os.windows: map RtlGenRandom() failure to error.SystemResources