Public: anyone can read this; the crew can change it.
Full suite red: test/loop-stall.test.ts #12860
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/12844-full-suite-red-test-loop-stall-test-ts → main 2 commits fork 4 threads · 4 unresolved
src/kernel/slow_sql.ts +36 −1
| @@ -23,6 +23,9 @@ const PER_MINUTE = 10; | ||
| 23 | 23 | const TOP_CALLERS = 10; |
| 24 | 24 | const METHODS = ["run", "get", "all", "values"] as const; |
| 25 | 25 | |
| 26 | /** Transaction factories this module has already wrapped. */ | |
| 27 | const timedTransactions = new WeakSet<object>(); | |
| 28 | ||
| 26 | 29 | /** The SQL's first 100 characters on one line. */ |
| 27 | 30 | export function sqlHead(sql: unknown): string { |
| 28 | 31 | return String(sql).replace(/\s+/g, " ").trim().slice(0, 100); |
| @@ -125,4 +128,36 @@ export function timeStatements( | ||
| 125 | 128 | const exec = handle[name]!.bind(db); |
| 126 | 129 | handle[name] = (sql: unknown, ...rest: unknown[]) => timed(sql, exec)(sql, ...rest); |
| 127 | 130 | } |
| 128 | } | |
| 131 | // Newer bun begins and commits a transaction natively, past the wrapped | |
| 132 | // `run`: the lock wait of BEGIN IMMEDIATE would go unseen. Timing the native | |
| 133 | // call as a whole would label the body's time as BEGIN IMMEDIATE, so an | |
| 134 | // outermost transaction is begun and ended through the timed `run` instead; | |
| 135 | // a nested one (savepoints) keeps the native path. | |
| 136 | if (typeof handle.transaction === "function" && !timedTransactions.has(handle.transaction)) { | |
| 137 | const makeTx = handle.transaction.bind(db) as (fn: (...a: unknown[]) => unknown) => Record<string, unknown>; | |
| 138 | const modes = { deferred: "BEGIN DEFERRED", immediate: "BEGIN IMMEDIATE", exclusive: "BEGIN EXCLUSIVE" } as const; | |
| 139 | const open = (): boolean => (db as unknown as { inTransaction: boolean }).inTransaction; | |
| 140 | const transaction = (fn: (...a: unknown[]) => unknown) => { | |
| 141 | const native = makeTx(fn); | |
| 142 | const through = (mode: keyof typeof modes | "default") => { | |
| 143 | const nativeFn = (mode === "default" ? native : native[mode]) as (...a: unknown[]) => unknown; | |
| 144 | return function (this: unknown, ...args: unknown[]): unknown { | |
| 145 | if (open()) return nativeFn.apply(this, args); | |
| 146 | handle.run!(mode === "default" ? "BEGIN" : modes[mode]); | |
| 147 | try { | |
| 148 | const out = fn.apply(this, args); | |
| 149 | handle.run!("COMMIT"); | |
| 150 | return out; | |
| 151 | } catch (e) { | |
| 152 | if (open()) handle.run!("ROLLBACK"); | |
| 153 | throw e; | |
| 154 | } | |
| 155 | }; | |
| 156 | }; | |
| 157 | const tx = through("default") as unknown as Record<string, unknown>; | |
| 158 | for (const m of Object.keys(modes) as (keyof typeof modes)[]) tx[m] = through(m); | |
| 159 | return tx; | |
| 160 | }; | |
| 161 | timedTransactions.add(transaction); | |
| 162 | handle.transaction = transaction as unknown as (...args: unknown[]) => unknown; | |
Non-blocking: |
||
| 163 | }} |
test/loop-stall.test.ts +28
| @@ -72,6 +72,34 @@ describe("slow statements", () => { | ||
| 72 | 72 | db.close(); |
| 73 | 73 | }); |
| 74 | 74 | |
| 75 | test("a transaction's BEGIN and COMMIT are timed; a nested one adds none; rollback leaves nothing", () => { | |
| 76 | const db = new Database(":memory:"); | |
| 77 | const { lines, log } = recorder(); | |
| 78 | let step = 0; | |
| 79 | let t = 0; | |
| 80 | timeStatements(db, log, 200, () => (t += step)); | |
| 81 | db.run("CREATE TABLE t (x INTEGER)"); | |
| 82 | const put = db.transaction((x: number) => db.query("INSERT INTO t VALUES (?1)").run(x)); | |
| 83 | step = 250; | |
| 84 | put.immediate(1); | |
| 85 | const sqls = () => lines.map((l) => l.fields?.sql); | |
| 86 | expect(sqls()).toContain("BEGIN IMMEDIATE"); | |
| 87 | expect(sqls()).toContain("COMMIT"); | |
| 88 | lines.length = 0; | |
| 89 | const outer = db.transaction(() => { | |
| 90 | put(3); | |
outdated
This test passes on the old code too: native transactions already commit, roll back and nest correctly, so it would not fail without the change. The behaviour added is that BEGIN IMMEDIATE and COMMIT are now seen by the timer. Fix: use recorder() and a clock that advances past the 200 ms threshold, run put.immediate(1), and assert the log has a slow statement whose sql starts with BEGIN IMMEDIATE. Also assert that a nested put(3) inside outer does not issue a second BEGIN. |
||
| 91 | throw new Error("boom"); | |
| 92 | }); | |
| 93 | expect(() => outer.exclusive()).toThrow("boom"); | |
| 94 | expect(sqls().filter((s) => String(s).startsWith("BEGIN"))).toEqual(["BEGIN EXCLUSIVE"]); | |
| 95 | expect(sqls()).toContain("ROLLBACK"); | |
| 96 | expect(db.inTransaction).toBe(false); | |
| 97 | step = 0; | |
| 98 | expect(db.query("SELECT x FROM t").all()).toEqual([{ x: 1 }]); | |
| 99 | timeStatements(db, log, 200, () => 0); // idempotent for transactions | |
Non-blocking: this only shows a second call does not throw. Run one more transaction after it and assert exactly one BEGIN IMMEDIATE is logged. |
||
| 100 | db.close(); | |
| 101 | }); | |
| 102 | ||
| 75 | 103 | test("every statement is counted under its caller, and the top ten are one line a minute", () => { |
| 76 | 104 | const db = new Database(":memory:"); |
| 77 | 105 | const { lines, log } = recorder(); |
handle.transaction! is dereferenced unguarded. If a bun version lacks it, or it is already wrapped, this throws at boot in main.ts and worker.ts. Guard it, and make timeStatements idempotent for transaction the way wrap() is for statements. Also,
thisis lost when the user function is called as body(...args); bun passes the db context, so keep fn.apply(this, args).