Skip to content

webcore: make object URL blob stores safe to share across workers - #31487

Open
Jarred-Sumner wants to merge 4 commits into
mainfrom
claude/blob-objecturl-thread-safety
Open

Jarred-Sumner wants to merge 4 commits into
mainfrom
claude/blob-objecturl-thread-safety

Conversation

@Jarred-Sumner

@Jarred-Sumner Jarred-Sumner commented May 27, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #30800

What

URL.createObjectURL stores a Blob in the process-global ObjectURLRegistry, and resolveObjectURL / fetch("blob:…") / new Worker("blob:…") handed that same backing Store out to every caller — including callers on other worker threads, since the registry is shared across workers. The store is mutable through any holder (new File([blob], name) rewrites stored_name, .text() caches the is-all-ascii flag, file-backed blobs cache stat results), so a blob resolved in one worker could observe renames made concurrently in another worker, and later resolutions reflected mutations made after registration — which Node does not do.

This PR makes both sides of the registry boundary hand out snapshots instead:

  • Store::deep_dupe() builds a fresh store: Bytes contents and stored_name are copied, File paths are re-owned with the lazily-derived stat fields reset, and S3 keys are re-owned while the (immutable) credentials are shared via Arc.
  • ObjectURLRegistry stores a snapshot at createObjectURL and returns a snapshot from every resolution, with the blob's content_type and name also owned by the copy. The registry's own entry is only touched under the registry mutex.
  • store::S3.credentials switches from Rc<S3Credentials> to Arc<S3Credentials>, since a store held by the registry is reachable from more than one thread.
  • The Send/Sync comments on Blob / StoreRef are rewritten to state this invariant.

Behavior notes

  • Object-URL resolution still returns the same contents, type, and name, and cross-worker resolution keeps working.
  • Mutations made to a blob after createObjectURL, or to a resolved blob (e.g. new File([resolved], "renamed.txt")), no longer show up in later resolutions. Node already behaves this way (it never reflects post-registration changes, and does not expose object URLs to worker_threads at all).
  • File-backed blobs resolved from an object URL re-stat on first size/lastModified access instead of reusing the registering blob's cached stat.

Memory tradeoff

