Skip to content

fix(wasm-adapter): gate unsafe Send/Sync buffer impls to wasm32 - #496

Closed
lxsaah wants to merge 1 commit into
mainfrom
claude/gracious-cannon-r89w1r
Closed

lxsaah wants to merge 1 commit into
mainfrom
claude/gracious-cannon-r89w1r

Conversation

@lxsaah

@lxsaah lxsaah commented Nov 22, 2026

Copy link
Copy Markdown
Member

Description

WasmBuffer and WasmBufferReader declare unsafe impl Send/Sync on every target, on top of Rc<RefCell<…>>. That is only sound on single-threaded wasm32. On native targets, which is how the host unit-test lane builds the crate, a buffer can be moved or shared across threads, and that is undefined behaviour.

This PR is the soundness fix from #493 and nothing else:

  • The unsafe impl Send/Sync for both types now apply only on wasm32.
  • Native builds use Arc<Mutex<…>>. The traits require Send + Sync, and on native the compiler now provides them. A small lock_state helper hides the difference. A poisoned lock is recovered instead of panicking, since the state is always consistent.
  • wasm32 behaviour is unchanged. It still uses Rc<RefCell<…>>. The only difference is that subscribe and peek now borrow mutably, which is harmless on a single thread.
  • poll_recv reads again after registering its waker. Otherwise, on native, a push from another thread that lands between the read and the registration could be missed. On wasm32 the extra read only runs when the buffer is empty, and it costs very little.
  • New native test. test_cross_thread_push_wakes_reader pushes 500 values from another thread while a reader waits with block_on.

Out of scope, compared with #493: the file moves, removing the guard that stops a native build with wasm-runtime on, and the new no-op time fallback.

Related Issue

Checklist

  • I have read the CONTRIBUTING.md document.
  • My code follows the project's coding standards.
  • I have added tests to cover my changes.
  • All new and existing tests passed (make check). Not run in full. These ran locally:
    • cargo test -p aimdb-wasm-adapter --no-default-features --lib, with and without observability: pass
    • cargo clippy -D warnings for the lib, native and wasm32-unknown-unknown (with wasm-runtime and observability): clean
    • cargo fmt --check: clean
    • The wasm-pack browser tests were not run locally (wasm-pack isn't installed here), so CI covers them.
  • I have updated the documentation accordingly (module docs and SAFETY comments).

Generated by Claude Code

`WasmBuffer` and `WasmBufferReader` declared `unsafe impl Send/Sync` on
every target, over `Rc<RefCell<…>>`. That is sound only on single-threaded
wasm32; native builds (the host unit-test lane) could move or share a
buffer across threads, which is undefined behaviour.

Gate the impls to wasm32 and back the buffer with `Arc<Mutex<…>>` off
wasm32 so native gets `Send + Sync` from the compiler. wasm32 keeps
`Rc<RefCell<…>>` and is unchanged apart from going through the same
`lock_state` helper. `poll_recv` re-checks after registering its waker so
a push from another thread can't be missed between read and registration.

Adds a native test that pushes from another thread while a reader waits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WGkDJA9Zpan1vsursyFWHd

lxsaah commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Closing without merging. The wasm adapter only runs in the browser. The native build exists only for the host unit-test lane (--no-default-features), and those tests run on one thread with block_on. In that build the unsafe impl Send/Sync over Rc<RefCell> is technically unsound, but nothing triggers it in practice. Adding a mutex path that production never runs isn't worth the extra code.

If we ever want the native lane to be strictly sound, the cleaner route is to make the crate wasm32-only and move these tests to wasm-bindgen-test.


Generated by Claude Code

@lxsaah lxsaah closed this Nov 22, 2026
@lxsaah
lxsaah deleted the claude/gracious-cannon-r89w1r branch November 22, 2026 09:44
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.

2 participants