Repository navigation
fix: keep ResponseBody's Send impl sound with wasm threads - #120
Open
QuantumExplorer wants to merge 1 commit into
Open
QuantumExplorer wants to merge 1 commit into
QuantumExplorer wants to merge 1 commit into
Conversation
BodyStream holds JS objects (the fetch body stream, the AbortController and a timeout closure), which belong to the thread that created them. Its unconditional unsafe impl Send is fine on single-threaded wasm, but with wasm threads (target_feature = "atomics") a ResponseBody could be moved to another worker, where polling or dropping it touches JS handles from the wrong thread. With atomics, BodyStream now records its thread, panics if it is polled on another one, and leaks the JS objects if it is dropped on another one. Without atomics nothing changes. Send stays, since tonic requires it; Sync is sound as is, because &BodyStream gives no access to the JS objects. Fixes devashishdxt#106 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Fixes #106.
Problem
BodyStreamholds JS objects (the fetch body stream, theAbortControllerand the timeout closure), which belong to the thread that created them. Itsunsafe impl Sendapplies to every target. That is fine on single-threaded wasm, but with wasm threads (target_feature = "atomics") aResponseBodycan be moved to another worker, where polling or dropping it touches JS handles that belong to another thread.Fix
With
target_feature = "atomics",BodyStreamrecords the thread that created it, panics if it is polled on another thread, and leaks its JS objects if it is dropped on another thread. Without atomics nothing changes: the new code is all behindcfg.The
SendandSyncimpls stay, with a comment on why they hold. tonic needsSend, andSyncwas already sound, because&BodyStreamgives no access to the JS objects. RemovingSendunder atomics would also be sound, but builds with wasm threads would then stop compiling, since tonic would rejectResponseBody.Before and after
Send, sound (one thread)Tests
There is no automated test, since exercising this needs a multi-threaded wasm app. I checked that the crate builds and passes clippy both ways: as usual, and with
RUSTFLAGS="-C target-feature=+atomics,+bulk-memory" cargo +nightly clippy --target wasm32-unknown-unknown -Z build-std=panic_abort,std.cargo fmt --check,cargo clippy --all-targets -- -D warnings,cargo clippy --target wasm32-unknown-unknown -- -D warningsandcargo testare clean, and the browser suite intest-suite/simplepasses in headless Chrome 153.This is one of 14 PRs from the review asked for in #97. They are independent, and all 14 also merge together cleanly; with all of them merged, both browser suites (
test-suite/simpleandtest-suite/gzip) pass.🤖 Generated with Claude Code