Repository navigation
Dedupe extracted embedded native modules in compiled binaries - #29587
Merged
Jarred-Sumner merged 1 commit intoAug 18, 2026
Merged
Conversation
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.
Fixes #29585.
Problem
A
bun build --compilebinary that loads an embedded native library (bun:ffidlopenorprocess.dlopenon a.node) extracted a fresh copy to/tmpon every call and never cleaned it up. Each Worker re-extracted on its firstdlopen, so Worker-heavy servers leaked fast (the reporter measured ~14 GB/hour). The cause:resolve_embedded_file_to_bufnamed each tmpfile withnanoTimestamp()plus a per-VM counter, so no two calls ever produced the same path.Fix
Extraction is now stateless and idempotent. The on-disk file is the cache.
{tmpdir}/.bun-{euid}-{wyhash(contents):x}.{ext}. Same content gives the same path, so all dlopens, Workers, and restarts of the binary share one file. The euid keeps users on a shared/tmpapart.lstatatthe canonical name and reuse it if it is a regular file, owned by our euid, with the expected size.lstatat(notfstatat) makes a planted symlink fail theISREGcheck instead of being followed.rename(2)s it onto the canonical name. Racing Workers converge on one valid file. If the rename fails (a foreign owner squats the name on sticky/tmp), that call uses its scratch file.dlopendeletion inBunProcess.cppis removed on both platforms: deleting the shared file would defeat the dedup, and the Windows delete-on-reboot required admin rights and only ever marked the file in active use. The orphanedBun__unlinkexport is deleted with its last caller.Verification
test/regression/issue/29585.test.ts: one file after 10 dlopens, after a second process run, after deleting the file (re-extracts), and across 5 concurrent Workers. Fails pre-fix withExpected: 1 / Received: 10.test/napi/napi.test.ts(--compile): the.node/process.dlopenpath keeps a stable extracted count across two runs, on all platforms (verified on Windows Server 2019 x64).no test proof · iteration 55 · Platform-specific test(s) that do not run on this machine. Deferring to CI, which covers all platforms: test/napi/napi.test.ts test/regression/issue/29585.test.ts