import type { Client, Transaction, TransactionMode } from "@libsql/client"; /** * SQLite lets one writer in at a time, and does not queue a second: it is refused at once ("database is locked"). The client keeps one connection for ordinary * statements and hands another to each open transaction, and a transaction here awaits between its statements, so a write from anywhere else — the diagnostic log * trimming itself, an agent's report — that lands in one of those pauses is refused. Worse, a refused statement is left half-finished on its connection, and the * next transaction on that connection then cannot commit ("SQL statements in progress") until the leftover is garbage collected. Trying the write again does not * help with that, so the writes are kept from being refused instead: while a transaction of this process is open, an ordinary write (and the next transaction) * waits for it to end. * * Reads are never held — SQLite lets them through while a writer is working. A wait that goes on too long (a transaction that was never ended) is given up, and * the statement goes ahead and is refused as it would have been, rather than the whole app standing still. */ export const WRITE_WAIT_LIMIT_MS = 10_000; const isRead = (sql: string) => /^\s*(select|pragma|explain)\b/i.test(sql); export function serialiseWrites(client: Client, waitLimitMs = WRITE_WAIT_LIMIT_MS): Client { /** Settles when the transaction that is open now ends; null when none is. */ let open: Promise | null = null; const plain = { execute: client.execute.bind(client), batch: client.batch.bind(client), executeMultiple: client.executeMultiple.bind(client), transaction: client.transaction.bind(client), }; /** * Runs `run` once no transaction is open. The last look at `open` and the call to `run` are in the same turn of the event loop (no `await` between them), so * nothing can open a transaction in between. */ async function whenQuiet(run: () => Promise): Promise { const giveUpAt = Date.now() + waitLimitMs; while (open !== null && Date.now() < giveUpAt) { let timer: ReturnType | undefined; const patience = new Promise((resolve) => { timer = setTimeout(resolve, Math.max(0, giveUpAt - Date.now())); }); await Promise.race([open, patience]); clearTimeout(timer); } return run(); } const writing = (call: (...args: A) => Promise) => (...args: A) => whenQuiet(() => call(...args)); client.execute = ((...args: Parameters) => { const [stmt] = args; const sql = typeof stmt === "string" ? stmt : stmt.sql; return isRead(sql) ? plain.execute(...args) : whenQuiet(() => plain.execute(...args)); }) as typeof client.execute; client.batch = writing(plain.batch) as typeof client.batch; client.executeMultiple = writing(plain.executeMultiple) as typeof client.executeMultiple; client.transaction = ((mode?: TransactionMode) => whenQuiet(async () => { // If the wait was given up with another transaction still open, this one is not the one the others wait for. const gate: { release: () => void } = { release: () => {} }; const mine = open === null ? new Promise((resolve) => (gate.release = resolve)) : null; if (mine) open = mine; const done = () => { if (mine && open === mine) open = null; gate.release(); }; let tx: Transaction; try { tx = await plain.transaction(mode); } catch (err) { done(); throw err; } const wrapped: Transaction = { execute: (stmt) => tx.execute(stmt), batch: (stmts) => tx.batch(stmts), executeMultiple: (sql) => tx.executeMultiple(sql), // A commit that fails leaves the transaction open (the caller rolls it back), so only a commit that went through ends the wait. commit: async () => { await tx.commit(); done(); }, rollback: async () => { try { await tx.rollback(); } finally { done(); } }, close: () => { tx.close(); done(); }, get closed() { return tx.closed; }, }; return wrapped; })) as typeof client.transaction; return client; }