Skip to content

[Bug]: wa-sqlite changeset_apply throws "function signature mismatch" during rebase rollback #998

@slashv

Description

@slashv

Bug Description

I've been encountering this bug often in my LiveStore apps. It seems to be most prevalent when syncing ClientDocuments with Schema.Record between tabs due to the high probability of conflicts but from my understanding wouldn't be limited to ClientDocument:

Rebase rollback applies inverted changesets and sqlite3.changeset_apply crashes with RuntimeError: function signature mismatch (Failed calling makeChangeset.apply). The failure occurs during conflict handling in wa‑sqlite because a JS callback is passed to WASM instead of a function pointer, so any rollback that invokes changeset_apply can fail under concurrent sync.

See more info and two possible solutions in:
https://github.com/slashv/livestore/blob/repro/changeset-apply-bug/repro/CHANGESET_APPLY_BUG.md

Minimal Reproduction

The issue is timing dependent. Currently implemented as repro app with playwright test and triggers only when running in --headed mode. The tests only fail if the exact changeset error is raised.

https://github.com/slashv/livestore/tree/repro/changeset-apply-bug

cd repro
pnpm playwright test --headed

LiveStore Version

0.4.0-dev.22

Which package(s) are affected?

@livestore/wa-sqlite

Environment

- OS: MacOs
- Node.js version: 
- Package manager: pnpm
- Runtime: (browser/Node.js/Cloudflare Workers/other)

Expected Behavior

The conflict resolution should resolve correctly and LiveStore should remain alive.

Actual Behavior

Error is raised and LiveStore stops:
[@livestore/adapter-web:client-session] client-session shutdown

Error Logs/Screenshots

Error message:
LiveStore.SqliteError: { "query": undefined, "code": -1, "cause": RuntimeError: function signature mismatch, "note": "Failed calling makeChangeset.apply" }
Cause:
RuntimeError: function signature mismatch
    at wa-sqlite.wasm:wasm-function[517]:0x30368
    at wa-sqlite.wasm:wasm-function[830]:0x4550b
    at wa-sqlite.wasm:wasm-function[1217]:0x65d91
    at wa-sqlite.wasm:wasm-function[1649]:0x7b6b1
    at Object.changeset_apply (wa-sqlite/src/sqlite-api.js:1176:22)
    at Object.apply (sqlite-wasm/src/make-sqlite-db.ts:189:21)
    at SqliteDbWrapper.rollback (livestore/src/SqliteDbWrapper.ts:94:23)
    at rollback (livestore/src/store/store.ts:188:52)
    at ClientSessionSyncProcessor.ts:183:17
    at client-session-sync-processor:pull (ClientSessionSyncProcessor.ts:235:14)

Then LiveStore shuts down:
[@livestore/adapter-web:client-session] client-session shutdown

Additional Context

Happy to try resolving this bug but would need some guidance in best path forward as it touches critical code-paths and potentially C code which I'm less familiar with.

Checklist

  • I have searched existing issues to ensure this bug hasn't been reported before
  • I have provided a minimal reproduction that demonstrates the issue
  • I have included all the requested information above

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions