Repository navigation
Conversation
JsPoster was a {*const (), &'static JsPosterVTable} pair with five unsafe fn slots, hand-written Send/Sync impls, an unsafe from_raw constructor and manual Clone/Drop; bun_jsc filled the two vtables with nine trampolines that cast the pointer back to a leaked Arc<PosterData> or Arc<IsolatedPosterInner>. JsPoster is now a newtype over Arc<dyn JsPost>, where JsPost: Send + Sync is implemented directly by PosterData and IsolatedPosterInner, and JsPoster::new takes the Arc each implementation already allocated. The public post/embedded_work_* methods are unchanged, so no holder (spawn, bundler, ConcurrentPoster) changes. Arc<dyn JsPost> is the same two words as the old pair and keeps the niche Option<JsPoster> relied on, the heap object is the same Arc allocation as before, post is still one vtable dispatch, and clone/drop become the inline Arc count operations the trampolines performed behind an indirect call. Removes 15 unsafe blocks, 10 unsafe fns, the 5 unsafe fn pointer slots and both unsafe Send/Sync impls; thread safety of the poster is now implied by the impl types.
|
Updated 7:40 AM PT - Aug 11th, 2026
✅ @robobun, your commit eb6354b1fb5cad7c57dcbaa2492db70f8f4b1e8b passed in 🧪 To try this PR locally: bunx bun-pr 37518That installs a local version of the PR into your bun-37518 --bun |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (3)
WalkthroughChangesThe event loop replaces the unsafe raw-pointer JsPoster dispatch
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Comment |
There was a problem hiding this comment.
LGTM — mechanical replacement of a hand-rolled vtable with Arc<dyn JsPost>, net -75 lines and ~15 unsafe blocks removed with no behavior change.
What was reviewed
- Verified
JsPoster's public surface (post,embedded_work_*,Clone,Send/Sync) is preserved;Arc<dyn JsPost>auto-derivesSend + Syncvia the trait's supertrait bounds, which the compiler now checks per-impl. - Confirmed both impl bodies (
PosterData,IsolatedPosterInner) are byte-for-byte the old trampoline bodies; the isolated-loopembedded_work_*no-ops keep the same rationale comment. - Grepped for remaining references to
JsPosterVTable/JsPoster::from_raw(none) and for direct callers ofIsolatedPosterInner::postoutside the trait (none — onlyto_js_poster/newinevent_loop.rs).
Extended reasoning...
Overview
This PR replaces bun_event_loop::JsPoster's hand-rolled type erasure — a *const () + &'static JsPosterVTable pair with five unsafe fn slots, manual Clone/Drop impls calling through the table, hand-written unsafe impl Send/Sync, and nine trampolines in VmHandle.rs that cast the erased pointer back to a leaked Arc — with a standard Arc<dyn JsPost> trait object. PosterData and IsolatedPosterInner now impl JsPost directly, and JsPoster::new takes the same Arc they were already allocating (unsized coercion, no reallocation). Three files touched: src/event_loop/AnyEventLoop.rs (the trait + newtype), src/event_loop/lib.rs (re-export swap), src/jsc/VmHandle.rs (impls replace trampolines + vtable statics). 42 insertions, 117 deletions.
Security risks
None. This is an internal refactor of the dispatch mechanism for posting concurrent tasks between threads; no user input, parsing, auth, or external surface is involved. The change strictly reduces the trusted-unsafe surface: the Send + Sync guarantee that bun_event_loop previously asserted blindly about a type it could not name is now checked by the compiler at each impl JsPost site (PosterData is auto-Send + Sync via VmHandle: Arc<Shared> and LoopKind: Copy; IsolatedPosterInner retains its existing justified unsafe impls for the gated raw pointer).
Level of scrutiny
Medium — this is thread-safety-adjacent code (cross-thread task posting to a JS VM), but the change is a textbook "let the compiler generate the vtable" refactor with no logic touched. Each new trait method body is the old trampoline body minus the unsafe { &*data.cast::<T>() } recovery, and the old clone/drop fn-pointer slots (Arc::increment_strong_count / Arc::from_raw) become the semantically identical #[derive(Clone)] on Arc. The size/niche analysis in the PR description checks out: Arc<dyn Trait> is two words with a non-null niche, same as the old {data, vtable} pair, so Option<JsPoster> in Process/BundleV2 stays 16 bytes.
Other factors
JsPoster's caller-facing API (post,embedded_work_scheduled,embedded_work_finished,Clone) is unchanged, so none of the 9.post()sites or theOption<JsPoster>holders need edits — I grepped and found no remaining references to the removedJsPosterVTableorJsPoster::from_raw.IsolatedPosterInner::postmoved frompub(crate)inherent to trait impl; grep confirms its only external users go throughto_js_poster/new, so no call site breaks.- No new tests are expected here per REVIEW.md conventions: this is a behavior-preserving internal refactor whose invariants are compiler-checked, and the PR ran the existing spawn/spawnSync/worker-refusal/plugins suites (172 pass) that exercise both
JsPostimplementations, including theBUN_FEATURE_FLAG_FORCE_WAITER_THREADpath. - The isolated-loop no-op
embedded_work_*bodies preserve the explanatory comment verbatim, satisfying the "don't silently delete a load-bearing comment" rule.
…hs (#43840) ### Problem - `src/codegen/generate-classes.ts` still has the DOMJIT emitter (C++ signatures, `WithoutTypeChecks` wrappers, result-type asserts, Rust thunks). None of it can run: `define()` in `class-definitions.ts` has set `DOMJIT = undefined` on each field since #14005 (2024-09), and all 31 `*.classes.ts` files go through `define()`. - Nothing is left for it to bind: #35002 deleted each Rust `*_without_type_checks` fast path, and #36756 and #36903 removed the C++ leftovers. - A `DOMJIT:` option in a `.classes.ts` file does nothing. Five exist. ### Fix - Delete the emitter, the option type, the strip in `define()`, the five ignored blocks, and the stale notes next to them: 6 files, 273 lines removed. - The generated output is the same except for 94 empty `#if BUN_DEBUG`/`#endif` pairs in `ZigGeneratedClasses.h` and three DOMJIT-only `#include`s in `ZigGeneratedClasses.cpp`. `generated_classes.rs` is byte-identical. - Verified: the generator run before and after, `bun bd`, `tsc -p src`, and the tests in the Notes. - Self-reviewed: 13 concerns raised, 13 addressed (Notes). ### Background - DOMJIT is the JavaScriptCore fast path that lets the JIT call a host function with unboxed, type-checked arguments. The generator could emit a C++ signature and a Rust thunk for each method. - The hand-written DOMJIT users (`Buffer.alloc`, `performance.now`, `bun:ffi`) do not use this generator. They stay. - Earlier sweeps (#39249, #41169) called this removal a design call. This PR asks for that call alone. ### Downsides - To turn generated DOMJIT on again, a person must restore these paths from git history and write the Rust fast paths again. - No runtime cost found. Checked: the generated `.cpp`, `.h` and `.rs` differ only as Fix says. <details><summary>Notes</summary> **Removed** - `generate-classes.ts`: `DOMJITName`, `argTypeName`, `DOMJITType`, `DOMJITFunctionDeclaration`, `DOMJITFunctionDefinition`, `domJITTypeCheckFields`, `RustDOMJITArgType`. Also the `DOMJIT` branches in `zigExportName`, `propRow`, `renderDecls`, the `expectedResultType` asserts in the host-function wrapper, both Rust thunk loops, and the `DOMJITAbstractHeap.h`, `FrameTracers.h`, `DFGAbstractHeap.h` includes of the generated prologue. The destructures that named `DOMJIT` also lose the unused `cache` and `value` bindings. - `class-definitions.ts`: the `DOMJIT?:` option type and the two `.map()` calls in `define()` that erased it. - Ignored live blocks: `Crypto.randomUUID`, `Crypto.timingSafeEqual` (`crypto.classes.ts`), `ServerWebSocket.publishText`, `publishBinary` (`server.classes.ts`), `TextDecoder.decode` (`encoding.classes.ts`). - Stale notes: the commented-out `// DOMJIT: {` blocks with their crash notes on `sendText`, `sendBinary` (2023) and `getRandomValues` (#13470, 2024-08), and three orphan "DOMJIT fast path" comments in `src/runtime/webcore/Crypto.rs` whose functions #35002 deleted. **Self-review, and what changed because of it** - The first draft mixed this design call with 22 log scopes and seven fields. It now ships alone, with its history in the body. - The leftover DOMJIT notes (`Crypto.rs`, the commented-out blocks) are folded in. - Three deletions that open PRs carry were dropped (#40232, #41385). - Five items that open PRs use were taken out of the held branch (#43283, #31855, #42819, #39222, #37518). Two `builtins.d.ts` lines were dropped too: `src/codegen/replacements.ts` defines `$ImportKindLabelToId`, so that declaration is live. **History** - #13470 (2024-08) turned DOMJIT off for `getRandomValues`. #14005 (2024-09) added the strip in `define()` as the repair for the #14001 segfault. #35224 found the cause (the wrappers returned `{ result }` with a null exception slot) and tried to repair the generated wrappers. A stale-PR cleanup closed it with no maintainer comment. #35002 deleted the Rust fast paths, so the option cannot come back without new native code. **Kept on purpose** - The hand-written `DomCall` path for `bun:ffi` (`src/jsc/host_fn.rs`, `src/runtime/ffi`), the C++ DOMJIT signatures in `JSBuffer.cpp`, `JSPerformance.cpp`, `NodeVM.cpp`, `JSSQLStatement.cpp`, and `test/js/bun/jsc/domjit.test.ts`. **Tests (debug build)** - `test/js/web/encoding/text-decoder.test.js` 127 pass, `test/js/web/web-globals.test.js` 23 pass, `test/js/bun/util/randomUUIDv5.test.ts` 40 pass, `test/js/bun/websocket/websocket-server.test.ts -t sendBinary` 5 pass. - `websocket-server.test.ts -t "publish|send"`: 44 pass, 4 time out near 19 s under debug+ASAN. A debug binary built from main fails the same 4. - `test/js/bun/jsc/domjit.test.ts`: 40 pass, 10 time out at the 100k-iteration sizes. A debug binary built from main gives the same 40/10. **The rest of this sweep** - Relink of the debug build with `-Wl,--gc-sections`, then the DWARF line table of the result: 2,290 of 31,118 Rust `fn`s have no live line. After `cargo check` on six targets only three `pub fn`s had no caller anywhere. The 96 trait impls with no caller are the ones #43664 kept on purpose. - clang `-fsyntax-only -Wunused-function -Wunused-template -Wunused-member-function -Wunused-macros` over bun's 177 C/C++ translation units (the build passes `-Wno-unused-function`): ten hits. Open PRs delete them, or an `#if` uses them. - oxlint `no-unused-vars` over `src/js`, `src/codegen`, `scripts`, `packages`. cargo's `unused_dependencies` lint over four targets. A scan for commented-out blocks (62 lines in the repo). Nothing new that is certain. - Each deletion was compared with the diffs of the 36 open dead-code PRs. Left out because an open PR has it: `Bun__napi_get_version` (#40232), two unused generator locals (#41385). **Verified and held for the next run** (branch `robobun/9a0817f5/dead-code-scopes-fields`, 25 files, 81 lines removed) - 20 `declare_scope!` scopes that nothing logs to: `JSC`, `STR`, `Bundle` and `scan_counter` (outer pair), `Store`, `hot_reloader`, `CLI`, `LibUVBackend`, `ResolveInfoRequest`, `GetHostByAddrInfoRequest`, `CAresNameInfo`, `GetNameInfoRequest`, `CAresReverse`, `CAresLookup`, `quic_session`, `PathWatcherManager`, `S3Client`, `S3Stat`, `AWS`, `uws` (`uws_sys/socket.rs`). rustc does not lint an item that another crate's macro expands. - Fields: `Runtime::Features.jsx_optimization_inline` with the local `can_be_inlined`, `DebugOptions.output_file`, `ArchiveIterator.filter`, `PackageManager.total_scripts`, `CommandLineArguments.lockfile`, `ArgumentsSlice::_vm`. Also `struct_Channeldata` and two empty modules in `napi_body.rs`. - It passed `rust:check-all` (12 targets), release and `--cfg bun_debug --cfg bun_asan` checks on linux, windows and darwin, and `cargo check --tests` before the trim below. The trimmed commit passes `cargo check` on linux. - Taken out because an open PR uses the item: `BundleOptions.code_coverage` (#43283 reads it), scope `ModuleLoader` (#31855), scope `PROCESS` (#42819, #39222), `impl Clone for JsPoster` and its vtable slot (#37518 rewrites the vtable). **Follow-up candidates, not verified dead** - `Parser Options.preserve_unused_imports_ts` is never `true`. tsconfig `importsNotUsedAsValues` is parsed into `preserve_imports_not_used_as_values` but never reaches the parser, in the released binary too. This looks like a missing feature. - `completions/bun-cli.json` (4,513 lines) and `misctools/generate-cli-completions.ts` (728 lines): nothing in the repo reads the JSON, but feature PRs still edit it by hand. - `bench/snippets/runner-entrypoint.js` (244 lines): no reference, first line says "this isn't done yet", last real change 2023-05. - Ten `impl_timer_owner!` accessors have no caller because `dispatch.rs` recovers the owner with its own `owner!` macro. Which mechanism stays is a design call. - `mordant-baseline.toml` still counts about 170 `unused_pub` findings (`sys/lib.rs` 56, `libuv_sys/libuv.rs` 41, `errno/windows_errno.rs` 31). `bun run rust:mordant` names them. </details>
|
Closing this PR because it has been inactive for more than 90 days. |
What
bun_event_loop::JsPosteris how crates belowbun_jsc(spawn's waiter thread, the bundler's JS-loop hops,ConcurrentPoster) post aConcurrentTaskto a JS VM from another thread. It was a type-erased pair built by hand: a*const ()plus a&'static JsPosterVTablewith fiveunsafe fnslots (post,embedded_work_scheduled,embedded_work_finished,clone,drop), hand-writtenunsafe impl Send/Sync, anunsafe fn from_rawconstructor, and manualClone/Dropimpls that called through the table.bun_jsc::vm_handlefilled two such tables with nine trampolines that cast the pointer back to anArc<PosterData>orArc<IsolatedPosterInner>leaked withArc::into_raw(Arc::increment_strong_countfor clone,Arc::from_rawfor drop).PosterDataandIsolatedPosterInnernow implementJsPostdirectly (the isolated loop's twoembedded_work_*methods are the explicit no-ops the old table pointed at, with the same comment), and theirto_js_posterconstructors pass theArcthey already allocated toJsPoster::new. The two vtable statics and nine trampolines inVmHandle.rsare deleted,lib.rsexportsJsPostin place ofJsPosterVTable, andJsPoster's publicpost/embedded_work_scheduled/embedded_work_finished/Clonesurface is unchanged, so none of its holders (bun_spawn::Process,bun_bundler::BundleV2,bun_jsc::ConcurrentPoster, the 9.post()call sites) change. Net: 15unsafeblocks, 10unsafe fns, the 5unsafe fnpointer slots and bothunsafe impls removed; none added. 42 insertions, 117 deletions across 3 files.Why
The trait object carries what the comments used to: the pointee is an
Arcthat outlives every holder (Arc<dyn JsPost>owns it), and the poster isSend + Syncbecause the implementing types are, which the compiler now checks at eachimpl JsPostinstead ofbun_event_loopasserting it about a type it cannot see. The erasure itself stays becausebun_event_loopsits belowbun_jscand cannot name the implementations. Cost:Arc<dyn JsPost>is the same two words (16 bytes) as the old{data, vtable}pair and keeps the non-null niche, soOption<JsPoster>inProcessandBundleV2stays 16 bytes; the unsized coercion inJsPoster::newdoes not reallocate, so the heap object is the very sameArcallocation the trampolines operated on;postandembedded_work_*are still a single indirect call each, now straight into the impl method (the dispatch additionally reads the payload alignment from the same vtable line to locate the value inside theArc, four ALU instructions on x86-64, on a path that then performs two SeqCst atomics, a queue push and a wakeup once per completed off-thread job); andclone/dropbecome the inlineArccount operations that previously sat behind an indirect call. No new allocation, check or branch on any path.Part of a series of small type-system hardening changes; each PR stands alone.
Verification
cargo checkandcargo clippyare clean for the touched crates. Debug build succeeds. bun bd test test/js/web/workers/worker-refused-completion.test.ts test/js/bun/spawn/spawnSync.test.ts test/js/bun/spawn/spawn.test.ts test/bake/dev/plugins.test.ts: 172 pass, 8 skip, 0 fail across 4 files (spawn.test.ts also re-runs itself with BUN_FEATURE_FLAG_FORCE_WAITER_THREAD: 139 pass, 7 skip, 0 fail).