Resolve #485: demonstrate + regression-test Send/Sync for R2 Bucket in axum handlers - #1055
Open
NAVEENKUMARKR777 wants to merge 1 commit into
Open
Resolve #485: demonstrate + regression-test Send/Sync for R2 Bucket in axum handlers#1055NAVEENKUMARKR777 wants to merge 1 commit into
NAVEENKUMARKR777 wants to merge 1 commit into
Conversation
Closes the long-standing repro in cloudflare#485: `Bucket` (and the other R2 handle types) are already `Send + Sync` today because `JsValue` itself is unconditionally `Send + Sync` on non-atomics wasm32 builds, so it's safe to hold a `Bucket` in axum router state. The remaining piece of the original error was that `Bucket::list().execute()` awaits a `JsFuture` internally, which makes the handler's generated future `!Send` unless the handler body is wrapped with `#[worker::send]`. Adds a compile-time Send/Sync check for the R2 handle types to guard against regressions, and extends the axum example with a working `list_bucket` handler reproducing and resolving the original issue. Co-Authored-By: Claude Sonnet 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.
Summary
Closes the reproduction described in #485, where using
Bucket(an R2 binding) inside anaxumhandler fails to compile with:Investigating on current
main, I found this is actually resolvable today, but the fix was never demonstrated or tested end-to-end:Bucket(and the other R2 handle types:Object,Objects,MultipartUpload,UploadedPart) are alreadySend + Sync— no manualunsafe implneeded, becausewasm_bindgen::JsValueitself is unconditionallySend + Syncoutside of atomics builds, and these types are thinJsValuewrappers. So storing aBucketinaxumrouter state (AppState) already works fine.Bucket::list().execute()(and similarly other R2 methods) internally.awaits aJsFuture, whoseRc<RefCell<Inner>>state isn'tSend. That makes the handler's generated future!Send, whichaxum'sHandlertrait rejects.#[worker::send]macro (added after this issue was filed) fixes exactly this: it wraps the handler body inSendFuture, which is unconditionally (and safely, since Workers are single-threaded) markedSend, erasing the bound regardless of what's captured inside.I verified this by reproducing the exact repro from the issue (axum
State<AppState>extractor +#[debug_handler]+Bucket::list().execute().await): it fails with the same error without#[worker::send], and compiles cleanly with it added.Changes
examples/axum/src/lib.rs: adds aBucketfield toAppStateand alist_buckethandler demonstrating the working#[worker::send]pattern, with a doc comment explaining why it's needed and linking back to [BUG]Rc<RefCell<wasm_bindgen_futures::Inner>>cannot be sent between threads safely #485 for future readers who hit the same wall.worker/src/r2/mod.rs: adds a compile-timeSend/Syncregression test (mirroring the existing pattern inworker/src/email.rs) forBucket,Object,Objects,MultipartUpload, andUploadedPart, so a future change (e.g. toworker-sysor awasm-bindgenupgrade) that accidentally breaks this doesn't go unnoticed.Test plan
cargo check --target wasm32-unknown-unknown -p worker --testspasses, exercising the newsend_checkmodule.#[worker::send](confirms root cause).cargo check -p axum-on-workers --target wasm32-unknown-unknowncompiles cleanly with#[worker::send]added (confirms the fix).cargo fmt/cargo clippy -p axum-on-workers --no-depsclean on the touched code (pre-existing warnings elsewhere in the example are unrelated).🤖 Generated with Claude Code