Repository navigation
Bun.build / Bun.Transpiler: remove unsafe from JSBundler, the completion task and JSTranspiler - #40405
Bun.build / Bun.Transpiler: remove unsafe from JSBundler, the completion task and JSTranspiler#40405Jarred-Sumner wants to merge 3 commits into
Conversation
|
Updated 12:59 PM PT - Sep 8th, 2026
✅ @Jarred-Sumner, your commit 756dc44dda31c14abe77e74f54d087daa180ff32 passed in 🧪 To try this PR locally: bunx bun-pr 40405That installs a local version of the PR into your bun-40405 --bun |
There was a problem hiding this comment.
I reviewed this PR and the bug hunt found no issues. Given the scope — splitting the completion task into three cross-thread halves, new OwnedTranspiler/TaskHop abstractions, reworked teardown/cancellation flow, C++↔Rust FFI signature changes, and a stated ~0.3 % Bun.build regression — a human pass on the ownership model and perf trade-off is still worthwhile.
What was reviewed: the BundleJob/BuildShared split and Box<C> handoff to the bundle thread (drop-unrun path, ticket release ordering in complete_on_bundle_thread); stop_for_vm_teardown for both Queued→Released and Started stages against the removed Releasing spin; OwnedTranspiler::with_call restore-on-drop and the with_mut job-copy-alive assert; the C++ plugin callback signature change (global replaces config) across all call sites and the split addError/answerCancelled HOST_EXPORTs.
Extended reasoning...
Overview
This PR continues the zero-unsafe programme across the JS-facing bundler surface: JSBundler.rs (27→1), js_bundle_completion_task.rs (26→0), JSTranspiler.rs (22→0). The completion task is split into a JS-thread JSBundleCompletionTask (RefCounted, build_ref self-keepalive, TaskHop delivery), a bundle-thread BundleJob (Box<C> owned and dropped there), and an Arc<BuildShared> (cancel/stage atomics, Guarded<Option<Ticket>>, outcome slot). Bun.Transpiler is rebuilt around a new OwnedTranspiler that owns its arena and lends a lifetime-narrowed TranspilerCall per host-fn call. The plugin bridge callbacks become typed HOST_EXPORTs taking &mut Resolve/&mut Load + &JSGlobalObject, and the C++ BundlerPlugin.config pointer is removed. New primitives: bun_event_loop::TaskHop, bun_threading::intrusive_link!, singleton::wake() (process-wide bundle-loop wake replacing per-task bundle_loop).
Security risks
None identified. No user-input parsing, auth, or crypto is touched. The changes are internal ownership/lifetime restructuring; the FFI surface is narrowed (typed references replace *mut c_void).
Level of scrutiny
High. This is a substantial architectural refactor of cross-thread memory ownership in a hot, memory-safety-critical path (Bun.build completion, plugin dispatch, VM teardown). It introduces several new abstractions (OwnedTranspiler with hand-justified unsafe impl Send/Sync on its inner, TaskHop, the three-way completion split), removes a synchronization state (Releasing spin) by changing the allocation model, and reshapes the teardown protocol. The author flags a ~0.3 % Bun.build instruction-count regression attributed to LTO layout perturbation — that is at the bar where a maintainer should weigh in. The PR is also stacked on #40383.
Other factors
The PR description is thorough and the test matrix is broad (bun-build-api, bundler_plugin/defer, transpiler, macros, worker-terminated-mid-build, ASAN clean). The bug-hunt pass examined the ticket-release ordering in complete_on_bundle_thread vs stop_for_vm_teardown, the OwnedTranspiler restore guard (correctly restores previous arena/log so nested with_call is safe), the new_job re-aiming to home arena/log (so a job created inside a with_call doesn't alias the call's temporaries), and the C++ side's addError/answerCancelled split — all checked out. No prior reviews on the timeline. Deferring because the design surface (new abstractions, perf trade-off, teardown protocol change) warrants human sign-off even absent concrete findings.
2e537c1 to
0b406e5
Compare
dda3c4e to
8d830b1
Compare
There was a problem hiding this comment.
Thanks for sweeping the vestigial { } blocks — confirmed gone at all three FileMap sites. I re-ran over the full diff and didn't find any bugs; given the scope (thread-hop ownership rework, unsafe impl Send on JobTranspiler, C++ plugin-bridge signature changes), a human pass is still worthwhile.
What was reviewed:
OwnedTranspiler'sArc<UnsafeCell<..>>aliasing contract and thewith_mutjob-copy-alive assert vs.new_jobretainer — no window where a liveJobTranspilerobserves reconfiguration.BundleThread::singleton::enqueue(Box<C>)drop-on-bundle-thread vs. the removedfree_released_unstarted— every enqueue path transfers ownership before the fallible wake.TaskHop/hop!/release_hop!wiring forJSBundleCompletionTaskagainststop_for_vm_teardown(ThisPtr)— cancel path settles the promise slot and dropsBuildSharedon the JS thread.JSBundlerPlugin.{cpp,h}host-export signatures vs. the Rust extern block — arity/types line up afterconfigpointer removal.
Extended reasoning...
Overview
This PR reworks ownership and thread-hop plumbing for the JS-facing bundler/transpiler API: JSBundleCompletionTask is split into a JS-side RefCounted half, a Send bundle-side BundleJob, and an Arc<BuildShared>; BundleThread now takes Box<C> and drops on the bundle thread; a new OwnedTranspiler wraps Transpiler<'static> in Arc<UnsafeCell<..>> with with_mut/with_call/new_job accessors and an unsafe impl Send on JobTranspiler; the C++ JSBundlerPlugin bridge drops the opaque config pointer in favor of HOST_EXPORTs taking &mut Resolve/&mut Load/&JSGlobalObject; and TaskHop/task_hop!/intrusive_link!/hop!/release_hop! primitives are added to type the concurrent-task dispatch. Net ~1500 lines churned across 20 files, with JSBundler.rs/JSTranspiler.rs/js_bundle_completion_task.rs heavily rewritten and 10 entries dropped from the vm-thread-door lint inventory.
Security risks
No user-facing input parsing or auth/crypto surface changes. The risk profile is entirely memory-safety and thread-affinity: Arc<UnsafeCell<Transpiler<'static>>> with hand-written Send/Sync bounds, Box<C> ownership transfer across threads with an AtomicPtr LOOP + wake(), and FFI signature changes on both sides of the Rust↔C++ plugin bridge. A mismatch in any of these is UAF/data-race territory rather than a security bypass, but it is exactly the class REVIEW.md flags as most-blocked.
Level of scrutiny
High. This is a large refactor of cross-thread ownership with new unsafe impl Send, an UnsafeCell aliasing contract enforced by convention + a debug assert, and coordinated changes to a C++ FFI boundary. The PR description's test matrix is thorough (ASAN+debug across the bundler/transpiler suites, hand-driven cancellation/teardown/concurrent runs, clippy, cross-target check), and the change is part of a series following an established pattern (#40055…#40399), but the invariants (JobTranspiler retainer keeps the aliased config alive; with_mut only when no job copies out; completion halves dropped on their owning threads) are the kind a maintainer should read the SAFETY comments for directly.
Other factors
Stacked on #40383 (base claude/modload-zero-unsafe), so a human needs to confirm the base landed before retarget anyway. The one prior inline nit from this bot (vestigial { } around the collapsed FileMap if let chain) was addressed in 610714e — verified at the remaining self.file_map.as_deref().and_then(...) sites. The earlier [COMMENTED] review under this app's identity at 04:24 is not verified as this bot's output and carries no state to block on. No outstanding third-party CHANGES_REQUESTED in the timeline.
0b406e5 to
2168fd0
Compare
aa66992 to
78e9475
Compare
bfa92b4 to
4a95c43
Compare
… exports, OwnedTranspiler JSBundleCompletionTask is split into the JS-thread side (ref-counted, a typed `build_ref` slot, completion delivered as a `TaskHop` on its `ThisPtr`, registered as an ActiveHandle), the bundle-thread `BundleJob` (Send by construction, owned and dropped by the bundle thread) and a shared `BuildShared` (cancel/stage atomics, the ticket, the outcome hand-back). The bundler sees them through `CompletionStruct` (now Box-owned) and a `CompletionDispatch` trait object; `file_map` and `optimize_imports` become owned (`Arc`) instead of lifetime-extended borrows, and the bundle thread exposes `singleton::wake()`. The C++ plugin bridge calls typed host exports (`&mut Resolve` / `&mut Load`, per-kind addError/answerCancelled, the global passed explicitly); `Resolve`/`Load` gain `answer`, `plugin` and `notify_deferred` next to `dispatch`. Bun.Transpiler holds an `OwnedTranspiler` (a Transpiler that owns its home arena; calls go through `with_call`, off-thread transforms take a `JobTranspiler` copy that keeps the shared storage alive), so JSTranspiler.rs, JSBundler.rs and js_bundle_completion_task.rs have no unsafe left.
…build's transpiler
78e9475 to
756dc44
Compare
What
Same programme as #40055 … #40399, applied to the JS-facing bundler API. Stacked on #40383 (base branch
claude/modload-zero-unsafe, forbun_jsc::Job/JobTranspiler); retarget to main once that lands. Bundler internals (bundle_v2/ParseTask/LinkerContext) are out of scope here — only the completion-task/plugin boundary is typed.src/runtime/api/JSBundler.rs27 → 1 (all-safe fnextern block),js_bundle_completion_task.rs26 → 0,JSTranspiler.rs22 → 0.JSBundleCompletionTaskis split into a JS-sideJSBundleCompletionTask(RefCounted, typedbuild_refslot, completion delivered via aTaskHopon itsThisPtr, registered asActiveHandle::Bundlefor VM teardown), a bundle-sideBundleJob(Sendby construction, owned and dropped by the bundle thread), andBuildShared(Arc: cancel/stage atomics,Guarded<Option<Ticket>>,Guarded<Option<Box<BuildOutcome>>>). The oldReleasingspin state is gone since the halves are separate allocations.bun_bundler::BundleThreadtakesBox<C>and drops it on the bundle thread;bundle_v2::dispatch::CompletionDispatchtrait +Arc<dyn CompletionDispatch>replace the vtable struct and opaque marker;BundleV2.file_map: Option<Arc<FileMap>>,BundleOptions.optimize_imports: Option<Arc<StringSet>>.JSBundlerPlugin__onResolveAsync/onLoadAsync/onDefer/addErrorResolve/addErrorLoad/answerCancelledResolve/answerCancelledLoadareHOST_EXPORTs taking&mut Resolve/&mut Load/&JSGlobalObject; C++JSBundlerPlugincallbacks take the global instead of the removedconfigpointer (JSBundlerPlugin__setConfigremoved);Resolve/Load::{answer, plugin(), notify_deferred}live on the bundler side, null-checked.Bun.TranspilerholdsJsCell<OwnedTranspiler>; per-call re-aiming throughwith_call/TranspilerCall(restricted handle, restores the previous arena/log so nested calls match);TransformTaskisSendby construction (tsconfig bits snapshotted at schedule;JobTranspilercopy viaOwnedTranspiler::new_job, with a job-copy-alive assert onwith_mut).bun_bundler::transpiler::{OwnedTranspiler, TranspilerCall}(OwnedTranspileris!Send/!Sync; innerSend/Syncwith SAFETY),BundleThread::singleton::{enqueue(Box<C>), wake},bun_threading::intrusive_link!,bun_event_loop::TaskHop+ dispatchhop!/release_hop!(same hunks as fetch: remove unsafe from FetchTasklet and fetch.rs #40202).Pre-existing issues noticed (unchanged):
Transpiler::for_workerdeep-clonesBundleOptionsper pool worker perBun.build(~4 % of bundle-thread instructions);html_build_task'sRefPtrstill leaks on the cancelled-at-teardown path.Perf (release,
taskset -c 56-63,perf stat -e instructions:u, interleaved):transpiler.transformSync×1e4 +0.01 %.Bun.buildof a 50-file project ×30 +0.32 %, ×100 +0.29–0.30 %; startup-only −0.15 %. Symbol-level sampling puts the delta inTranspiler::for_workerandeach_impl::<scan_imports_and_exports>on the bundle thread — neither changed here (consistent with LTO layout perturbation from theBundleOptionsfield-type change); the JS thread is < 1 % of the profile. This is at the 0.3 % bar, stated rather than rounded away.Testing
Debug+ASAN: bun-build-api 56/56, bundler_plugin 56, bundler_defer 12, native-plugin 19, plugins.test 47, bundler_compile 74/75 (
HelloWorldWithProcessVersionsBunfails identically on a main debug build — debug version string), bundler_compile_autoload 23, bundler_splitting 13, bun-build-compile 15, -sourcemap 9, -wasm 1, transpiler.test.js 190, repl-transform 36, tsconfig-uaf 3, error-gc-uaf 1, utf16-loader 5, parse-error-column 7, unsupported-loader 33, macro-test 22, decorators 24, decorator-metadata 5, jsx-tsconfig 2, bundler_html 22, bake/dev/plugins 3, esbuild/default 151/0, source lints. Hand-driven:Bun.build×200 sequential / ×20 concurrent (temporary counter: both halves back to 0), onLoad throw / reject / never-resolves-then-exit, onResolve external,.defer(),Bun.build({compile})(binary runs),files+optimizeImports, a Worker terminated mid-build ×5 + completed ×1 (all released),transform()×1e4 concurrent,transformSync/scan/scanImports/macros — exit 0, no ASAN output. clippy clean on bundler/runtime/threading/event_loop;rust-check-allwindows-msvc + aarch64-darwin pass.