Repository navigation
Conversation
… a thread index fast_random() keeps one xoshiro256++ generator per thread. It seeded each generator with the same process seed, so every thread returned the same sequence. Each thread's generator now starts from the process seed plus a per-thread index that an atomic counter supplies. Two call sites worked around the repeated sequence. `bun lint` and `bun fmt` appended a counter to the names of their temporary files. The installer drew the `.old-` suffix of a link job on the main thread. Both workarounds are removed: the temporary name is the random number alone, and LinkBatch::link draws on the thread that links. The test watches the temporary names of one `bun fmt` run of 64 files and expects 64 distinct random parts. A second case pins the seed with BUN_DEBUG_HASH_RANDOM_SEED in builds that read it.
Contributor
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 1 minute. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Collaborator
Author
|
Updated 12:20 PM PT - Oct 25th, 2026
✅ @robobun, your commit f573743c01dacfbf1844aeb87dab02a9882370ff passed in 🧪 To try this PR locally: bunx bun-pr 46905That installs a local version of the PR into your bun-46905 --bun |
Collaborator
Author
|
Status: reproduced on main (179664e), on debug and release builds.
|
This branch has not been deployed
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.
Not the requested mechanism: this adds a per-thread index to the process seed, not
os_entropyper thread. The swap is one line. Please choose (Notes).Problem
bun_core::fast_random()returns the same sequence on every thread. No user-visible symptom is known.src/bun_core/util.rs:3521gives every thread's generator the one process seed. The Rust port made Zig's single generator thread-local.LinkJob.randomand the lintCOUNTsuffix. In open PRs:os_entropyat the call site (Native OpenTelemetry tracing (Bun.otel) #39965, Remove libuv on Windows #42819).Fix
init(random_seed().wrapping_add(THREADS.fetch_add(1, SeqCst))).test/cli/format/temporary-name.test.tsfails on main and passes here (debug and release). The existing test "two threads link at the same time" fails without the index.format.test.ts(Notes).Background
fast_random()is a non-cryptographic generator for temporary names, retry jitter and bundler keys.BUN_DEBUG_HASH_RANDOM_SEEDpins the process seed in builds with assertions.getrandomcall.os_entropyper thread matches Native OpenTelemetry tracing (Bun.otel) #39965 and Remove libuv on Windows #42819, but repeats under the pinned seed.Downsides
bun fmttemporary names and installer.old-names now rely on distinct sequences. A counter and a main-thread draw guaranteed them before..text+512 bytes, stripped binary +0 bytes. Onelock xaddruns per drawing thread.Notes
The choice of mechanism
The request (July) was: seed each thread from
os_entropy, and keep theBUN_DEBUG_HASH_RANDOM_SEEDoverride. This PR keeps the one process seed and adds an index that is never reused. In release builds the result is the same: each thread has its own sequence.os_entropyper threados_entropyreadsSEEDmemo inrandom_seed()os_entropycomment on main ("once per thread")random_seed()reados_entropyon every call and removeTHREADS. That is theutil.rshunk of bun_core: seed fast_random() per thread, not per process #35894.LinkBatchwould draw the same.old-name again.fastRandom()before the port had one seed and one generator for the process.Earlier attempts
os_entropyper thread, as requested. No maintainer reviewed it, and the stale action closed it. This PR supersedes it.The workarounds
LinkJob.random(install: consolidate the open package manager pull requests #44968, comment from install: fix regressions since 1.4.3 and resolve peer dependencies more like pnpm #45898) and theCOUNTsuffix intemporary_name()(Addbun lintandbun format#44805, comment frombun check: 1.4x to 7.8x faster on large projects. Fixes forbun check,bun lintandbun fmt#45800).IdRngin Native OpenTelemetry tracing (Bun.otel) #39965 and themkdtempname in Remove libuv on Windows #42819. Both reados_entropyat the call site and make it public. A review of #39965 says: "fast_random's per-thread seeding is worth fixing on its own".Conflicts with open PRs (one hunk each)
_ => rand::DefaultPrng::init(random_seed().wrapping_add(THREADS.fetch_add(1, O::SeqCst))),os_entropyLinkBatch::linkpassesjob.randomtomove_asidebun_core::fast_random()The test
fs.watch(inotify) records the temporary names of onebun fmtrun of 64 files. A name is.<16 hex digits of fast_random()>.tmp. The test expects 64 distinct numbers.bun fmttakes the largest file first, so one thread is still formatting it while the other threads write the rest. With 64 one-line files, one thread can write every file on a release build. Then the unfixed build passes: 76 of 200 cases at 1 CPU.isDebug || isASAN).Runs of the file. Every failure is the count assertion.
bun bd test test/cli/format/format.test.tsdoes not pass at the default timeout on main. One run had 29 timeouts, and "isn't corrupted when the run reaches it through two hard links" timed out when it ran alone. So a debug build cannot show that file fail on main and pass here. The test fits indescribe("a written file")if a maintainer prefers it there.The installer
bun-install-staging.test.ts, "two threads link at the same time" (5 rows, added withLinkJob.randomin install: consolidate the open package manager pull requests #44968). Release: 50 of 50 rows pass on main, and 150 of 150 here at 12 CPUs and at 2 CPUs. On a debug build of this PR withutil.rsas on main, the rows--forceandnode_modules has other versionsfail in 3 of 3 runs. The packages keep a file of the old copy.node_modules, with the seed pinned, on debug builds: main draws all 40.old-names on one thread. This PR draws them on 7 to 9 threads. All 40 names differ, and no file of the old version stays.Measurements (linux-x64, 12 CPUs, main 179664e against this PR)
fast_random, release (nm, objdump): the draw path has 49 instructions on both builds, instruction for instruction the same, none lock-prefixed. The whole symbol goes from 217 to 220 instructions and from 960 to 977 bytes. It has one lock-prefixed instruction, thefetch_add, which runs once per drawing thread..text+512 bytes. The sizes of.bssand.tbssdo not change (fast_random::THREADS+8,temporary_name::COUNT-4). The strippedbunhas 107,730,656 bytes on both builds.size_of::<LinkJob>(): 80 to 72 bytes (gdb, debug).os_entropycalls per process, counted as 8-bytegetrandomcalls by an LD_PRELOAD interposer, 20 runs, debug, main / this PR.bun -e 1: 0 / 0.bun fmt --check: 0 / 0.bun fmton 240 files, min-median-max: 1-1-4 / 1-1-5. A count above 1 is the first-draw race inrandom_seed(), which this PR does not change.bun fmt, distinct random parts among N temporary names, 5 runs each. Main, debug: 14 to 27 of 64 and 39 to 48 of 240. Main, debug, seed pinned: 16 to 25 and 40 to 49. Main, release: 20 to 43 and 44 to 72. This PR: N of N in 45 of 45 runs. Each run with the seed pinned to 42 has the first number of that seed (d0764d4f4476689f) exactly once.DefaultPrng: seeds 42, 43 and 44 start withd0764d4f4476689f,2b05935e6f17747dand004ffeb0127bf876, as the binary does. 1,024 thread indexes with 512 draws each give 524,288 distinct numbers. Two neighbouring indexes share no state word. A golden-ratio step would share 3 of 4, becauseinitsteps the seed by that constant.Suites (the
src/diff did not change after these runs)test/cli/format/format.test.tstest/cli/lint/lint.test.tstest/cli/install/hoist.test.tstest/cli/install/bun-prune.test.tstest/cli/install/bun-install-staging.test.tstest/cli/run/markdown-entrypoint.test.tstest/internal/source-lintsformat.test.tson release had 14 timeouts on main and 19 on this PR, on a loaded 12-CPU machine. No assertion failed.cargo clippyonbun_core,bun_installandbun_lint_driveris clean.Not changed
random_seed(): several threads can each read OS entropy and keep different seeds. It does no harm here, because the indexes differ. Startup snapshots (2/4): take/restore runtime and Bun.startupSnapshot #37260 rewrites those lines.random_seed(),FileSystem::tmpname(its timestamp XOR and its counter come from Zig), and therandomparameter ofmove_aside.no test proof · iteration 3 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/cli/format/temporary-name.test.ts