Repository navigation
crypto: avoid reusing randomInt() cache bytes - #66597
Open
gengjiawen wants to merge 1 commit into
Open
gengjiawen wants to merge 1 commit into
gengjiawen wants to merge 1 commit into
Conversation
Collaborator
|
Review requested:
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #66597 +/- ##
==========================================
+ Coverage 90.45% 90.48% +0.02%
==========================================
Files 791 791
Lines 276562 277097 +535
Branches 53111 53278 +167
==========================================
+ Hits 250154 250718 +564
+ Misses 16801 16799 -2
+ Partials 9607 9580 -27
🚀 New features to boost your workflow:
|
panva
reviewed
Oct 13, 2026
When an asynchronous randomInt() call finds the cache empty, it queues itself and starts randomFill() on the shared cache buffer. A synchronous call made before that job completes refills the same buffer with randomFillSync() and returns values from offset 0. The completion callback then reset the offset to 0, so the queued call and later calls returned the bytes that synchronous calls had already used. The threadpool job and randomFillSync() could also write the buffer at the same time. Fill a separate buffer asynchronously and copy it into the cache once the job is done. Fixes: nodejs#66595 Refs: nodejs#35110 Signed-off-by: Jiawen Geng <technicalcute@gmail.com> Assisted-by: claude:opus-5.5
gengjiawen
force-pushed
the
crypto-randomint-cache-reuse
branch
from
October 13, 2026 07:16
59e0b84 to
360f18a
Compare
panva
approved these changes
Oct 13, 2026
Collaborator
Collaborator
Collaborator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When an asynchronous
crypto.randomInt()call finds the cache empty, it queues itself and refills the shared cache withrandomFill(). A synchronous call made before the refill callback runs refills the same buffer withrandomFillSync()and takes values from offset 0. The callback then reset the offset to 0, so the queued call and later calls got the bytes that synchronous calls had already returned. The threadpool job andrandomFillSync()could also write the buffer at the same time.The asynchronous refill now fills a separate buffer, allocated on first use and reused after that. The callback copies it into the cache before resetting the offset. Synchronous calls and the threadpool never touch the same memory, and resetting the offset only hands out bytes nobody has used. The synchronous path is unchanged. The asynchronous path does one extra 6 KiB copy per refill (every 1024 values).
The new test makes the first
randomInt()call in a process asynchronous. It wrapsondoneof the refill job, which runs only after the threadpool has written the buffer, and makes synchronous calls there before the refill callback runs. It then checks that the async value and the values after the callback differ from the synchronous ones. Without the fix it fails every time, also when the threadpool is busy.Fixes: #66595
Refs: #35110
AI disclosure: written with the help of Claude (claude:opus-5.5). I checked the bug against the source and the latest nightly, and reviewed the fix and the test.