Title: crypto.randomInt() can return the same value to a sync and an async caller · Issue #66595 · nodejs/node · GitHub
Open Graph Title: crypto.randomInt() can return the same value to a sync and an async caller · Issue #66595 · nodejs/node
X Title: crypto.randomInt() can return the same value to a sync and an async caller · Issue #66595 · nodejs/node
Description: Version v27.0.0-nightly20261007aa1bf2cc37 (main at 6d5e309), also v24.21.0 Platform Linux x64 Subsystem crypto What steps will reproduce the bug? const crypto = require('node:crypto'); const max = 2 ** 48 - 1; const syncValues = []; // T...
Open Graph Description: Version v27.0.0-nightly20261007aa1bf2cc37 (main at 6d5e309), also v24.21.0 Platform Linux x64 Subsystem crypto What steps will reproduce the bug? const crypto = require('node:crypto'); const max = ...
X Description: Version v27.0.0-nightly20261007aa1bf2cc37 (main at 6d5e309), also v24.21.0 Platform Linux x64 Subsystem crypto What steps will reproduce the bug? const crypto = require('node:crypto'); cons...
Opengraph URL: https://github.com/nodejs/node/issues/66595
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"crypto.randomInt() can return the same value to a sync and an async caller","articleBody":"### Version\n\nv27.0.0-nightly20261007aa1bf2cc37 (main at 6d5e309871), also v24.21.0\n\n### Platform\n\nLinux x64\n\n### Subsystem\n\ncrypto\n\n### What steps will reproduce the bug?\n\n```js\nconst crypto = require('node:crypto');\n\nconst max = 2 ** 48 - 1;\nconst syncValues = [];\n\n// The first randomInt() call finds the cache empty, so it is queued\n// while the cache is refilled asynchronously.\ncrypto.randomInt(max, (err, n) => {\n console.log('async value:', n);\n console.log('sync values:', syncValues);\n});\n\n// Let the refill job finish before the synchronous calls below.\nconst start = Date.now();\nwhile (Date.now() - start \u003c 50);\n\nfor (let i = 0; i \u003c 3; i++) syncValues.push(crypto.randomInt(max));\n```\n\n### How often does it reproduce? Is there a required condition?\n\nEvery time with the snippet above. In general it needs an asynchronous `randomInt()` call that finds the cache empty (the first call in the process, or after the 1024 cached values are used up) followed by synchronous `randomInt()` calls before the refill callback runs.\n\n### What is the expected behavior? Why is that the expected behavior?\n\nThe async value is independent of the sync values. Every `randomInt()` call should get its own CSPRNG bytes.\n\n### What do you see instead?\n\n```\nasync value: 59323658883457\nsync values: [ 59323658883457, 78518031191563, 206053154540152 ]\n```\n\nThe async callback receives the same value as the first sync call. Calls made after the callback also repeat the second and third sync values.\n\n### Additional information\n\n`randomInt()` keeps a 6 KiB cache. An async call that finds it empty queues itself and runs `randomFill()` on that cache. A sync call made before the job's callback runs sees the cache as still empty, refills the same buffer with `randomFillSync()`, and reads from offset 0. The callback then sets the offset back to 0 and replays the queued call, which reads those bytes again. The threadpool job and `randomFillSync()` can also write the buffer at the same time.\n\nThe 50 ms wait only makes the threadpool job finish before the sync refill. Without it the result depends on timing. This has been there since the cache was added in #35110.\n\nI have a fix and will open a PR.\n","author":{"url":"https://github.com/gengjiawen","@type":"Person","name":"gengjiawen"},"datePublished":"2026-10-08T07:37:30Z","url":"https://github.com/nodejs/node/issues/66595"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f34df810-7739-6721-322a-365b7cb95404 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8296:9286A:24E68EE:30EAE56:6B0339B9 |
| html-safe-nonce | 7711dff568e301772fd2bb65083ad92340c4d6c368e1bd2bc2eb8693b36c34e8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4Mjk2OjkyODZBOjI0RTY4RUU6MzBFQUU1Njo2QjAzMzlCOSIsInZpc2l0b3JfaWQiOiI0MDAzNjcxNTEzMjkwMTkzMzM3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | dce56afd901ce3197a00023f1c5d0ed7088fa3fca3701ab6375178741c88052d |
| hovercard-subject-tag | issue:5757917488 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/66595/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4a7adb4f5bc392bcca0bff02e2d0b319220d0c3828bf643d73854f1de4f9d775/nodejs/node/issues/66595 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4a7adb4f5bc392bcca0bff02e2d0b319220d0c3828bf643d73854f1de4f9d775/nodejs/node/issues/66595 |
| og:image:alt | Version v27.0.0-nightly20261007aa1bf2cc37 (main at 6d5e309), also v24.21.0 Platform Linux x64 Subsystem crypto What steps will reproduce the bug? const crypto = require('node:crypto'); const max = ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gengjiawen |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width