From c2ec65f8826fc829c3f9f87939cf3684cc410650 Mon Sep 17 00:00:00 2001 From: Carl Vitullo Date: Mon, 26 Jan 2026 10:54:21 -0500 Subject: [PATCH] Fix potential cause of db corruption When Connection A (rollback) and Connection B (WAL) both access the same database: 1. Connection B writes in WAL mode - appends to -wal file, updates -shm coordination 2. Connection A doesn't know about WAL - it's looking for a -journal file and using file locks designed for rollback mode 3. Connection A writes in rollback mode - modifies database pages directly, creates a rollback journal 4. Conflict occurs: - Connection A may overwrite pages that Connection B expects to be at a certain state - Connection B's WAL entries reference page states that Connection A has modified - The -shm shared memory state becomes inconsistent with actual file state - B-tree page references become invalid ("2nd reference to page" errors we saw) --- app/db.server.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/db.server.ts b/app/db.server.ts index daeb848f..9232d5f2 100644 --- a/app/db.server.ts +++ b/app/db.server.ts @@ -8,8 +8,13 @@ export { SqliteError } from "better-sqlite3"; console.log(`Connecting to database at ${databaseUrl}`); +const sqliteDb = new SQLite(databaseUrl); +// Enable WAL mode to match @effect/sql-sqlite-node's default. +// Both connections MUST use the same journal mode to prevent corruption. +sqliteDb.pragma("journal_mode = WAL"); + export const dialect = new SqliteDialect({ - database: new SQLite(databaseUrl), + database: sqliteDb, }); const db = new Kysely({