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

Full suite red: test/loop-stall.test.ts #12860

Merged opened this in answer to

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

Merged

scout/forge:scout/12844-full-suite-red-test-loop-stall-test-ts → main 2 commits fork 4 threads · 4 unresolved

  • Merged. scout/12844-full-suite-red-test-loop-stall-test-ts was merged into main as 576c348.

Review

Sign in to review this change.
2 files changed +64 −1
src/kernel/slow_sql.ts +36 −1
@@ -23,6 +23,9 @@ const PER_MINUTE = 10;
2323 const TOP_CALLERS = 10;
2424 const METHODS = ["run", "get", "all", "values"] as const;
2525
26 /** Transaction factories this module has already wrapped. */
27 const timedTransactions = new WeakSet<object>();
28
2629 /** The SQL's first 100 characters on one line. */
2730 export function sqlHead(sql: unknown): string {
2831 return String(sql).replace(/\s+/g, " ").trim().slice(0, 100);
@@ -125,4 +128,36 @@ export function timeStatements(
125128 const exec = handle[name]!.bind(db);
126129 handle[name] = (sql: unknown, ...rest: unknown[]) => timed(sql, exec)(sql, ...rest);
127130 }
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
outdated

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, this is lost when the user function is called as body(...args); bun passes the db context, so keep fn.apply(this, args).

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: }} closes the if and the function on one line. Split them.

163 }}
test/loop-stall.test.ts +28
@@ -72,6 +72,34 @@ describe("slow statements", () => {
7272 db.close();
7373 });
7474
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
75103 test("every statement is counted under its caller, and the top ten are one line a minute", () => {
76104 const db = new Database(":memory:");
77105 const { lines, log } = recorder();

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs