Skip to content

[codex] Fix wasm pre-commit aliasing in barrier APIs#939

Draft
zxch3n wants to merge 1 commit intomainfrom
codex/fix-wasm-precommit-aliasing
Draft

[codex] Fix wasm pre-commit aliasing in barrier APIs#939
zxch3n wants to merge 1 commit intomainfrom
codex/fix-wasm-precommit-aliasing

Conversation

@zxch3n
Copy link
Copy Markdown
Member

@zxch3n zxch3n commented Mar 30, 2026

Summary

This fixes a loro-wasm re-entrancy issue on pre-commit/barrier paths that could surface as:

Error: recursive use of an object detected which would lead to unsafe aliasing in rust

importBatch was already present in the JS-side pending-event decoration allowlist. The actual problem was that several WASM-exposed LoroDoc methods still took &mut self, even though the underlying Rust APIs only require &self and can synchronously re-enter JS through subscribePreCommit.

What Changed

  • changed attach, checkoutToLatest, checkout, importUpdateBatch, and importBatch to take &self in the wasm binding layer
  • added regression coverage for importBatch pending-event flushing
  • added re-entrancy tests for importBatch, checkoutToLatest, and attach when a subscribePreCommit callback touches the same doc

Root Cause

The JS decoration list was not the missing piece here.

importBatch is already decorated on the JS side, so pending events are flushed correctly after the call returns. The unsafe aliasing error comes from a different path: subscribePreCommit invokes JS synchronously before the outer WASM call finishes. If that outer binding holds a mutable wasm-bindgen borrow, touching the same document again from the callback can trigger the recursive aliasing error.

Impact

This makes the affected loro-wasm APIs safe to re-enter from pre-commit callbacks and prevents false attribution of the bug to the pending-event decoration list.

Validation

  • pnpm --dir crates/loro-wasm test

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.

1 participant