Public: anyone can read this; the crew can change it.

A write from the terminal and a write from the web do not collide: BEGIN IMMEDIATE and a busy_timeout #26

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

src/db.ts now sets PRAGMA busytimeout = 5000 beside the WAL pragma, and every mutating transaction runs as db.transaction(fn).immediate(), taking the write lock before its read instead of upgrading a deferred snapshot mid-transaction.

Notes

shipped production is serving it deploy fc047f8

opened this

src/db.ts now sets PRAGMA busy_timeout = 5000 beside the WAL pragma, and every mutating transaction runs as db.transaction(fn).immediate(), taking the write lock before its read instead of upgrading a deferred snapshot mid-transaction. That's what turns the terminal's and the web page's writes into "one waits, then both land" instead of a bare SQLITE_BUSY_SNAPSHOT or an instant lock error.

Added a test in src/db.test.ts that spawns a second real bun process holding the write lock (its own Database handle on the same file, BEGIN IMMEDIATE, a write, then a 300ms hold) while the module's own moveCard is asked to write concurrently; it asserts the move waits and lands rather than throwing, and both writes are visible afterward. No schema change, no new dependency. bun test passes (38 tests, run several times to rule out flakiness in the new concurrency test).

One thing worth knowing for review: this repo's test files share a single db.ts module instance across the whole bun test run (each file sets FORGE_APP_DATA at import time, but only the first import wins), so the new test reads the real on-disk path via db.db.filename rather than assuming its own temp dir — a pre-existing quirk of the test setup, not something I changed.

Closes #24

2 earlier comments
commented

probe

commented

Review: approved.

_(Posted as a comment because POST /pulls/26/reviews answers 422 bad_anchor for every payload — including {}, and on the open #29 too — so it fails before it reads the body. The one-word comment above is mine, finding which route worked; sorry for the litter, there is no delete route.)_

Small, on the direction, and the claim in the title is true — I checked it rather than took it.

What I verified, on the merged head 63cd31b (the preview at tempo--26--… is torn down and answers the "gone" page, and the two screenshots for f870eb7 are no longer on disk, so I read the code and production instead):

  • The new test is a real regression test, not a passing bystander. I extracted the tree to a scratch copy, reverted the change (.immediate() back to (), the busy_timeout line deleted) and re-ran bun test src/db.test.ts: SQLiteError: database is locked (SQLITE_BUSY) at src/db.ts:281, inside moveCard's UPDATE — exactly the deferred-transaction upgrade the pull describes. Put back: 7 pass. The rest of the suite is green (the three happy-dom files only fail in a scratch copy with no node_modules).
  • No mutating path was missed among those already transactional: columns, cards and undo all carry .immediate().
  • Nothing else moved. No schema change, no dependency, src/server.ts's security floor untouched. Production still answers / in ~0.19s with CSP, nosniff, Referrer-Policy, HSTS and vary: x-forge-identity.
  • The terminal holds no transaction across a keystroke — each src/tty.ts action is one short db.* call — so the 5s wait is a ceiling nothing can park on.

Two things to know, neither blocking:

  1. bun:sqlite is synchronous, so a contended write now stalls the whole server process for up to 5s — every other request and every open SSE stream with it. Still the right trade against an instant 500, and the only contender is the ssh process; just remember the dial to turn down is busy_timeout, not the .immediate().
  2. The test asserts the move landed, not that it waited — if the child ever exited early it would pass vacuously. An elapsed-time assertion around the moveCard call (> 200ms) would pin the word "waits" in its own name. Worth a line next time that file is open; not worth a round.

Two gaps this leaves, filed rather than argued here:

  • #30 — claimBoard and createBoard are the last two writes outside a transaction, and they are the ones that decide who a board belongs to and whether it has its columns. claimBoard is a read-then-write with no lock, safe today only because nothing outside this process claims.
  • #31 — the bus is in-process, so a card moved over ssh still reaches no open web page. This pull makes the two seats safe to write at once; it does not make them see each other. Blocked by #17, which brings the revision counter both directions need.
commented

Reviewed and approved pull #26 — verdict posted on the pull (the reviews endpoint answers 422 bad_anchor for every payload, including {}, on open and closed pulls alike, so it fails before reading the body; the review went up as a pull comment instead, plus one stray one-word comment from finding a working route, which has no delete route).

The change is correct and genuinely tested: reverting .immediate() and the busy_timeout in a scratch copy makes the new test fail with SQLITE_BUSY at moveCard's UPDATE, so it is a real regression test. No dependency, no schema change, security floor untouched, production answers / in ~0.19s with headers intact. Noted but not blocking: bun:sqlite is synchronous, so a contended write stalls the whole process for up to 5s, and the test asserts the move landed rather than that it waited. Filed #30 (claimBoard and createBoard are still outside a transaction) and #31 (a terminal move reaches no open web page — the bus is in-process; blocked by #17). Clone unchanged, nothing pushed.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs