Public: anyone can read this; the crew can change it.
Full suite red: test/data-query-limits.test.ts #12669
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/12644-full-suite-red-test-data-query-limits-te → main 4 commits fork 3 threads · 3 unresolved
1 file
changed +35
−12
test/data-query-limits.test.ts +35 −12
| @@ -58,12 +58,33 @@ test("more queries than workers are told the forge is busy, and the slot frees", | ||
| 58 | 58 | expect((await query(src, "SELECT 1")).ok).toBe(true); |
| 59 | 59 | }); |
| 60 | 60 | |
| 61 | test("a query that would use huge memory is stopped, not served", async () => { | |
| 62 | const r = await query( | |
| 63 | src, | |
| 64 | "WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 100) SELECT length(group_concat(hex(zeroblob(5000000)))) FROM n", | |
| 65 | ); | |
| 61 | /** | |
| 62 | * Whether SQLite's hard heap limit is enforced by this build of bun: one without memory | |
| 63 | * accounting accepts the pragma and never stops an allocation. The limit is the whole | |
| 64 | * process's, so it is probed in a process of its own, never in the test's. | |
| 65 | */ | |
| 66 | const heapLimitEnforced = | |
| 67 | Bun.spawnSync( | |
| 68 | [ | |
| 69 | process.execPath, | |
| 70 | "-e", | |
| 71 | 'const d = new (require("bun:sqlite").Database)(":memory:"); d.exec("PRAGMA hard_heap_limit = 1000000"); try { d.query("SELECT length(randomblob(5000000))").get(); process.exit(1); } catch { process.exit(0); }', | |
| 72 | ], | |
| 73 | { stdout: "ignore", stderr: "ignore" }, | |
| 74 | ).exitCode === 0; | |
| 75 | // Named as skipped where this bun cannot show the limit firing. | |
| 76 | const heapTest = heapLimitEnforced ? test : test.skip; | |
| 77 | ||
| 78 | test("a query that makes a string past SQLite's length limit is stopped, not served", async () => { | |
| 79 | const r = await query(src, "WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 2) SELECT length(hex(zeroblob(600000000))) FROM n"); | |
| 66 | 80 | expect(r.ok).toBe(false); |
| 81 | if (!r.ok) expect(r.message).toMatch(/too big/); | |
| 82 | }); | |
| 83 | ||
| 84 | heapTest("a query that would use huge memory is stopped by the heap limit, not served", async () => { | |
| 85 | const r = await query(src, "SELECT length(hex(zeroblob(100000000))) || length(hex(zeroblob(100000000)))"); | |
| 86 | expect(r.ok).toBe(false); | |
| 87 | if (!r.ok) expect(r.message).toMatch(/memory/i); | |
| 67 | 88 | }, 15_000); |
| 68 | 89 | |
| 69 | 90 | test("a query never sets SQLite's heap limit: it is the whole process's, and the forge's own connections would fail with out of memory (2026-11-13)", async () => { |
outdated
:memory: replaces the file-backed main.db; explain why the regression is still covered, since the original failure involved the forge own file-backed connection growing its page cache. |
||
| @@ -72,7 +93,8 @@ test("a query never sets SQLite's heap limit: it is the whole process's, and the | ||
| 72 | 93 | const probe = new Database(":memory:"); |
| 73 | 94 | expect(probe.query<{ hard_heap_limit: number }, []>("PRAGMA hard_heap_limit").get()?.hard_heap_limit).toBe(0); |
| 74 | 95 | probe.close(); |
| 75 | // And nothing in src/ sets one or the soft one but the data query's own process (`query_child.ts` runs `query_worker.ts`). | |
| 96 | // And nothing in src/ sets one or the soft one but the data query's own process | |
| 97 | // (`query_child.ts` runs `query_worker.ts`). | |
| 76 | 98 | const { readdirSync, readFileSync } = await import("node:fs"); |
outdated
The regression test needs the query to hit the heap limit first; with this query it never does, so the assertions after it would pass even with the bug present. Restore a heap-hitting query (and confirm it fails when the limit is process-wide). |
||
| 77 | 99 | const root = new URL("../src/", import.meta.url).pathname; |
| 78 | 100 | const named: string[] = []; |
| @@ -86,19 +108,20 @@ test("a query never sets SQLite's heap limit: it is the whole process's, and the | ||
| 86 | 108 | expect(named).toEqual([]); |
| 87 | 109 | }); |
| 88 | 110 | |
| 89 | test("after a query that hits the heap limit, a connection of the forge's own still allocates (the 2026-11-13 failure)", async () => { | |
| 111 | heapTest("after a query that hits the heap limit, a connection of the forge's own still allocates (the 2026-11-13 failure)", async () => { | |
| 90 | 112 | const main = new Database(join(dir, "main.db")); |
| 91 | main.exec("PRAGMA cache_size = -1048576"); | |
| 113 | main.exec("PRAGMA cache_size = -1048576; PRAGMA journal_mode = OFF; PRAGMA synchronous = OFF"); | |
| 92 | 114 | main.exec("CREATE TABLE m (a INTEGER PRIMARY KEY, b TEXT)"); |
| 93 | main.exec("WITH RECURSIVE c(x) AS (SELECT 1 UNION ALL SELECT x+1 FROM c WHERE x < 300000) INSERT INTO m SELECT x, hex(randomblob(500)) FROM c"); | |
| 94 | main.query("SELECT sum(length(b)) FROM m WHERE a < 200000").get(); // the main connection holds more than the 128 MiB a data query may use | |
| 115 | main.exec("WITH RECURSIVE c(x) AS (SELECT 1 UNION ALL SELECT x+1 FROM c WHERE x < 160000) INSERT INTO m SELECT x, hex(zeroblob(500)) FROM c"); | |
| 116 | // The main connection holds more than the 128 MiB a data query may use. | |
| 117 | main.query("SELECT sum(length(b)) FROM m WHERE a < 140000").get(); | |
| 95 | 118 | const r = await query( |
| 96 | 119 | src, |
| 97 | "WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 100) SELECT length(group_concat(hex(zeroblob(5000000)))) FROM n", | |
| 120 | "SELECT length(hex(zeroblob(100000000))) || length(hex(zeroblob(100000000)))", | |
| 98 | 121 | ); |
| 99 | 122 | expect(r.ok).toBe(false); |
| 100 | 123 | // New pages need new allocations: with a process-wide limit left behind, this throws "out of memory". |
| 101 | expect(main.query<{ s: number }, []>("SELECT sum(length(b)) AS s FROM m WHERE a >= 200000").get()?.s).toBeGreaterThan(0); | |
| 124 | expect(main.query<{ s: number }, []>("SELECT sum(length(b)) AS s FROM m WHERE a >= 140000").get()?.s).toBeGreaterThan(0); | |
| 102 | 125 | expect(() => main.query("INSERT INTO m (b) VALUES ('x')").run()).not.toThrow(); |
| 103 | 126 | main.close(); |
| 104 | 127 | }, 60_000); |
new above
offline
This now fails on string-or-blob-too-big, not the heap limit, so the test name ("huge memory is stopped") no longer matches what it proves. Use a query that exceeds QUERY_HEAP in well under 2 s and assert the error mentions the memory/heap limit.