Bytes-backed blobs are now copied once at URL.createObjectURL and once per resolution (resolveObjectURL, fetch("blob:"), new Worker("blob:")) instead of refcount-shared. Negligible for the common worker-bootstrap / small-payload cases, but for very large blobs kept in the registry it roughly doubles peak memory while both the registry entry and a resolved copy are alive. The lower-memory alternative is to keep sharing the store and make the shared fields safe in place (atomic is_all_ascii, has_one_ref()-gated stat caching, never rewriting a shared store's stored_name); that avoids the copies but keeps the store's data shared across threads and requires auditing every present and future data_mut() caller. Happy to switch to that shape if the copies are a concern.

Tests

  • test/js/node/buffer-resolveObjectURL.test.ts: resolutions stay isolated from later new File([blob], name) renames. Fails on 1.4.0 (the rename shows up in the next resolution), passes here. Matches Node.
  • test/js/web/workers/worker_blob.test.ts: one worker repeatedly renames/reads its resolved copies while another resolves the same URL and checks name/type/contents — a sequenced phase (fails on 1.4.0: the other worker's rename is visible) plus a bounded concurrent phase exercising simultaneous access from two workers.
  • Ran the workers, blob/fetch, structured-clone, and S3 suites with the debug build; bun run rust:check-all passes for all 10 targets.

URL.createObjectURL and resolveObjectURL/fetch(blob:)/new Worker(blob:)
previously shared one Blob store between the registry entry and every
caller, including callers on other worker threads. Renaming a blob via
new File([blob], name), the cached file stat, and the is-all-ascii flag
all wrote into that shared store, so a blob resolved in one worker could
observe (and race) mutations made in another, and later resolutions
reflected post-registration renames.

Hand out a deep-duped store, an owned content type, and an isolated name
on both sides of the registry boundary instead, so registry entries and
resolved blobs never share mutable state. Resolution still returns the
same contents, type, and name, and now matches Node's behavior of not
reflecting post-registration mutations into later resolutions.

Also switch store::S3.credentials from Rc to Arc, since blob stores are
reachable from more than one thread.
@robobun

robobun commented May 27, 2026 •

Copy link
Copy Markdown
Collaborator
Updated 8:54 PM PT - May 27th, 2026

✅ @autofix-ci[bot], your commit 6b3e7905a6ca8cfbdd1ccde941f485d0149407ed passed in Build #58552! 🎉


🧪   To try this PR locally:

bunx bun-pr 31487

That installs a local version of the PR into your bun-31487 executable, so you can run:

bun-31487 --bun

@github-actions

Copy link
Copy Markdown
Contributor

Found 1 issue this PR may fix:

  1. StoreRef Sync is unsound: data_mut(&self) enables mutable aliasing via safe API #30800 - Directly addresses the StoreRef Sync unsoundness by ensuring the ObjectURLRegistry never shares a Store across threads (deep-copies on register and resolve instead)

If this is helpful, copy the block below into the PR description to auto-close this issue on merge.

Fixes #30800

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented May 27, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

This PR introduces Store::deep_dupe() to create thread-safe, isolated blob snapshots with independently owned payloads. ObjectURLRegistry uses this mechanism when registering and resolving blobs so blobs returned by resolveObjectURL remain isolated from concurrent store mutations. Tests validate isolation in single-thread and multi-worker scenarios.

Changes

Cross-thread blob isolation mechanism

Layer / File(s) Summary
Store::deep_dupe() implementation and Send trait documentation
src/jsc/webcore_types.rs
Adds Store::deep_dupe(&self) → StoreRef to create a fresh heap-allocated Store with copied Bytes payload, re-owned pathlike data for File and S3 variants, and re-allocated S3 credentials. Expands safety docs for unsafe impl Send for Blob and unsafe impl Send for StoreRef to clarify ownership discipline and cross-thread dereferencing rules.
ObjectURLRegistry cross-thread blob duplication
src/runtime/webcore/ObjectURLRegistry.rs
Adds dupe_for_cross_thread, dupe_without_store_snapshot, and snapshot_store to snapshot registered blobs by calling deep_dupe on the underlying store, ensuring owned content_type and thread-safe name. Rewrites resolve_and_dupe to seed the resolving thread's global_this and snapshot the store before returning an isolated Blob.
Callsites: pass per-thread global to resolver
src/runtime/jsc_hooks.rs, src/runtime/webcore/fetch.rs
Updates loader and VM callsites to pass the VM/global reference into ObjectURLRegistry::resolve_and_dupe(...) so the resolving thread seeds global_this before snapshotting the Store.
Test coverage for resolved URL isolation and concurrency
test/js/node/buffer-resolveObjectURL.test.ts, test/js/web/workers/worker_blob.test.ts
Node test validates that resolveObjectURL returns isolated blobs even when source data mutates. Worker test runs concurrent reader/writer workers resolving the same URL and asserts readers observe consistent metadata and content without observing writer mutations.

Possibly related PRs

  • oven-sh/bun#30955: Related changes addressing Blob name ownership and duplication semantics that interact with Store deep-copy and object URL handling.
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically summarizes the main change: making object URL blob stores safe to share across workers.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The PR description is well-structured and comprehensive, covering all required sections with detailed technical information.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

- deep-copy S3 credentials in Store::deep_dupe instead of sharing the Rc
- take the store snapshot after releasing the registry lock in resolve
- point resolved blobs at the resolving global
@Jarred-Sumner

Copy link
Copy Markdown
Collaborator Author

Fixed in e5f8ef2 by dropping the Rc→Arc change entirely (option b). S3Credentials holds a non-atomic intrusive RefCount (Cell + thread-lock), so Arc was both lint-rejected and not actually sound. Store::deep_dupe now deep-copies the credentials value into a fresh Rc owned solely by the snapshot (S3Credentials::Clone already deep-copies its Box<[u8]> fields and resets the refcount), so the registry copy still shares no state with the source blob; all other Rc usage is reverted to match main. cargo clippy -p bun_jsc -p bun_runtime is clean locally.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/runtime/webcore/ObjectURLRegistry.rs`:
- Around line 109-126: resolve_and_dupe currently returns a Blob still seeded
with the registry entry's stale global_this; change resolve_and_dupe to accept a
&JSGlobalObject (or a pointer) so it can bind the resolving global: after
creating copy via dupe_without_store_snapshot(&map.get(&uuid.bytes)?.blob) set
copy.global_this to std::ptr::from_ref(global_object) before calling
snapshot_store(&copy) and returning it, then update resolve_and_dupe_to_js to
call the new resolve_and_dupe(...) and remove the manual global_this patch
there; keep references to uuid_from_pathname, dupe_without_store_snapshot,
snapshot_store, Blob, resolve_and_dupe_to_js and map.get when making the edits.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 95e942fc-b6b7-4555-8b0a-eb96028ffcf3

📥 Commits

Reviewing files that changed from the base of the PR and between 20f023b and e5f8ef2.

📒 Files selected for processing (2)
  • src/jsc/webcore_types.rs
  • src/runtime/webcore/ObjectURLRegistry.rs

@Jarred-Sumner Jarred-Sumner left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct. We should fix the specific fields which are not threadsafe. The RefCount for Store is threadsafe. We should not need to clone the bytes.

@Jarred-Sumner

Copy link
Copy Markdown
Collaborator Author

@robobun adopt

@robobun

robobun commented May 29, 2026 •

Copy link
Copy Markdown
Collaborator

✅ Adopted & verified — cross-worker object-URL isolation fix holds up end to end.

Gate (debug/ASAN build, both ways):

  • Reverted the src/ changes → both tests fail: node resolveObjectURL sees second.name = "renamed-from-original.txt" (leaked rename through the shared store), and the worker test sees the writer's renamed-0.txt cross-worker.
  • Restored the fix → all 10 tests pass. Worker isolation test is stable across repeated runs.

Root cause confirmed: new File([bytesBlob], name) shares the backing Store by refcount and rewrites its stored_name in place, so a registry that handed out a shared StoreRef leaked renames across resolutions and across workers. Store::deep_dupe on both register and resolve gives each side a private store.

Linked the issue it closes (Fixes #30800 — the StoreRef: Sync mutable-aliasing report). All review threads resolved, CI green. Ready to merge.

@robobun

robobun commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

Note from deduping the #30800 PRs: this branch keeps unsafe impl Sync for StoreRef and the safe data_mut(&self) -> &mut Data (it updates the comments on them), so merging it would auto-close #30800 without changing the API the issue is about. #30812 is the PR kept for that issue; #30805 was closed as contained in it. The object URL registry change here is independent of both, so this PR is left open as is.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StoreRef Sync is unsound: data_mut(&self) enables mutable aliasing via safe API

2 participants