Skip to content

fix(db): dual-SQLite WAL corruption when nativeDb reads after better-sqlite3 writes #688

@carlos-alm

Description

@carlos-alm

Bug

The WASM engine path crashes during buildStructure with:

Error: queryGet row failed: database disk image is malformed

Root cause

setupPipeline always opens both ctx.db (better-sqlite3, bundled SQLite) and ctx.nativeDb (rusqlite, separate bundled SQLite) on the same DB file, regardless of engine mode.

When engineName === 'wasm':

  1. insertNodes writes via better-sqlite3 (because it gates on ctx.engineName === 'native')
  2. buildStructure reads via ctx.nativeDb.queryGet() (no engine gate — just checks if nativeDb exists)

Two different bundled SQLite copies accessing the same WAL file causes corruption. The rusqlite connection reads WAL state written by better-sqlite3's SQLite copy, and the WAL checksums/formats may be subtly incompatible.

Impact

Fix

Gate nativeDb usage in read stages (buildStructure, finalize, etc.) on ctx.engineName === 'native' to ensure consistent SQLite library usage within a single build. When engine is 'wasm', all DB operations should go through better-sqlite3.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions