From f21a38812fe388184275ed399712cc5981acb3f3 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Tue, 6 Oct 2026 23:39:14 +0000 Subject: [PATCH 01/13] sql: reject a query that is cancelled before it runs The guards of Query#run() and Query#runAsync() returned early on the cancelled bit, so a query that was cancelled before its first run never reached its handler and never settled. The handlers already reject a cancelled query, and the transaction handler removes it from its scope set first. Squash of #41492. --- src/js/internal/sql/query.ts | 10 +-- test/js/sql/sqlite-sql.test.ts | 111 +++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 8 deletions(-) diff --git a/src/js/internal/sql/query.ts b/src/js/internal/sql/query.ts index b037f80a2308..331432cd8ed2 100644 --- a/src/js/internal/sql/query.ts +++ b/src/js/internal/sql/query.ts @@ -101,10 +101,7 @@ class Query> extends PublicPromise { #run() { const { [_handler]: handler, [_queryStatus]: status } = this; - if ( - status & - (SQLQueryStatus.executed | SQLQueryStatus.error | SQLQueryStatus.cancelled | SQLQueryStatus.invalidHandle) - ) { + if (status & (SQLQueryStatus.executed | SQLQueryStatus.error | SQLQueryStatus.invalidHandle)) { return; } @@ -131,10 +128,7 @@ class Query> extends PublicPromise { async #runAsync() { const { [_handler]: handler, [_queryStatus]: status } = this; - if ( - status & - (SQLQueryStatus.executed | SQLQueryStatus.error | SQLQueryStatus.cancelled | SQLQueryStatus.invalidHandle) - ) { + if (status & (SQLQueryStatus.executed | SQLQueryStatus.error | SQLQueryStatus.invalidHandle)) { return; } diff --git a/test/js/sql/sqlite-sql.test.ts b/test/js/sql/sqlite-sql.test.ts index ed0567c186fb..e97bf4f569e9 100644 --- a/test/js/sql/sqlite-sql.test.ts +++ b/test/js/sql/sqlite-sql.test.ts @@ -915,6 +915,117 @@ describe("Query Execution", () => { expect(result2).toHaveLength(1); expect(result2[0].id).toBe(2); }); + + // These tests `await` the cancelled query directly. A pending promise inside + // `expect().rejects` does not trip the test timeout, so a hang would stall the file. + function settle(promise: Promise) { + return promise.then( + () => "resolved", + (error: Error & { code?: string }) => ({ code: error.code, message: error.message }), + ); + } + + test("cancel() before the query runs rejects when awaited", async () => { + const query = sql`SELECT 1 AS x`; + query.cancel(); + expect(query.cancelled).toBe(true); + + expect(await settle(query)).toEqual({ code: "ERR_SQLITE_QUERY_CANCELLED", message: "Query cancelled" }); + // The connection stays usable. + expect(await sql`SELECT 2 AS x`).toEqual([{ x: 2 }]); + }); + + test("cancel() before the query runs settles a Promise.all batch", async () => { + const cancelled = sql`SELECT 1 AS x`; + cancelled.cancel(); + const live = sql`SELECT 2 AS x`; + + expect(await settle(Promise.all([cancelled, live]))).toEqual({ + code: "ERR_SQLITE_QUERY_CANCELLED", + message: "Query cancelled", + }); + expect(await live).toEqual([{ x: 2 }]); + }); + + test("cancel() before execute() rejects instead of running", async () => { + await sql`CREATE TABLE cancel_before_execute (id INTEGER)`; + const query = sql`INSERT INTO cancel_before_execute VALUES (1)`; + query.cancel(); + query.execute(); + + expect(await settle(query)).toEqual({ code: "ERR_SQLITE_QUERY_CANCELLED", message: "Query cancelled" }); + expect(await sql`SELECT count(*) AS n FROM cancel_before_execute`).toEqual([{ n: 0 }]); + }); + + // A query that never runs opens no connection, so the server does not need to exist. + test.each([ + ["postgres://u:p@127.0.0.1:9/db", "ERR_POSTGRES_QUERY_CANCELLED"], + ["mysql://u:p@127.0.0.1:9/db", "ERR_MYSQL_QUERY_CANCELLED"], + ])("cancel() before the query runs rejects for %s", async (url, code) => { + await using remote = new SQL(url); + const query = remote`SELECT 1 AS x`; + query.cancel(); + + expect(await settle(query)).toEqual({ code, message: "Query cancelled" }); + }); + + // Transaction queries use their own Query handler. Each test owns its database, + // because a transaction that hangs keeps its connection. + test("cancel() before the query runs rejects inside a transaction", async () => { + await using db = new SQL(":memory:"); + const result = await db.begin(async tx => { + const query = tx`SELECT 1 AS x`; + query.cancel(); + const outcome = await settle(query); + // The transaction stays usable. + return [outcome, await tx`SELECT 2 AS x`]; + }); + + expect(result).toEqual([{ code: "ERR_SQLITE_QUERY_CANCELLED", message: "Query cancelled" }, [{ x: 2 }]]); + }); + + test("a transaction that returns a query cancelled before it runs rolls back", async () => { + await using db = new SQL(":memory:"); + await db`CREATE TABLE cancel_in_transaction (id INTEGER)`; + const transaction = db.begin(tx => { + const cancelled = tx`SELECT 1 AS x`; + cancelled.cancel(); + return [tx`INSERT INTO cancel_in_transaction VALUES (1)`, cancelled]; + }); + + expect(await settle(transaction)).toEqual({ code: "ERR_SQLITE_QUERY_CANCELLED", message: "Query cancelled" }); + expect(await db`SELECT count(*) AS n FROM cancel_in_transaction`).toEqual([{ n: 0 }]); + }); + + test("a query cancelled before it runs leaves its transaction scope when it rejects", async () => { + await using db = new SQL(":memory:"); + await db`CREATE TABLE cancel_then_close (id INTEGER)`; + const transaction = db.begin(async tx => { + await tx`INSERT INTO cancel_then_close VALUES (1)`; + const query = tx`SELECT 1 AS x`; + query.cancel(); + await settle(query); + // close({ timeout }) waits on every query that is still in the scope. A rejected + // query that stays there ends that wait at once, and close() then skips the ROLLBACK. + await tx.close({ timeout: 1 }); + }); + + expect(await settle(transaction)).toEqual({ code: "ERR_SQLITE_CONNECTION_CLOSED", message: "Connection closed" }); + expect(await db`SELECT count(*) AS n FROM cancel_then_close`).toEqual([{ n: 0 }]); + }); + + test("a query that tx.close() cancels before it runs rejects when awaited", async () => { + await using db = new SQL(":memory:"); + let lazy: Promise | undefined; + await settle( + db.begin(async tx => { + lazy = tx`SELECT 1 AS x`; // created, not awaited: close() cancels it + await tx.close(); + }), + ); + + expect(await settle(lazy!)).toEqual({ code: "ERR_SQLITE_QUERY_CANCELLED", message: "Query cancelled" }); + }); }); // Bun's bundled SQLite allows 250000 parameters. A system libsqlite3 (macOS) can stop at 32766. From 8d03dd609eee39b4b9911c8be83b6b26b579069a Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Tue, 6 Oct 2026 23:39:14 +0000 Subject: [PATCH 02/13] sql: close the reserved connection when close({ timeout }) drains before the timeout The grace period of reserved.close({ timeout }) becomes waitForPendingWork(): it resolves when the pending work has settled or when the timer fires. close() awaits it and falls through to the close body of the no-timeout case. release() returns when the closed bit is set. Squash of #39617. --- src/js/bun/sql.ts | 43 +- .../sql-pool-transaction-isolation.test.ts | 536 ++++++++++++++---- test/js/sql/wire-frames.test.ts | 6 + 3 files changed, 457 insertions(+), 128 deletions(-) diff --git a/src/js/bun/sql.ts b/src/js/bun/sql.ts index 64bf08c13cb8..be519765d99a 100644 --- a/src/js/bun/sql.ts +++ b/src/js/bun/sql.ts @@ -63,6 +63,21 @@ function settleReservedTransaction( settle(value); } +function onPendingWorkSettled(timer: ReturnType, resolve: () => void) { + clearTimeout(timer); + resolve(); +} + +/// The grace period of close({ timeout }): resolves once `pending` has settled or after `timeout` seconds. +function waitForPendingWork(pending: PromiseLike[], timeout: number): Promise { + const { promise, resolve } = Promise.withResolvers(); + const timer = setTimeout(resolve, timeout * 1000); + timer.unref(); // dont block the event loop + // allSettled: one rejected query must not cut the grace period short for the rest + Promise.allSettled(pending).then(onPendingWorkSettled.bind(null, timer, resolve)); + return promise; +} + function adapterFromOptions(options: Bun.SQL.__internal.DefinedOptions): Adapter | ListenableAdapter { switch (options.adapter) { case "postgres": @@ -493,39 +508,23 @@ const SQL = function SQL( throw $ERR_INVALID_ARG_VALUE("options.timeout", timeout, "must be a non-negative integer less than 2^31"); } if (timeout > 0 && (reserveQueries.size > 0 || reservedTransaction.size > 0)) { - const { promise, resolve } = Promise.withResolvers(); - // race all queries vs timeout - const pending_queries = Array.from(reserveQueries); - const pending_transactions = Array.from(reservedTransaction); - const timer = setTimeout(() => { - state.connectionState |= ReservedConnectionState.closed; - for (const query of reserveQueries) { - (query as Query).cancel(); - } - state.connectionState |= ReservedConnectionState.closed; - pooledConnection.close(); - - resolve(); - }, timeout * 1000); - timer.unref(); // dont block the event loop - Promise.all([Promise.all(pending_queries), Promise.all(pending_transactions)]).finally(() => { - clearTimeout(timer); - resolve(); - }); - return promise; + await waitForPendingWork([...reserveQueries, ...reservedTransaction], timeout); + // release() or a disconnect may have ended the reservation while we waited + if (state.connectionState & ReservedConnectionState.closed) return; } } state.connectionState |= ReservedConnectionState.closed; for (const query of reserveQueries) { (query as Query).cancel(); } - + // the close handler attached above returns the pool slot pooledConnection.close(); return Promise.$resolve(undefined); }; reserved_sql.release = () => { - if (state.connectionState & ReservedConnectionState.released) { + // closed but not released: close() is closing the socket (TLS defers it), and the close handler returns the slot + if (state.connectionState & (ReservedConnectionState.released | ReservedConnectionState.closed)) { return Promise.$resolve(undefined); } // just release the connection back to the pool diff --git a/test/js/sql/sql-pool-transaction-isolation.test.ts b/test/js/sql/sql-pool-transaction-isolation.test.ts index 6e20d7a26293..15e57371c52f 100644 --- a/test/js/sql/sql-pool-transaction-isolation.test.ts +++ b/test/js/sql/sql-pool-transaction-isolation.test.ts @@ -5,90 +5,156 @@ // Wire bytes come from ./wire-frames.ts. import { SQL } from "bun"; import { describe, expect, test } from "bun:test"; -import { bunEnv, bunExe } from "harness"; +import { bunEnv, bunExe, tls as tlsCert } from "harness"; import type net from "node:net"; +import tls from "node:tls"; import { listeningServer, + MYSQL_CLIENT_SSL, + MYSQL_DEFAULT_CAPABILITIES, mysqlAckSessionSetup, + mysqlErrPacket, mysqlHandshakeV10, mysqlOkPacket, mysqlReadPackets, pgAuthenticationOk, pgCommandComplete, + pgErrorResponse, pgReadyForQuery, + pgSSLResponse, } from "./wire-frames"; type Received = { conn: number; sql: string }; -type MockServer = (received: Received[]) => Promise<{ port: number; server: net.Server }>; +// `onReceived` runs after each statement is recorded. `secure` makes the server accept the +// client's TLS request and speak the protocol over TLS from there on. +type MockOptions = { onReceived?: () => void; secure?: boolean }; +type MockServer = (received: Received[], options?: MockOptions) => Promise<{ port: number; server: net.Server }>; + +function secureSocket(rawSocket: net.Socket): net.Socket { + rawSocket.pause(); + const socket = new tls.TLSSocket(rawSocket, { isServer: true, ...tlsCert }); + socket.on("error", () => {}); + return socket; +} -// Query text containing "KILL" destroys the socket without answering. -const pgMockServer: MockServer = received => { +// Both mocks answer every statement at once, except for these markers in the query text: +// KILL destroys the socket without answering, +// FAIL answers with an error, +// HOLD never answers. +const pgMockServer: MockServer = (received, { onReceived, secure } = {}) => { let nextConn = 0; - return listeningServer(socket => { + return listeningServer(rawSocket => { const connId = nextConn++; - let buffered = Buffer.alloc(0); - let startup = true; - socket.on("data", (chunk: Buffer) => { - buffered = Buffer.concat([buffered, chunk]); - if (startup) { - if (buffered.length < 4) return; - const len = buffered.readInt32BE(0); - if (buffered.length < len) return; - buffered = buffered.subarray(len); - startup = false; - socket.write(Buffer.concat([pgAuthenticationOk(), pgReadyForQuery()])); - } - while (buffered.length >= 5) { - const type = String.fromCharCode(buffered[0]); - const len = buffered.readInt32BE(1); - if (buffered.length < 1 + len) return; - const body = buffered.subarray(5, 1 + len); - buffered = buffered.subarray(1 + len); - if (type !== "Q") continue; - const sql = body.subarray(0, body.indexOf(0)).toString("utf8"); - received.push({ conn: connId, sql }); - if (sql.includes("KILL")) { - socket.destroy(); - return; + rawSocket.on("error", () => {}); + if (secure) { + // SSLRequest. The client sends nothing more until it has the answer. + rawSocket.once("data", () => { + rawSocket.write(pgSSLResponse("S")); + serve(secureSocket(rawSocket)); + }); + } else { + serve(rawSocket); + } + + function serve(socket: net.Socket) { + let buffered = Buffer.alloc(0); + let startup = true; + socket.on("data", (chunk: Buffer) => { + buffered = Buffer.concat([buffered, chunk]); + if (startup) { + if (buffered.length < 4) return; + const len = buffered.readInt32BE(0); + if (buffered.length < len) return; + buffered = buffered.subarray(len); + startup = false; + socket.write(Buffer.concat([pgAuthenticationOk(), pgReadyForQuery()])); } - socket.write(Buffer.concat([pgCommandComplete("SELECT 0"), pgReadyForQuery()])); - } - }); - socket.on("error", () => {}); + while (buffered.length >= 5) { + const type = String.fromCharCode(buffered[0]); + const len = buffered.readInt32BE(1); + if (buffered.length < 1 + len) return; + const body = buffered.subarray(5, 1 + len); + buffered = buffered.subarray(1 + len); + if (type !== "Q") continue; + const sql = body.subarray(0, body.indexOf(0)).toString("utf8"); + received.push({ conn: connId, sql }); + onReceived?.(); + if (sql.includes("KILL")) { + socket.destroy(); + return; + } + if (sql.includes("HOLD")) continue; + if (sql.includes("FAIL")) { + socket.write( + Buffer.concat([pgErrorResponse({ S: "ERROR", C: "XX000", M: "mock failure" }), pgReadyForQuery()]), + ); + continue; + } + socket.write(Buffer.concat([pgCommandComplete("SELECT 0"), pgReadyForQuery()])); + } + }); + } }); }; -const mysqlMockServer: MockServer = received => { +const mysqlMockServer: MockServer = (received, { onReceived, secure } = {}) => { const COM_QUIT = 0x01; const COM_QUERY = 0x03; let nextConn = 0; - return listeningServer(socket => { + return listeningServer(rawSocket => { const connId = nextConn++; - let buffered = Buffer.alloc(0); - let authed = false; - socket.write(mysqlHandshakeV10()); - socket.on("data", (chunk: Buffer) => { - buffered = mysqlReadPackets(Buffer.concat([buffered, chunk]), (seq, payload) => { - if (!authed) { - authed = true; - socket.write(mysqlOkPacket(seq + 1)); - return; - } - if (mysqlAckSessionSetup(socket, payload)) return; - if (payload[0] === COM_QUERY) { - const sql = payload.subarray(1).toString("utf8"); - received.push({ conn: connId, sql }); - if (sql.includes("KILL")) { - socket.destroy(); + rawSocket.on("error", () => {}); + rawSocket.write(mysqlHandshakeV10({ capabilities: MYSQL_DEFAULT_CAPABILITIES | (secure ? MYSQL_CLIENT_SSL : 0) })); + if (secure) { + // One plaintext packet, the SSLRequest. The client starts its ClientHello right after + // it, so bytes past the packet are TLS records and go back for the TLS socket to read. + let plain = Buffer.alloc(0); + const onPlainData = (chunk: Buffer) => { + plain = Buffer.concat([plain, chunk]); + if (plain.length < 4) return; + const end = 4 + plain.readUIntLE(0, 3); + if (plain.length < end) return; + rawSocket.removeListener("data", onPlainData); + rawSocket.pause(); + if (plain.length > end) rawSocket.unshift(plain.subarray(end)); + serve(secureSocket(rawSocket)); + }; + rawSocket.on("data", onPlainData); + } else { + serve(rawSocket); + } + + function serve(socket: net.Socket) { + let buffered = Buffer.alloc(0); + let authed = false; + socket.on("data", (chunk: Buffer) => { + buffered = mysqlReadPackets(Buffer.concat([buffered, chunk]), (seq, payload) => { + if (!authed) { + authed = true; + socket.write(mysqlOkPacket(seq + 1)); return; } - socket.write(mysqlOkPacket(1)); - } else if (payload[0] === COM_QUIT) { - socket.end(); - } + if (mysqlAckSessionSetup(socket, payload)) return; + if (payload[0] === COM_QUERY) { + const sql = payload.subarray(1).toString("utf8"); + received.push({ conn: connId, sql }); + onReceived?.(); + if (sql.includes("KILL")) { + socket.destroy(); + return; + } + if (sql.includes("HOLD")) return; + if (sql.includes("FAIL")) { + socket.write(mysqlErrPacket(1, 1105, "HY000", "mock failure")); + return; + } + socket.write(mysqlOkPacket(1)); + } else if (payload[0] === COM_QUIT) { + socket.end(); + } + }); }); - }); - socket.on("error", () => {}); + } }); }; @@ -109,11 +175,40 @@ function firstInterleaving(received: Received[]): string | null { return null; } -const adapters: Array<{ adapter: "postgres" | "mysql"; mockServer: MockServer; beginCommand: string }> = [ - { adapter: "postgres", mockServer: pgMockServer, beginCommand: "BEGIN" }, - { adapter: "mysql", mockServer: mysqlMockServer, beginCommand: "START TRANSACTION" }, +const adapters: Array<{ + adapter: "postgres" | "mysql"; + mockServer: MockServer; + beginCommand: string; + connectionClosedCode: string; +}> = [ + { + adapter: "postgres", + mockServer: pgMockServer, + beginCommand: "BEGIN", + connectionClosedCode: "ERR_POSTGRES_CONNECTION_CLOSED", + }, + { + adapter: "mysql", + mockServer: mysqlMockServer, + beginCommand: "START TRANSACTION", + connectionClosedCode: "ERR_MYSQL_CONNECTION_CLOSED", + }, ]; +// A transaction on the reservation whose callback parks after its first statement until +// finish() is called, so a test controls when the transaction settles. +function parkedTransaction(reserved: Bun.ReservedSQL, statement: string) { + const started = Promise.withResolvers(); + const gate = Promise.withResolvers(); + const promise = reserved.begin(async tx => { + await tx.unsafe(statement); + started.resolve(); + await gate.promise; + return "committed"; + }); + return { promise, started: started.promise, finish: gate.resolve }; +} + // reserved.begin() / beginDistributed() calls that reject before anything is sent. const rejectedBeforeBegin = [ { @@ -128,7 +223,7 @@ const rejectedBeforeBegin = [ }, ]; -describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand }) => { +describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand, connectionClosedCode }) => { const options = (port: number): Bun.SQL.Options => ({ adapter, hostname: "127.0.0.1", @@ -238,6 +333,8 @@ describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand }) => { return "t1"; }); expect(t1).toBe("t1"); + // A graceful close waits for pending work. It hangs if the pool still counts the reservation. + await sql.close(); } finally { await sql.close({ timeout: 0 }).catch(() => {}); await new Promise(r => server.close(() => r())); @@ -335,62 +432,289 @@ describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand }) => { }, ); - test("reserved.close({ timeout }) waits for a transaction started on the reservation", async () => { + // reserved.close({ timeout }) waits, up to the timeout (in seconds), for the queries and + // transactions that are pending on the reservation, then closes the connection. The + // connection's close handler returns the pool slot. The tests below use a pool whose + // onclose callback counts the connections the pool has closed so far. + type CloseTestPool = { + sql: SQL; + received: Received[]; + closes: () => number; + firstClose: Promise; + [Symbol.asyncDispose](): Promise; + }; + async function closeTestPool(mock: MockOptions = {}, max = 1): Promise { const received: Received[] = []; - const { port, server } = await mockServer(received); - const sql = new SQL(options(port)); - try { - const reserved = await sql.reserve(); - const t1 = reserved.begin(async tx => { - await tx.unsafe("SELECT 'T1a'"); - return "t1"; - }); - // The timeout is in seconds. close() resolves as soon as t1 settles; without the - // transaction being tracked it would close the connection under t1 instead. - const closed = reserved.close({ timeout: 60 }); - expect(await t1).toBe("t1"); - await closed; - reserved.release(); - expect(received).toEqual([ - { conn: 0, sql: beginCommand }, - { conn: 0, sql: "SELECT 'T1a'" }, - { conn: 0, sql: "COMMIT" }, - ]); - } finally { - await sql.close({ timeout: 0 }).catch(() => {}); - await new Promise(r => server.close(() => r())); - } + const { port, server } = await mockServer(received, mock); + const firstClose = Promise.withResolvers(); + let closes = 0; + const sql = new SQL({ + ...options(port), + max, + tls: mock.secure ? { rejectUnauthorized: false } : false, + onclose: () => { + closes++; + firstClose.resolve(); + }, + }); + return { + sql, + received, + closes: () => closes, + firstClose: firstClose.promise, + async [Symbol.asyncDispose]() { + await sql.close({ timeout: 0 }).catch(() => {}); + await new Promise(r => server.close(() => r())); + }, + }; + } + + // The statements of the transaction that expectSlotReturned / expectConnectionKept run. + const afterTransaction = (conn: number): Received[] => [ + { conn, sql: beginCommand }, + { conn, sql: "SELECT 'after'" }, + { conn, sql: "COMMIT" }, + ]; + const runAfterTransaction = (sql: SQL) => + sql.begin(async tx => { + await tx.unsafe("SELECT 'after'"); + return "after"; + }); + + // close() closed the reservation's connection and gave its slot back: the pool's only + // slot reconnects (conn 1) for the next transaction, and a graceful sql.close() resolves + // because the pool no longer counts the reservation. + async function expectSlotReturned(pool: CloseTestPool) { + await pool.firstClose; + expect(await runAfterTransaction(pool.sql)).toBe("after"); + await pool.sql.close(); + } + + // close() left the connection alone: nothing was closed, and the next transaction runs + // on the same connection (conn 0). + async function expectConnectionKept(pool: CloseTestPool) { + expect(pool.closes()).toBe(0); + expect(await runAfterTransaction(pool.sql)).toBe("after"); + await pool.sql.close(); + } + + test("reserved.close({ timeout }) waits for a transaction started on the reservation", async () => { + await using pool = await closeTestPool(); + const reserved = await pool.sql.reserve(); + const t1 = reserved.begin(async tx => { + await tx.unsafe("SELECT 'T1a'"); + return "t1"; + }); + // close() resolves as soon as t1 settles; without the transaction being tracked it + // would close the connection under t1 instead. + const closed = reserved.close({ timeout: 60 }); + expect(await t1).toBe("t1"); + await closed; + // The slot already went back when close() closed the connection, so this is a no-op. + // `await using reserved` ends the same way. + reserved.release(); + await expectSlotReturned(pool); + expect(pool.received).toEqual([ + { conn: 0, sql: beginCommand }, + { conn: 0, sql: "SELECT 'T1a'" }, + { conn: 0, sql: "COMMIT" }, + ...afterTransaction(1), + ]); }); // Same overlap with a transaction that fails. close() must wait for the ROLLBACK, and // the failure is the caller's to handle: bun:test fails this test if close()'s wait // reports it as an unhandled rejection as well. test("reserved.close({ timeout }) waits for a failing transaction without reporting its handled error", async () => { - const received: Received[] = []; - const { port, server } = await mockServer(received); - const sql = new SQL(options(port)); - try { - const reserved = await sql.reserve(); - const failing = reserved - .begin(async tx => { - await tx.unsafe("SELECT 'T1a'"); - throw new Error("t1-app-error"); - }) - .catch(err => err.message); - const closed = reserved.close({ timeout: 60 }); - expect(await failing).toBe("t1-app-error"); - await closed; + await using pool = await closeTestPool(); + const reserved = await pool.sql.reserve(); + const failing = reserved + .begin(async tx => { + await tx.unsafe("SELECT 'T1a'"); + throw new Error("t1-app-error"); + }) + .catch(err => err.message); + const closed = reserved.close({ timeout: 60 }); + expect(await failing).toBe("t1-app-error"); + await closed; + reserved.release(); + await expectSlotReturned(pool); + expect(pool.received).toEqual([ + { conn: 0, sql: beginCommand }, + { conn: 0, sql: "SELECT 'T1a'" }, + { conn: 0, sql: "ROLLBACK" }, + ...afterTransaction(1), + ]); + }); + + test("reserved.close({ timeout }) closes the connection once the pending query finishes", async () => { + await using pool = await closeTestPool(); + const reserved = await pool.sql.reserve(); + const inFlight = reserved.unsafe("SELECT 'in flight'").execute(); + const closed = reserved.close({ timeout: 60 }); + await inFlight; + await closed; + await expectSlotReturned(pool); + expect(pool.received).toEqual([{ conn: 0, sql: "SELECT 'in flight'" }, ...afterTransaction(1)]); + }); + + // Neither of these ever reaches the server, and neither leaves the reservation's set of + // pending queries, so close({ timeout }) takes its wait with nothing in flight. It still + // has to close the connection. bun:test also fails these tests if the wait reports the + // entry's rejection as unhandled. + test.each([ + { + entry: "an identifier helper", + make: async (reserved: Bun.ReservedSQL) => { + reserved("users"); + }, + }, + { + entry: "a query whose text cannot be built", + make: async (reserved: Bun.ReservedSQL) => { + const error = await reserved`INSERT INTO users ${reserved({})}`.then( + () => null, + e => e.message, + ); + expect(error).toBe("Cannot INSERT with no columns"); + }, + }, + ])("reserved.close({ timeout }) closes a reservation whose only pending entry is $entry", async ({ make }) => { + await using pool = await closeTestPool(); + const reserved = await pool.sql.reserve(); + await make(reserved); + await reserved.close({ timeout: 60 }); + await expectSlotReturned(pool); + expect(pool.received).toEqual(afterTransaction(1)); + }); + + // A query that fails while close() waits does not end the wait: the transaction that is + // still open on the same reservation must get to COMMIT before the connection closes. + // bun:test also fails this test if close()'s wait reports the handled failure as unhandled. + test("reserved.close({ timeout }) keeps waiting for the other pending work when one query fails", async () => { + await using pool = await closeTestPool(); + const reserved = await pool.sql.reserve(); + const t1 = parkedTransaction(reserved, "SELECT 'T1a'"); + await t1.started; + const failed = reserved.unsafe("SELECT 'FAIL'").then( + () => null, + e => e.message, + ); + const closed = reserved.close({ timeout: 60 }); + expect(await failed).toBe("mock failure"); + t1.finish(); + expect(await t1.promise).toBe("committed"); + await closed; + await expectSlotReturned(pool); + expect(pool.received).toEqual([ + { conn: 0, sql: beginCommand }, + { conn: 0, sql: "SELECT 'T1a'" }, + { conn: 0, sql: "SELECT 'FAIL'" }, + { conn: 0, sql: "COMMIT" }, + ...afterTransaction(1), + ]); + }); + + // The query that close() cuts off here rejects, and that rejection belongs to its caller. + // bun:test fails this test if close()'s own wait reports it as unhandled as well. + test("reserved.close({ timeout }) closes the connection under a query that outlives the timeout", async () => { + const held = Promise.withResolvers(); + await using pool = await closeTestPool({ onReceived: () => held.resolve() }); + const reserved = await pool.sql.reserve(); + const neverAnswered = reserved.unsafe("SELECT 'HOLD'").then( + () => null, + e => e.code, + ); + // The server has the query and will not answer it, so only the timer can end close(). + await held.promise; + await reserved.close({ timeout: 0.05 }); + expect(await neverAnswered).toBe(connectionClosedCode); + await expectSlotReturned(pool); + expect(pool.received).toEqual([{ conn: 0, sql: "SELECT 'HOLD'" }, ...afterTransaction(1)]); + }); + + // bun:test fails this test if the queries the server took down are reported as unhandled + // by close()'s wait. The close handler already returned the slot when the server dropped + // the connection, so close() has nothing left to do but resolve. + test("reserved.close({ timeout }) resolves when the server drops the connection while it waits", async () => { + await using pool = await closeTestPool(); + const reserved = await pool.sql.reserve(); + const dropped = reserved.unsafe("SELECT 'KILL'").then( + () => null, + e => e.code, + ); + const closed = reserved.close({ timeout: 60 }); + expect(await dropped).toBe(connectionClosedCode); + await closed; + await expectSlotReturned(pool); + expect(pool.received).toEqual([{ conn: 0, sql: "SELECT 'KILL'" }, ...afterTransaction(1)]); + }); + + // release() during the wait gives the connection back to the pool, where it may already + // serve other callers, so neither way of ending the wait may close it. The transaction + // still open on it is the probe: it can only commit if the connection stayed open. + test.each([ + { ending: "the pending transaction finishes", timeout: 60, timerEndsTheWait: false }, + { ending: "the timer fires", timeout: 0.05, timerEndsTheWait: true }, + ])( + "reserved.close({ timeout }) leaves a connection alone that release() gave back before $ending", + async ({ timeout, timerEndsTheWait }) => { + await using pool = await closeTestPool(); + const reserved = await pool.sql.reserve(); + const t1 = parkedTransaction(reserved, "SELECT 'T1a'"); + await t1.started; + const closed = reserved.close({ timeout }); reserved.release(); - expect(received).toEqual([ + // While t1 is parked, only the timer can end the wait. + if (timerEndsTheWait) await closed; + t1.finish(); + expect(await t1.promise).toBe("committed"); + await closed; + await expectConnectionKept(pool); + expect(pool.received).toEqual([ { conn: 0, sql: beginCommand }, { conn: 0, sql: "SELECT 'T1a'" }, - { conn: 0, sql: "ROLLBACK" }, + { conn: 0, sql: "COMMIT" }, + ...afterTransaction(0), ]); - } finally { - await sql.close({ timeout: 0 }).catch(() => {}); - await new Promise(r => server.close(() => r())); - } - }); + }, + ); + + // Over TLS the socket does not close inside close(): the close handler, which returns the + // slot, runs only after the peer has answered the close_notify. A release() in between, + // which is what `await using` does, must not put the closing connection back into the pool. + // The pool spreads two queries over every connection it holds ready, so one of them would + // land on it and fail. + test.each([ + { + call: "close()", + close: async (reserved: Bun.ReservedSQL) => { + await reserved.unsafe("SELECT 'done'"); + await reserved.close(); + }, + }, + { + call: "close({ timeout })", + close: async (reserved: Bun.ReservedSQL) => { + const inFlight = reserved.unsafe("SELECT 'in flight'").execute(); + const closed = reserved.close({ timeout: 60 }); + await inFlight; + await closed; + }, + }, + ])( + "reserved.release() after $call leaves a TLS connection that is still closing to its close handler", + async ({ close }) => { + await using pool = await closeTestPool({ secure: true }, 2); + const reserved = await pool.sql.reserve(); + await close(reserved); + reserved.release(); + const results = await Promise.allSettled([pool.sql.unsafe("SELECT 'a'"), pool.sql.unsafe("SELECT 'b'")]); + expect(results.map(result => result.status)).toEqual(["fulfilled", "fulfilled"]); + await pool.firstClose; + await pool.sql.close(); + }, + ); // Runs in a child process: bun:test would turn any unhandled rejection into a test // failure, and the second half of this contract is that one rejection IS reported. diff --git a/test/js/sql/wire-frames.test.ts b/test/js/sql/wire-frames.test.ts index 2bbed36e31f7..14d6ee94a1a0 100644 --- a/test/js/sql/wire-frames.test.ts +++ b/test/js/sql/wire-frames.test.ts @@ -8,6 +8,7 @@ import { expect, test } from "bun:test"; import { listeningServer, mysqlAckSessionSetup, + mysqlErrPacket, mysqlHandshakeV10, mysqlLenencInt, mysqlOkPacket, @@ -40,6 +41,11 @@ test("mysqlLenencInt encodes per page_protocol_basic_dt_integers.html", () => { expect(mysqlLenencInt(0x1_00_0000n)).toEqual(Buffer.from([0xfe, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00])); }); +test("mysqlErrPacket encodes per page_protocol_basic_err_packet.html", () => { + // error_code 1064 = 0x0428, little-endian; the 3-byte packet length counts the payload only + expect(mysqlErrPacket(1, 1064, "42000", "x")).toEqual(Buffer.from("\x0a\x00\x00\x01\xff\x28\x04#42000x", "binary")); +}); + test("pgErrorResponse encodes per §55.7", () => { expect(pgErrorResponse({ S: "FATAL", C: "57P03", M: "x" })).toEqual( Buffer.from("E\x00\x00\x00\x16SFATAL\x00C57P03\x00Mx\x00\x00", "binary"), From 884c8e90160c1f2521762ba0d8bcabca63d802b8 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Tue, 6 Oct 2026 23:39:15 +0000 Subject: [PATCH 03/13] sql: roll back when transaction.close({ timeout }) drains before the timeout Every arm of transaction.close() runs one memoized helper that cancels the pending queries, sends ROLLBACK, and sets the closed bit. Squash of #32149. --- src/js/bun/sql.ts | 68 +-- test/js/sql/sql-transaction-close.test.ts | 479 ++++++++++++++++++++++ 2 files changed, 522 insertions(+), 25 deletions(-) create mode 100644 test/js/sql/sql-transaction-close.test.ts diff --git a/src/js/bun/sql.ts b/src/js/bun/sql.ts index be519765d99a..60d15ba6eccc 100644 --- a/src/js/bun/sql.ts +++ b/src/js/bun/sql.ts @@ -671,6 +671,36 @@ const SQL = function SQL( } return unsafeQueryFromTransaction(string, [], pooledConnection, state.queries); } + // every transaction_sql.close path converges here: cancel whatever is + // still pending, roll the transaction back, and only then set the closed + // bit (run_internal_transaction_sql rejects once it is set), so a later + // COMMIT attempt fails with connectionClosedError instead of committing a + // closed transaction. The pooled connection itself stays open; it is + // released back to the pool by onTransactionConnected once the begin + // callback settles. + let closeTransactionPromise: Promise | null = null; + async function rollbackAndMarkClosed() { + if (state.connectionState & ReservedConnectionState.closed) { + // the connection dropped while close() was waiting; the server + // already aborted the transaction so there is nothing to roll back + return; + } + for (const query of state.queries) { + (query as Query).cancel(); + } + try { + if (BEFORE_COMMIT_OR_ROLLBACK_COMMAND) { + await run_internal_transaction_sql(BEFORE_COMMIT_OR_ROLLBACK_COMMAND); + } + await run_internal_transaction_sql(ROLLBACK_COMMAND); + } finally { + // even when ROLLBACK fails the transaction must not be committable + state.connectionState |= ReservedConnectionState.closed; + } + } + function closeTransaction() { + return (closeTransactionPromise ??= rollbackAndMarkClosed()); + } function transaction_sql( strings: string | TemplateStringsArray | import("internal/sql/shared.ts").SQLHelper | Query, ...values: any[] @@ -777,37 +807,25 @@ const SQL = function SQL( } if (timeout > 0 && (transactionQueries.size > 0 || transactionSavepoints.size > 0)) { - const { promise, resolve } = Promise.withResolvers(); - // race all queries vs timeout + const { promise, resolve, reject } = Promise.withResolvers(); + // grace period: wait up to the timeout for the pending queries and + // savepoints to settle before rolling back const pending_queries = Array.from(transactionQueries); const pending_savepoints = Array.from(transactionSavepoints); - const timer = setTimeout(async () => { - for (const query of transactionQueries) { - (query as Query).cancel(); - } - if (BEFORE_COMMIT_OR_ROLLBACK_COMMAND) { - await run_internal_transaction_sql(BEFORE_COMMIT_OR_ROLLBACK_COMMAND); - } - await run_internal_transaction_sql(ROLLBACK_COMMAND); - state.connectionState |= ReservedConnectionState.closed; - resolve(); - }, timeout * 1000); - timer.unref(); // dont block the event loop - Promise.all([Promise.all(pending_queries), Promise.all(pending_savepoints)]).finally(() => { + function settleClose() { clearTimeout(timer); - resolve(); - }); + closeTransaction().then(resolve, reject); + } + const timer = setTimeout(settleClose, timeout * 1000); + timer.unref(); // dont block the event loop + // allSettled: one rejecting query must not cut the grace period + // short for the rest, and its rejection is observed here instead of + // surfacing as an unhandled rejection + Promise.allSettled([...pending_queries, ...pending_savepoints]).then(settleClose, settleClose); return promise; } } - for (const query of transactionQueries) { - (query as Query).cancel(); - } - if (BEFORE_COMMIT_OR_ROLLBACK_COMMAND) { - await run_internal_transaction_sql(BEFORE_COMMIT_OR_ROLLBACK_COMMAND); - } - await run_internal_transaction_sql(ROLLBACK_COMMAND); - state.connectionState |= ReservedConnectionState.closed; + return closeTransaction(); }; transaction_sql[Symbol.asyncDispose] = () => transaction_sql.close(); transaction_sql.options = sql.options; diff --git a/test/js/sql/sql-transaction-close.test.ts b/test/js/sql/sql-transaction-close.test.ts new file mode 100644 index 000000000000..8c8c90663661 --- /dev/null +++ b/test/js/sql/sql-transaction-close.test.ts @@ -0,0 +1,479 @@ +// https://github.com/oven-sh/bun/issues/32148 +// transaction.close({ timeout }) resolved without issuing ROLLBACK and +// without marking the transaction closed when the pending queries settled +// before the timeout, so the COMMIT issued after the begin() callback +// returned persisted writes the user explicitly closed. The internal +// Promise.all bookkeeping was also dropped, turning a rejecting pending +// query into an unhandledRejection even when the user handled it. +// +// Uses minimal mock postgres/mysql servers so it can run without Docker. + +import { SQL } from "bun"; +import { expect, test } from "bun:test"; +import { once } from "events"; +import net from "net"; + +// a handler can claim a query by returning true; it is then responsible for +// answering on the socket. Everything else gets a canned response. +type QueryHandler = (query: string, socket: net.Socket) => boolean | void; + +// --------------------------------------------------------------------------- +// postgres mock +// --------------------------------------------------------------------------- + +function pkt(type: string, body: Buffer): Buffer { + const header = Buffer.alloc(5); + header.write(type, 0); + header.writeInt32BE(body.length + 4, 1); + return Buffer.concat([header, body]); +} + +function int16(n: number): Buffer { + const b = Buffer.alloc(2); + b.writeInt16BE(n, 0); + return b; +} + +function int32(n: number): Buffer { + const b = Buffer.alloc(4); + b.writeInt32BE(n, 0); + return b; +} + +function cstr(s: string): Buffer { + return Buffer.concat([Buffer.from(s), Buffer.from([0])]); +} + +function rowDescription(names: string[]): Buffer { + const fields = Buffer.concat( + names.map(name => + Buffer.concat([ + cstr(name), // column name + int32(0), // table oid + int16(0), // column attr number + int32(25), // type oid: text + int16(-1), // type size + int32(-1), // type modifier + int16(0), // format: text + ]), + ), + ); + return pkt("T", Buffer.concat([int16(names.length), fields])); +} + +function dataRow(values: string[]): Buffer { + const cols = Buffer.concat( + values.map(v => { + const bytes = Buffer.from(v); + return Buffer.concat([int32(bytes.length), bytes]); + }), + ); + return pkt("D", Buffer.concat([int16(values.length), cols])); +} + +const authenticationOk = pkt("R", int32(0)); +const readyForQuery = pkt("Z", Buffer.from("I")); + +function selectResponse(value: string): Buffer { + return Buffer.concat([rowDescription(["x"]), dataRow([value]), pkt("C", cstr("SELECT 1")), readyForQuery]); +} + +function commandResponse(query: string): Buffer { + // the command tag is the statement's first word: BEGIN, ROLLBACK, ... + return Buffer.concat([pkt("C", cstr(query.split(" ")[0].toUpperCase())), readyForQuery]); +} + +function errorResponse(message: string): Buffer { + return pkt( + "E", + Buffer.concat([ + Buffer.from("S"), + cstr("ERROR"), + Buffer.from("C"), + cstr("XX000"), + Buffer.from("M"), + cstr(message), + Buffer.from([0]), + ]), + ); +} + +async function startPostgresServer(onQuery: QueryHandler = () => {}) { + const sockets = new Set(); + const commands: string[] = []; + const server = net.createServer(socket => { + sockets.add(socket); + socket.on("close", () => sockets.delete(socket)); + socket.on("error", () => {}); + let startup = true; + let buffered = Buffer.alloc(0); + socket.on("data", data => { + buffered = Buffer.concat([buffered, data]); + if (startup) { + // the startup message has no type byte: int32 length + payload + if (buffered.length < 4) return; + const len = buffered.readInt32BE(0); + if (buffered.length < len) return; + buffered = buffered.subarray(len); + startup = false; + socket.write(Buffer.concat([authenticationOk, readyForQuery])); + } + // regular messages: type byte + int32 length (which includes itself) + while (buffered.length >= 5) { + const len = buffered.readInt32BE(1); + if (buffered.length < 1 + len) break; + const type = buffered[0]; + const payload = buffered.subarray(5, 1 + len); + buffered = buffered.subarray(1 + len); + if (type === 0x51 /* 'Q' simple query */) { + const query = payload.toString("utf8").replace(/\0$/, ""); + commands.push(query); + if (!onQuery(query, socket)) { + socket.write(/^select/i.test(query) ? selectResponse("1") : commandResponse(query)); + } + } + // anything else ('X' Terminate, ...) needs no reply + } + }); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const { port } = server.address() as net.AddressInfo; + return { + port, + commands, + async [Symbol.asyncDispose]() { + for (const socket of sockets) socket.destroy(); + await new Promise(r => server.close(() => r())); + }, + }; +} + +// --------------------------------------------------------------------------- +// mysql mock +// --------------------------------------------------------------------------- + +function u16le(n: number): Buffer { + return Buffer.from([n & 0xff, (n >> 8) & 0xff]); +} +function u24le(n: number): Buffer { + return Buffer.from([n & 0xff, (n >> 8) & 0xff, (n >> 16) & 0xff]); +} +function u32le(n: number): Buffer { + return Buffer.from([n & 0xff, (n >> 8) & 0xff, (n >> 16) & 0xff, (n >>> 24) & 0xff]); +} +function packet(seq: number, payload: Buffer): Buffer { + return Buffer.concat([u24le(payload.length), Buffer.from([seq]), payload]); +} +function lenencStr(s: string): Buffer { + const buf = Buffer.from(s, "utf-8"); + return Buffer.concat([Buffer.from([buf.length]), buf]); +} + +const CLIENT_PROTOCOL_41 = 1 << 9; +const CLIENT_SECURE_CONNECTION = 1 << 15; +const CLIENT_PLUGIN_AUTH = 1 << 19; +const CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA = 1 << 21; +const CLIENT_DEPRECATE_EOF = 1 << 24; +const SERVER_CAPS = + CLIENT_PROTOCOL_41 | + CLIENT_SECURE_CONNECTION | + CLIENT_PLUGIN_AUTH | + CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA | + CLIENT_DEPRECATE_EOF; + +function handshakeV10(): Buffer { + const authData1 = Buffer.alloc(8, 0x61); + const authData2 = Buffer.alloc(13, 0x62); + authData2[12] = 0; + return packet( + 0, + Buffer.concat([ + Buffer.from([10]), // protocol version + Buffer.from("mock-5.7.0\0"), // server version + u32le(1), // connection id + authData1, // auth-plugin-data-part-1 + Buffer.from([0]), // filler + u16le(SERVER_CAPS & 0xffff), // capability flags lower + Buffer.from([0x2d]), // character set (utf8mb4_general_ci) + u16le(0x0002), // status flags (SERVER_STATUS_AUTOCOMMIT) + u16le((SERVER_CAPS >>> 16) & 0xffff), // capability flags upper + Buffer.from([21]), // length of auth-plugin-data + Buffer.alloc(10, 0), // reserved + authData2, // auth-plugin-data-part-2 + Buffer.from("mysql_native_password\0"), + ]), + ); +} + +function okPacket(seq: number, header = 0x00): Buffer { + return packet(seq, Buffer.from([header, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00])); +} + +// VARCHAR column named "x" (utf8mb4, no flags) +function varcharColumn(): Buffer { + return Buffer.concat([ + lenencStr("def"), + lenencStr(""), + lenencStr("t"), + lenencStr("t"), + lenencStr("x"), + lenencStr("x"), + Buffer.from([0x0c]), // length of fixed fields + u16le(0x2d), // charset utf8mb4_general_ci + u32le(1024), // column length + Buffer.from([0xfd]), // type: VAR_STRING + u16le(0), // flags + Buffer.from([0]), // decimals + Buffer.from([0, 0]), // filler + ]); +} + +// Text-protocol result set for a single-row `select ... as x` (COM_QUERY) +function textResultSet(startSeq: number, value: string): Buffer { + let seq = startSeq; + return Buffer.concat([ + packet(seq++, Buffer.from([1])), // column count + packet(seq++, varcharColumn()), + packet(seq++, lenencStr(value)), // row + okPacket(seq++, 0xfe), // DEPRECATE_EOF terminator + ]); +} + +async function startMysqlServer(onQuery: QueryHandler = () => {}) { + const sockets = new Set(); + const commands: string[] = []; + const server = net.createServer(socket => { + sockets.add(socket); + socket.on("close", () => sockets.delete(socket)); + socket.on("error", () => {}); + let buffered = Buffer.alloc(0); + let authed = false; + socket.write(handshakeV10()); + socket.on("data", chunk => { + buffered = Buffer.concat([buffered, chunk]); + while (buffered.length >= 4) { + const len = buffered[0] | (buffered[1] << 8) | (buffered[2] << 16); + if (buffered.length < 4 + len) break; + const seq = buffered[3]; + const payload = buffered.subarray(4, 4 + len); + buffered = buffered.subarray(4 + len); + if (!authed) { + // HandshakeResponse41 from client → accept unconditionally + authed = true; + socket.write(okPacket(seq + 1)); + continue; + } + const cmd = payload[0]; + if (cmd === 0x03 /* COM_QUERY */) { + const query = payload.subarray(1).toString("utf8"); + commands.push(query); + if (!onQuery(query, socket)) { + socket.write(/^select/i.test(query) ? textResultSet(seq + 1, "1") : okPacket(seq + 1)); + } + } else if (cmd === 0x19 /* COM_STMT_CLOSE */) { + // no response expected + } else { + // COM_QUIT or anything else → close + socket.end(); + } + } + }); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const { port } = server.address() as net.AddressInfo; + return { + port, + commands, + async [Symbol.asyncDispose]() { + for (const socket of sockets) socket.destroy(); + await new Promise(r => server.close(() => r())); + }, + }; +} + +// --------------------------------------------------------------------------- +// tests +// --------------------------------------------------------------------------- + +test("postgres: transaction.close({ timeout }) rolls back when pending queries drain before the timeout", async () => { + const slowQuery = Promise.withResolvers(); + await using pg = await startPostgresServer((query, socket) => { + if (query.includes("hold")) { + slowQuery.resolve(socket); + return true; // response withheld until the test releases it + } + }); + await using sql = new SQL({ url: `postgres://u@127.0.0.1:${pg.port}/db`, max: 1, connectionTimeout: 5 }); + + let pendingResult: unknown; + let queryAfterClose: unknown; + const begin = sql.begin(async tx => { + const pending = tx`select 'hold' as x`.simple().execute(); + const socket = await slowQuery.promise; // the query is now in flight + const closed = tx.close({ timeout: 5 }); + // let the pending query finish long before the 5s timeout + socket.write(selectResponse("hold")); + pendingResult = await pending; + await closed; + // the transaction no longer accepts queries (a closed transaction hands + // back a plain rejected promise, not a Query) + queryAfterClose = await (tx`select 1 as x` as Promise).then( + () => "resolved", + (err: any) => err.code, + ); + // close is idempotent + await tx.close(); + }); + + // close() rolled back, so the COMMIT attempt after the callback must fail + await expect(begin).rejects.toMatchObject({ code: "ERR_POSTGRES_CONNECTION_CLOSED" }); + expect(pendingResult).toEqual([{ x: "hold" }]); + expect(queryAfterClose).toBe("ERR_POSTGRES_CONNECTION_CLOSED"); + // the rejected post-close query never reached the wire + expect(pg.commands).not.toContain("select 1 as x"); + expect(pg.commands).toContain("ROLLBACK"); + expect(pg.commands).not.toContain("COMMIT"); +}); + +test("postgres: transaction.close({ timeout }) does not leak an unhandledRejection when a pending query fails", async () => { + const unhandled: unknown[] = []; + const onUnhandled = (err: unknown) => unhandled.push(err); + process.on("unhandledRejection", onUnhandled); + try { + const failingQuery = Promise.withResolvers(); + await using pg = await startPostgresServer((query, socket) => { + if (query.includes("boom")) { + failingQuery.resolve(socket); + return true; + } + }); + await using sql = new SQL({ url: `postgres://u@127.0.0.1:${pg.port}/db`, max: 1, connectionTimeout: 5 }); + + let handledAs: unknown; + const begin = sql.begin(async tx => { + const failing = tx`select 'boom' as x`.simple().execute(); + const handled = failing.then( + () => "resolved", + () => "handled", + ); + const socket = await failingQuery.promise; + const closed = tx.close({ timeout: 5 }); + // fail the pending query during the grace period + socket.write(Buffer.concat([errorResponse("boom"), readyForQuery])); + handledAs = await handled; + await closed; + }); + + await expect(begin).rejects.toMatchObject({ code: "ERR_POSTGRES_CONNECTION_CLOSED" }); + expect(handledAs).toBe("handled"); + expect(pg.commands).toContain("ROLLBACK"); + expect(pg.commands).not.toContain("COMMIT"); + // tear the pool down now, while the unhandledRejection listener is still + // attached (the await using dispose after the finally would be too late); + // close is idempotent so the second dispose at scope exit is a no-op + await sql.close(); + + // the query rejection was handled by the user; close()'s internal + // bookkeeping must not surface it again (one macrotask turn so a report + // queued by the teardown above still lands while the listener is attached) + await Bun.sleep(0); + expect(unhandled).toEqual([]); + } finally { + process.off("unhandledRejection", onUnhandled); + } +}); + +test("postgres: transaction.close({ timeout }) waits for pending savepoints and still rolls back", async () => { + const slowQuery = Promise.withResolvers(); + await using pg = await startPostgresServer((query, socket) => { + if (query.includes("hold")) { + slowQuery.resolve(socket); + return true; + } + }); + await using sql = new SQL({ url: `postgres://u@127.0.0.1:${pg.port}/db`, max: 1, connectionTimeout: 5 }); + + let savepointResult: unknown; + const begin = sql.begin(async tx => { + const savepoint = tx.savepoint(async sp => { + return await sp`select 'hold' as x`.simple().execute(); + }); + const socket = await slowQuery.promise; + const closed = tx.close({ timeout: 5 }); + // the savepoint finishes (including RELEASE SAVEPOINT) before the timeout + socket.write(selectResponse("hold")); + savepointResult = await savepoint; + await closed; + }); + + await expect(begin).rejects.toMatchObject({ code: "ERR_POSTGRES_CONNECTION_CLOSED" }); + expect(savepointResult).toEqual([{ x: "hold" }]); + expect(pg.commands).toContain("SAVEPOINT s0"); + expect(pg.commands).toContain("RELEASE SAVEPOINT s0"); + expect(pg.commands).toContain("ROLLBACK"); + expect(pg.commands).not.toContain("COMMIT"); +}); + +test("postgres: transaction.close({ timeout }) still rolls back when the timeout fires first", async () => { + const slowQuery = Promise.withResolvers(); + await using pg = await startPostgresServer((query, socket) => { + if (query.includes("hold")) { + slowQuery.resolve(socket); + return true; + } + }); + await using sql = new SQL({ url: `postgres://u@127.0.0.1:${pg.port}/db`, max: 1, connectionTimeout: 5 }); + + const begin = sql.begin(async tx => { + const pending = tx`select 'hold' as x`.simple().execute(); + const socket = await slowQuery.promise; + // fractional seconds are accepted by the timeout validation; a small + // value keeps the test fast, and nothing races the timer here: the held + // query is only answered after the timer's cancellation is observed + const closed = tx.close({ timeout: 0.1 }); + // the timer fired once close() cancelled the pending queries + while (!pending.cancelled) { + await Bun.sleep(5); + } + // ROLLBACK can only go out on the wire after the in-flight query + // completes (simple queries never pipeline), so it cannot have reached + // the server yet; answer the held query now (settlement of a cancelled + // in-flight query is not the subject here) + expect(pg.commands).not.toContain("ROLLBACK"); + socket.write(selectResponse("hold")); + await (pending as Promise).catch(() => {}); + await closed; + }); + + await expect(begin).rejects.toMatchObject({ code: "ERR_POSTGRES_CONNECTION_CLOSED" }); + expect(pg.commands).toContain("ROLLBACK"); + expect(pg.commands).not.toContain("COMMIT"); +}); + +test("mysql: transaction.close({ timeout }) rolls back when pending queries drain before the timeout", async () => { + const slowQuery = Promise.withResolvers(); + await using my = await startMysqlServer((query, socket) => { + if (query.includes("hold")) { + slowQuery.resolve(socket); + return true; + } + }); + await using sql = new SQL({ url: `mysql://root@127.0.0.1:${my.port}/db`, max: 1, connectionTimeout: 5 }); + + let pendingResult: unknown; + const begin = sql.begin(async tx => { + const pending = tx`select 'hold' as x`.simple().execute(); + const socket = await slowQuery.promise; + const closed = tx.close({ timeout: 5 }); + socket.write(textResultSet(1, "hold")); + pendingResult = await pending; + await closed; + }); + + await expect(begin).rejects.toMatchObject({ code: "ERR_MYSQL_CONNECTION_CLOSED" }); + expect(pendingResult).toEqual([{ x: "hold" }]); + expect(my.commands).toContain("ROLLBACK"); + expect(my.commands).not.toContain("COMMIT"); +}); From aa908f90dca1542ad8d2b08cd3fe16bc5615cf63 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Tue, 6 Oct 2026 23:40:07 +0000 Subject: [PATCH 04/13] test(sql): tx.close({ timeout }) rolls back with a pending, cancelled, or awaited second query Four shapes of a second query inside sql.begin(), each in its own process. Each partial set of the three changes fails at least one shape: the unfixed build commits the second shape and waits for the timer in the other two, the cancel change alone commits the third shape. --- test/js/sql/sqlite-sql.test.ts | 54 +++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/test/js/sql/sqlite-sql.test.ts b/test/js/sql/sqlite-sql.test.ts index e97bf4f569e9..6f64615e05f7 100644 --- a/test/js/sql/sqlite-sql.test.ts +++ b/test/js/sql/sqlite-sql.test.ts @@ -1,7 +1,7 @@ import { randomUUIDv7, SQL } from "bun"; import { Database } from "bun:sqlite"; import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, mock, test } from "bun:test"; -import { isDebug, tempDir } from "harness"; +import { bunEnv, bunExe, isDebug, tempDir } from "harness"; import { existsSync } from "node:fs"; import { rm, stat } from "node:fs/promises"; import { join } from "node:path"; @@ -1367,6 +1367,58 @@ describe("Transactions", () => { }); }); +// Inside sql.begin() a row is inserted, a second query is made, and the callback closes the +// transaction with a timeout. close() waits for the second query, for at most the timeout, +// and then rolls back. The timeout is far longer than the test, so only the second query can +// end the wait. +// +// Each shape runs in its own process: a rejection that close() leaves unhandled then shows on +// stderr and in the exit code. The row count comes from a second connection, which sees +// committed rows only. +describe("tx.close({ timeout }) rolls the transaction back", () => { + const fixture = ` + const [file, shape] = process.argv.slice(1); + const sql = new Bun.SQL("sqlite://" + file); + await sql\`CREATE TABLE t (v INTEGER)\`; + const begin = await sql + .begin(async tx => { + await tx\`INSERT INTO t VALUES (1)\`; + if (shape !== "no second query") { + const query = tx\`SELECT 1 AS x\`; + if (shape !== "a second query that nothing awaits") query.cancel(); + if (shape === "a second query that is cancelled before it runs, then awaited") await query.catch(() => {}); + } + await tx.close({ timeout: 60 }); + }) + .then(() => "resolved", error => error.code); + const [{ rows }] = await new Bun.SQL("sqlite://" + file)\`SELECT count(*) AS rows FROM t\`; + console.log(JSON.stringify({ begin, rows })); + `; + + test.concurrent.each([ + "no second query", + "a second query that nothing awaits", + "a second query that is cancelled before it runs", + "a second query that is cancelled before it runs, then awaited", + ])("with %s", async shape => { + using dir = tempDir("sqlite-tx-close-timeout", {}); + await using proc = Bun.spawn({ + cmd: [bunExe(), "-e", fixture, join(String(dir), "db.sqlite"), shape], + env: bunEnv, + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + + expect({ stdout: stdout.trim(), stderr, exitCode, signalCode: proc.signalCode }).toEqual({ + stdout: JSON.stringify({ begin: "ERR_SQLITE_CONNECTION_CLOSED", rows: 0 }), + stderr: "", + exitCode: 0, + signalCode: null, + }); + }); +}); + describe("SQLite-specific features", () => { let sql: SQL; From b854f04bcc846f5a7b42175d3b719a5718f9d392 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Wed, 7 Oct 2026 18:52:31 +0000 Subject: [PATCH 05/13] sql: one rollback for a closing transaction, shared by close() and the begin() runner transaction.close() awaits waitForPendingWork() like reserved.close() does, then runs rollbackTransaction(). The closing bit marks a transaction whose close() accepted its arguments. The runner reads it at the COMMIT site and rolls back through the same memoized promise, so a transaction that close() was called on never reaches COMMIT and the wire sees one ROLLBACK. --- src/js/bun/sql.ts | 107 ++++++++++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 51 deletions(-) diff --git a/src/js/bun/sql.ts b/src/js/bun/sql.ts index 60d15ba6eccc..6b12c58b2411 100644 --- a/src/js/bun/sql.ts +++ b/src/js/bun/sql.ts @@ -34,6 +34,8 @@ enum ReservedConnectionState { acceptQueries = 1 << 0, closed = 1 << 1, released = 1 << 2, + /// transaction.close() accepted its arguments: the transaction ends in ROLLBACK, never in COMMIT. + closing = 1 << 3, } interface TransactionState { @@ -41,6 +43,8 @@ interface TransactionState { reject: (err: Error) => void; storedError?: Error | null | undefined; queries: Set>; + /// The ROLLBACK of a closing transaction. close() and the transaction runner share it. + rollback?: Promise | null; } /// Bound as `this` to both callbacks of a reserve({ signal }) call, so each can @@ -273,6 +277,30 @@ const SQL = function SQL( } } + /// Ends a closing transaction: cancels what is pending, then sends `before` (XA END) and `rollback`. + /// Callers memoize it in state.rollback, so the wire sees one ROLLBACK. + async function rollbackTransaction( + state: TransactionState, + pooledConnection: PooledConnection, + before: string | null, + rollback: string, + ) { + for (const query of state.queries) { + query.cancel(); + } + try { + if (before) { + await unsafeQueryFromTransaction(before, [], pooledConnection, state.queries); + } + await unsafeQueryFromTransaction(rollback, [], pooledConnection, state.queries); + } catch (err) { + // the next caller sends it again + state.rollback = null; + throw err; + } + state.connectionState |= ReservedConnectionState.closed; + } + function onTransactionDisconnected(this: TransactionState, err: Error) { const reject = this.reject; this.connectionState |= ReservedConnectionState.closed; @@ -603,7 +631,7 @@ const SQL = function SQL( }; let savepoints = 0; - let transactionSavepoints = new Set(); + let transactionSavepoints = new Set>(); let BEGIN_COMMAND: string; let ROLLBACK_COMMAND: string; @@ -671,36 +699,6 @@ const SQL = function SQL( } return unsafeQueryFromTransaction(string, [], pooledConnection, state.queries); } - // every transaction_sql.close path converges here: cancel whatever is - // still pending, roll the transaction back, and only then set the closed - // bit (run_internal_transaction_sql rejects once it is set), so a later - // COMMIT attempt fails with connectionClosedError instead of committing a - // closed transaction. The pooled connection itself stays open; it is - // released back to the pool by onTransactionConnected once the begin - // callback settles. - let closeTransactionPromise: Promise | null = null; - async function rollbackAndMarkClosed() { - if (state.connectionState & ReservedConnectionState.closed) { - // the connection dropped while close() was waiting; the server - // already aborted the transaction so there is nothing to roll back - return; - } - for (const query of state.queries) { - (query as Query).cancel(); - } - try { - if (BEFORE_COMMIT_OR_ROLLBACK_COMMAND) { - await run_internal_transaction_sql(BEFORE_COMMIT_OR_ROLLBACK_COMMAND); - } - await run_internal_transaction_sql(ROLLBACK_COMMAND); - } finally { - // even when ROLLBACK fails the transaction must not be committable - state.connectionState |= ReservedConnectionState.closed; - } - } - function closeTransaction() { - return (closeTransactionPromise ??= rollbackAndMarkClosed()); - } function transaction_sql( strings: string | TemplateStringsArray | import("internal/sql/shared.ts").SQLHelper | Query, ...values: any[] @@ -807,25 +805,20 @@ const SQL = function SQL( } if (timeout > 0 && (transactionQueries.size > 0 || transactionSavepoints.size > 0)) { - const { promise, resolve, reject } = Promise.withResolvers(); - // grace period: wait up to the timeout for the pending queries and - // savepoints to settle before rolling back - const pending_queries = Array.from(transactionQueries); - const pending_savepoints = Array.from(transactionSavepoints); - function settleClose() { - clearTimeout(timer); - closeTransaction().then(resolve, reject); - } - const timer = setTimeout(settleClose, timeout * 1000); - timer.unref(); // dont block the event loop - // allSettled: one rejecting query must not cut the grace period - // short for the rest, and its rejection is observed here instead of - // surfacing as an unhandled rejection - Promise.allSettled([...pending_queries, ...pending_savepoints]).then(settleClose, settleClose); - return promise; + // set before the wait: the callback can settle while close() waits + state.connectionState |= ReservedConnectionState.closing; + await waitForPendingWork([...transactionQueries, ...transactionSavepoints], timeout); + // a disconnect ended the transaction, or the runner already rolls it back + if (state.connectionState & ReservedConnectionState.closed || state.rollback) return; } } - return closeTransaction(); + state.connectionState |= ReservedConnectionState.closing; + return (state.rollback ??= rollbackTransaction( + state, + pooledConnection, + BEFORE_COMMIT_OR_ROLLBACK_COMMAND, + ROLLBACK_COMMAND, + )); }; transaction_sql[Symbol.asyncDispose] = () => transaction_sql.close(); transaction_sql.options = sql.options; @@ -895,6 +888,8 @@ const SQL = function SQL( if ($isArray(transaction_result)) { transaction_result = await Promise.all(transaction_result); } + // close() was called: the catch below rolls back, nothing is committed + if (state.connectionState & ReservedConnectionState.closing) throw pool.connectionClosedError(); // at this point we dont need to rollback anymore needs_rollback = false; if (BEFORE_COMMIT_OR_ROLLBACK_COMMAND) { @@ -915,10 +910,20 @@ const SQL = function SQL( } catch (err) { try { if (!(state.connectionState & ReservedConnectionState.closed) && needs_rollback) { - if (BEFORE_COMMIT_OR_ROLLBACK_COMMAND) { - await run_internal_transaction_sql(BEFORE_COMMIT_OR_ROLLBACK_COMMAND); + if (state.connectionState & ReservedConnectionState.closing) { + // close() owns the ROLLBACK: join it, or send it if close() still waits + await (state.rollback ??= rollbackTransaction( + state, + pooledConnection, + BEFORE_COMMIT_OR_ROLLBACK_COMMAND, + ROLLBACK_COMMAND, + )); + } else { + if (BEFORE_COMMIT_OR_ROLLBACK_COMMAND) { + await run_internal_transaction_sql(BEFORE_COMMIT_OR_ROLLBACK_COMMAND); + } + await run_internal_transaction_sql(ROLLBACK_COMMAND); } - await run_internal_transaction_sql(ROLLBACK_COMMAND); } } catch (err) { return reject(err); From e82afc6bfb734997db1a868c1bcec0a221a48dd0 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Wed, 7 Oct 2026 20:37:27 +0000 Subject: [PATCH 06/13] test(sql): transaction.close() orders on the shared mocks, main's cases kept as they are The postgres and mysql transaction.close() cases move from sql-transaction-close.test.ts into sql-pool-transaction-isolation.test.ts and use its mock servers. The cases that main already has keep their text. New cases cover the orders that the COMMIT gate decides: close() that the callback does not await, a callback that throws while close() waits, close() awaited together with a failing query, a second close(), a close() whose ROLLBACK fails. The sqlite orders run in process. --- .../sql-pool-transaction-isolation.test.ts | 365 ++++++++++++- test/js/sql/sql-transaction-close.test.ts | 479 ------------------ test/js/sql/sqlite-sql.test.ts | 170 ++++--- 3 files changed, 460 insertions(+), 554 deletions(-) delete mode 100644 test/js/sql/sql-transaction-close.test.ts diff --git a/test/js/sql/sql-pool-transaction-isolation.test.ts b/test/js/sql/sql-pool-transaction-isolation.test.ts index 15e57371c52f..17ab6bc0b70d 100644 --- a/test/js/sql/sql-pool-transaction-isolation.test.ts +++ b/test/js/sql/sql-pool-transaction-isolation.test.ts @@ -180,18 +180,22 @@ const adapters: Array<{ mockServer: MockServer; beginCommand: string; connectionClosedCode: string; + // What sql.beginDistributed("dtx", fn) sends to start the transaction, and to roll it back once. + distributed: { begin: string; rollback: string[] }; }> = [ { adapter: "postgres", mockServer: pgMockServer, beginCommand: "BEGIN", connectionClosedCode: "ERR_POSTGRES_CONNECTION_CLOSED", + distributed: { begin: "BEGIN", rollback: ["ROLLBACK"] }, }, { adapter: "mysql", mockServer: mysqlMockServer, beginCommand: "START TRANSACTION", connectionClosedCode: "ERR_MYSQL_CONNECTION_CLOSED", + distributed: { begin: "XA START 'dtx'", rollback: ["XA END 'dtx'", "XA ROLLBACK 'dtx'"] }, }, ]; @@ -223,7 +227,7 @@ const rejectedBeforeBegin = [ }, ]; -describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand, connectionClosedCode }) => { +describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand, connectionClosedCode, distributed }) => { const options = (port: number): Bun.SQL.Options => ({ adapter, hostname: "127.0.0.1", @@ -333,8 +337,6 @@ describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand, connec return "t1"; }); expect(t1).toBe("t1"); - // A graceful close waits for pending work. It hangs if the pool still counts the reservation. - await sql.close(); } finally { await sql.close({ timeout: 0 }).catch(() => {}); await new Promise(r => server.close(() => r())); @@ -432,6 +434,63 @@ describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand, connec }, ); + test("reserved.close({ timeout }) waits for a transaction started on the reservation", async () => { + const received: Received[] = []; + const { port, server } = await mockServer(received); + const sql = new SQL(options(port)); + try { + const reserved = await sql.reserve(); + const t1 = reserved.begin(async tx => { + await tx.unsafe("SELECT 'T1a'"); + return "t1"; + }); + // The timeout is in seconds. close() resolves as soon as t1 settles; without the + // transaction being tracked it would close the connection under t1 instead. + const closed = reserved.close({ timeout: 60 }); + expect(await t1).toBe("t1"); + await closed; + reserved.release(); + expect(received).toEqual([ + { conn: 0, sql: beginCommand }, + { conn: 0, sql: "SELECT 'T1a'" }, + { conn: 0, sql: "COMMIT" }, + ]); + } finally { + await sql.close({ timeout: 0 }).catch(() => {}); + await new Promise(r => server.close(() => r())); + } + }); + + // Same overlap with a transaction that fails. close() must wait for the ROLLBACK, and + // the failure is the caller's to handle: bun:test fails this test if close()'s wait + // reports it as an unhandled rejection as well. + test("reserved.close({ timeout }) waits for a failing transaction without reporting its handled error", async () => { + const received: Received[] = []; + const { port, server } = await mockServer(received); + const sql = new SQL(options(port)); + try { + const reserved = await sql.reserve(); + const failing = reserved + .begin(async tx => { + await tx.unsafe("SELECT 'T1a'"); + throw new Error("t1-app-error"); + }) + .catch(err => err.message); + const closed = reserved.close({ timeout: 60 }); + expect(await failing).toBe("t1-app-error"); + await closed; + reserved.release(); + expect(received).toEqual([ + { conn: 0, sql: beginCommand }, + { conn: 0, sql: "SELECT 'T1a'" }, + { conn: 0, sql: "ROLLBACK" }, + ]); + } finally { + await sql.close({ timeout: 0 }).catch(() => {}); + await new Promise(r => server.close(() => r())); + } + }); + // reserved.close({ timeout }) waits, up to the timeout (in seconds), for the queries and // transactions that are pending on the reservation, then closes the connection. The // connection's close handler returns the pool slot. The tests below use a pool whose @@ -498,20 +557,19 @@ describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand, connec await pool.sql.close(); } - test("reserved.close({ timeout }) waits for a transaction started on the reservation", async () => { + // The two cases above end with release(). These two check the slot before it: close() alone + // closes the connection and returns the slot, so the release() after it is a no-op. + // `await using reserved` ends the same way. + test("reserved.close({ timeout }) returns the pool slot once a transaction on the reservation commits", async () => { await using pool = await closeTestPool(); const reserved = await pool.sql.reserve(); const t1 = reserved.begin(async tx => { await tx.unsafe("SELECT 'T1a'"); return "t1"; }); - // close() resolves as soon as t1 settles; without the transaction being tracked it - // would close the connection under t1 instead. const closed = reserved.close({ timeout: 60 }); expect(await t1).toBe("t1"); await closed; - // The slot already went back when close() closed the connection, so this is a no-op. - // `await using reserved` ends the same way. reserved.release(); await expectSlotReturned(pool); expect(pool.received).toEqual([ @@ -522,10 +580,7 @@ describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand, connec ]); }); - // Same overlap with a transaction that fails. close() must wait for the ROLLBACK, and - // the failure is the caller's to handle: bun:test fails this test if close()'s wait - // reports it as an unhandled rejection as well. - test("reserved.close({ timeout }) waits for a failing transaction without reporting its handled error", async () => { + test("reserved.close({ timeout }) returns the pool slot once a transaction on the reservation fails", async () => { await using pool = await closeTestPool(); const reserved = await pool.sql.reserve(); const failing = reserved @@ -547,6 +602,17 @@ describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand, connec ]); }); + // A graceful sql.close() waits for the pool's pending work. It hangs if the pool still + // counts the reservation. + test("a graceful sql.close() resolves after sql.reserve() is closed explicitly", async () => { + await using pool = await closeTestPool(); + const reserved = await pool.sql.reserve(); + await reserved.unsafe("SELECT 'inside'"); + await reserved.close(); + await expectSlotReturned(pool); + expect(pool.received).toEqual([{ conn: 0, sql: "SELECT 'inside'" }, ...afterTransaction(1)]); + }); + test("reserved.close({ timeout }) closes the connection once the pending query finishes", async () => { await using pool = await closeTestPool(); const reserved = await pool.sql.reserve(); @@ -716,6 +782,281 @@ describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand, connec }, ); + // transaction.close() ends a transaction in a rollback. With { timeout } it first waits, up + // to the timeout, for the queries and savepoints that are pending on the transaction. Once + // close() has accepted its arguments, the runner behind begin() and beginDistributed() does + // not commit, whether or not the callback awaited close(). The connection stays open and + // goes back to the pool, so the transaction that expectConnectionKept runs lands on conn 0. + type TransactionCallback = (tx: Bun.TransactionSQL) => Promise; + const transactionKinds = [ + { + kind: "begin()", + begin: (sql: SQL, callback: TransactionCallback) => sql.begin(callback), + started: beginCommand, + rolledBack: ["ROLLBACK"], + }, + { + kind: "beginDistributed()", + begin: (sql: SQL, callback: TransactionCallback) => sql.beginDistributed("dtx", callback), + started: distributed.begin, + rolledBack: distributed.rollback, + }, + ]; + const onConn0 = (...statements: string[]): Received[] => statements.map(sql => ({ conn: 0, sql })); + const settledCode = (promise: Promise) => + promise.then( + () => "committed", + error => error.code, + ); + + test.each(transactionKinds)( + "transaction.close({ timeout }) rolls $kind back once the pending query finishes", + async ({ begin, started, rolledBack }) => { + await using pool = await closeTestPool(); + let late: unknown; + const outcome = await settledCode( + begin(pool.sql, async tx => { + const inFlight = tx.unsafe("SELECT 'in flight'").execute(); + await tx.close({ timeout: 60 }); + await inFlight; + // The closed transaction takes no more statements, and a second close() is a no-op. + late = await tx`SELECT 'late'`.then( + () => "sent", + error => error.code, + ); + await tx.close(); + }), + ); + expect({ outcome, late }).toEqual({ outcome: connectionClosedCode, late: connectionClosedCode }); + await expectConnectionKept(pool); + expect(pool.received).toEqual([...onConn0(started, "SELECT 'in flight'", ...rolledBack), ...afterTransaction(0)]); + }, + ); + + // The callback returns while close({ timeout }) still waits for the query. The runner rolls + // back at once, and close() resolves when its wait ends. + test.each(transactionKinds)( + "transaction.close({ timeout }) that the callback does not await rolls $kind back", + async ({ begin, started, rolledBack }) => { + await using pool = await closeTestPool(); + let closed: Promise | undefined; + const outcome = await settledCode( + begin(pool.sql, async tx => { + tx.unsafe("SELECT 'pending'").execute(); + closed = tx.close({ timeout: 60 }); + }), + ); + expect(outcome).toBe(connectionClosedCode); + await closed; + await expectConnectionKept(pool); + expect(pool.received).toEqual([...onConn0(started, "SELECT 'pending'", ...rolledBack), ...afterTransaction(0)]); + }, + ); + + test("transaction.close() that the callback does not await rolls back, and nothing commits after it", async () => { + await using pool = await closeTestPool(); + let closed: Promise | undefined; + const outcome = await settledCode( + pool.sql.begin(async tx => { + await tx.unsafe("SELECT 'T1a'"); + closed = tx.close(); + }), + ); + expect(outcome).toBe(connectionClosedCode); + await closed; + await expectConnectionKept(pool); + expect(pool.received).toEqual([...onConn0(beginCommand, "SELECT 'T1a'", "ROLLBACK"), ...afterTransaction(0)]); + }); + + // A second close() resolves at once. The first one still waits when the callback returns. + test("a second transaction.close() behind a waiting close({ timeout }) does not let the transaction commit", async () => { + await using pool = await closeTestPool(); + let first: Promise | undefined; + const outcome = await settledCode( + pool.sql.begin(async tx => { + tx.unsafe("SELECT 'pending'").execute(); + first = tx.close({ timeout: 60 }); + await tx.close(); + }), + ); + expect(outcome).toBe(connectionClosedCode); + await first; + await expectConnectionKept(pool); + expect(pool.received).toEqual([...onConn0(beginCommand, "SELECT 'pending'", "ROLLBACK"), ...afterTransaction(0)]); + }); + + // The callback's own error reaches begin(), and the runner and close() share one rollback. + test("a callback that throws while transaction.close({ timeout }) waits keeps its error", async () => { + await using pool = await closeTestPool(); + let closed: Promise | undefined; + const outcome = await pool.sql + .begin(async tx => { + tx.unsafe("SELECT 'pending'").execute(); + closed = tx.close({ timeout: 60 }); + throw new Error("callback error"); + }) + .then( + () => "committed", + error => error.message, + ); + expect(outcome).toBe("callback error"); + await closed; + await expectConnectionKept(pool); + expect(pool.received).toEqual([...onConn0(beginCommand, "SELECT 'pending'", "ROLLBACK"), ...afterTransaction(0)]); + }); + + // close() has its rollback in flight when the failing query rejects the callback. The runner + // joins that rollback and sends no second one. A second XA END is an error on a real server. + test.each(transactionKinds)( + "transaction.close() awaited together with a failing query sends one rollback in $kind", + async ({ begin, started, rolledBack }) => { + await using pool = await closeTestPool(); + const outcome = await begin(pool.sql, async tx => { + const failing = tx.unsafe("SELECT 'FAIL'").execute(); + await Promise.all([tx.close(), failing]); + }).then( + () => "committed", + error => error.message, + ); + expect(outcome).toBe("mock failure"); + await expectConnectionKept(pool); + expect(pool.received).toEqual([...onConn0(started, "SELECT 'FAIL'", ...rolledBack), ...afterTransaction(0)]); + }, + ); + + // close() throws before it accepts its arguments, so this transaction is not closing. + test("transaction.close({ timeout: -1 }) throws and leaves the transaction to commit", async () => { + await using pool = await closeTestPool(); + const outcome = await pool.sql.begin(async tx => { + await tx.unsafe("SELECT 'T1a'"); + return await tx.close({ timeout: -1 }).then( + () => "closed", + error => error.code, + ); + }); + expect(outcome).toBe("ERR_INVALID_ARG_VALUE"); + await expectConnectionKept(pool); + expect(pool.received).toEqual([...onConn0(beginCommand, "SELECT 'T1a'", "COMMIT"), ...afterTransaction(0)]); + }); + + // A savepoint whose callback parks until finish() is called. + function parkedSavepoint(tx: Bun.TransactionSQL) { + const entered = Promise.withResolvers(); + const gate = Promise.withResolvers(); + const promise = tx.savepoint(async () => { + entered.resolve(); + await gate.promise; + return "saved"; + }); + return { promise, entered: entered.promise, finish: gate.resolve }; + } + + test("transaction.close({ timeout }) waits for a pending savepoint, its RELEASE included", async () => { + await using pool = await closeTestPool(); + let saved: unknown; + const outcome = await settledCode( + pool.sql.begin(async tx => { + const savepoint = parkedSavepoint(tx); + await savepoint.entered; + const closed = tx.close({ timeout: 60 }); + savepoint.finish(); + saved = await savepoint.promise; + await closed; + }), + ); + expect({ outcome, saved }).toEqual({ outcome: connectionClosedCode, saved: "saved" }); + await expectConnectionKept(pool); + expect(pool.received).toEqual([ + ...onConn0(beginCommand, "SAVEPOINT s0", "RELEASE SAVEPOINT s0", "ROLLBACK"), + ...afterTransaction(0), + ]); + }); + + // The savepoint stays open, so only the timer can end the wait. The rollback closes the + // transaction, and the savepoint's RELEASE is refused after it. + test("transaction.close({ timeout }) rolls back when the timeout ends the wait", async () => { + await using pool = await closeTestPool(); + let savepointCode: unknown; + const outcome = await settledCode( + pool.sql.begin(async tx => { + const savepoint = parkedSavepoint(tx); + const released = savepoint.promise.then( + () => null, + error => error.code, + ); + await savepoint.entered; + await tx.close({ timeout: 0.05 }); + savepoint.finish(); + savepointCode = await released; + }), + ); + expect({ outcome, savepointCode }).toEqual({ outcome: connectionClosedCode, savepointCode: connectionClosedCode }); + await expectConnectionKept(pool); + expect(pool.received).toEqual([...onConn0(beginCommand, "SAVEPOINT s0", "ROLLBACK"), ...afterTransaction(0)]); + }); + + // bun:test also fails this test if close()'s wait reports the handled failure as unhandled. + test("transaction.close({ timeout }) rolls back after a pending query fails, without reporting its handled error", async () => { + await using pool = await closeTestPool(); + let failure: unknown; + const outcome = await settledCode( + pool.sql.begin(async tx => { + const failed = tx.unsafe("SELECT 'FAIL'").then( + () => null, + error => error.message, + ); + await tx.close({ timeout: 60 }); + failure = await failed; + }), + ); + expect({ outcome, failure }).toEqual({ outcome: connectionClosedCode, failure: "mock failure" }); + await expectConnectionKept(pool); + expect(pool.received).toEqual([...onConn0(beginCommand, "SELECT 'FAIL'", "ROLLBACK"), ...afterTransaction(0)]); + }); + + // The disconnect ends the transaction and rejects begin() while the callback still runs, so + // close() has nothing left to do but resolve. bun:test also fails this test if the wait + // reports the query the server took down as unhandled. + test("transaction.close({ timeout }) resolves when the server drops the connection while it waits", async () => { + await using pool = await closeTestPool(); + const dropped = Promise.withResolvers(); + const outcome = await settledCode( + pool.sql.begin(async tx => { + const query = tx.unsafe("SELECT 'KILL'").then( + () => null, + error => error.code, + ); + await tx.close({ timeout: 60 }).then(() => dropped.resolve(query), dropped.reject); + }), + ); + expect({ outcome, dropped: await dropped.promise }).toEqual({ + outcome: connectionClosedCode, + dropped: connectionClosedCode, + }); + await expectSlotReturned(pool); + expect(pool.received).toEqual([...onConn0(beginCommand, "SELECT 'KILL'"), ...afterTransaction(1)]); + }); + + test("transaction.close({ timeout }) inside reserved.begin() rolls back and keeps the reservation", async () => { + await using pool = await closeTestPool(); + const reserved = await pool.sql.reserve(); + const outcome = await settledCode( + reserved.begin(async tx => { + const inFlight = tx.unsafe("SELECT 'in flight'").execute(); + await tx.close({ timeout: 60 }); + await inFlight; + }), + ); + expect(outcome).toBe(connectionClosedCode); + await reserved.unsafe("SELECT 'still reserved'"); + reserved.release(); + await expectConnectionKept(pool); + expect(pool.received).toEqual([ + ...onConn0(beginCommand, "SELECT 'in flight'", "ROLLBACK", "SELECT 'still reserved'"), + ...afterTransaction(0), + ]); + }); + // Runs in a child process: bun:test would turn any unhandled rejection into a test // failure, and the second half of this contract is that one rejection IS reported. test("a rejected reserved begin() is reported as unhandled only when the caller ignores it", async () => { diff --git a/test/js/sql/sql-transaction-close.test.ts b/test/js/sql/sql-transaction-close.test.ts deleted file mode 100644 index 8c8c90663661..000000000000 --- a/test/js/sql/sql-transaction-close.test.ts +++ /dev/null @@ -1,479 +0,0 @@ -// https://github.com/oven-sh/bun/issues/32148 -// transaction.close({ timeout }) resolved without issuing ROLLBACK and -// without marking the transaction closed when the pending queries settled -// before the timeout, so the COMMIT issued after the begin() callback -// returned persisted writes the user explicitly closed. The internal -// Promise.all bookkeeping was also dropped, turning a rejecting pending -// query into an unhandledRejection even when the user handled it. -// -// Uses minimal mock postgres/mysql servers so it can run without Docker. - -import { SQL } from "bun"; -import { expect, test } from "bun:test"; -import { once } from "events"; -import net from "net"; - -// a handler can claim a query by returning true; it is then responsible for -// answering on the socket. Everything else gets a canned response. -type QueryHandler = (query: string, socket: net.Socket) => boolean | void; - -// --------------------------------------------------------------------------- -// postgres mock -// --------------------------------------------------------------------------- - -function pkt(type: string, body: Buffer): Buffer { - const header = Buffer.alloc(5); - header.write(type, 0); - header.writeInt32BE(body.length + 4, 1); - return Buffer.concat([header, body]); -} - -function int16(n: number): Buffer { - const b = Buffer.alloc(2); - b.writeInt16BE(n, 0); - return b; -} - -function int32(n: number): Buffer { - const b = Buffer.alloc(4); - b.writeInt32BE(n, 0); - return b; -} - -function cstr(s: string): Buffer { - return Buffer.concat([Buffer.from(s), Buffer.from([0])]); -} - -function rowDescription(names: string[]): Buffer { - const fields = Buffer.concat( - names.map(name => - Buffer.concat([ - cstr(name), // column name - int32(0), // table oid - int16(0), // column attr number - int32(25), // type oid: text - int16(-1), // type size - int32(-1), // type modifier - int16(0), // format: text - ]), - ), - ); - return pkt("T", Buffer.concat([int16(names.length), fields])); -} - -function dataRow(values: string[]): Buffer { - const cols = Buffer.concat( - values.map(v => { - const bytes = Buffer.from(v); - return Buffer.concat([int32(bytes.length), bytes]); - }), - ); - return pkt("D", Buffer.concat([int16(values.length), cols])); -} - -const authenticationOk = pkt("R", int32(0)); -const readyForQuery = pkt("Z", Buffer.from("I")); - -function selectResponse(value: string): Buffer { - return Buffer.concat([rowDescription(["x"]), dataRow([value]), pkt("C", cstr("SELECT 1")), readyForQuery]); -} - -function commandResponse(query: string): Buffer { - // the command tag is the statement's first word: BEGIN, ROLLBACK, ... - return Buffer.concat([pkt("C", cstr(query.split(" ")[0].toUpperCase())), readyForQuery]); -} - -function errorResponse(message: string): Buffer { - return pkt( - "E", - Buffer.concat([ - Buffer.from("S"), - cstr("ERROR"), - Buffer.from("C"), - cstr("XX000"), - Buffer.from("M"), - cstr(message), - Buffer.from([0]), - ]), - ); -} - -async function startPostgresServer(onQuery: QueryHandler = () => {}) { - const sockets = new Set(); - const commands: string[] = []; - const server = net.createServer(socket => { - sockets.add(socket); - socket.on("close", () => sockets.delete(socket)); - socket.on("error", () => {}); - let startup = true; - let buffered = Buffer.alloc(0); - socket.on("data", data => { - buffered = Buffer.concat([buffered, data]); - if (startup) { - // the startup message has no type byte: int32 length + payload - if (buffered.length < 4) return; - const len = buffered.readInt32BE(0); - if (buffered.length < len) return; - buffered = buffered.subarray(len); - startup = false; - socket.write(Buffer.concat([authenticationOk, readyForQuery])); - } - // regular messages: type byte + int32 length (which includes itself) - while (buffered.length >= 5) { - const len = buffered.readInt32BE(1); - if (buffered.length < 1 + len) break; - const type = buffered[0]; - const payload = buffered.subarray(5, 1 + len); - buffered = buffered.subarray(1 + len); - if (type === 0x51 /* 'Q' simple query */) { - const query = payload.toString("utf8").replace(/\0$/, ""); - commands.push(query); - if (!onQuery(query, socket)) { - socket.write(/^select/i.test(query) ? selectResponse("1") : commandResponse(query)); - } - } - // anything else ('X' Terminate, ...) needs no reply - } - }); - }); - server.listen(0, "127.0.0.1"); - await once(server, "listening"); - const { port } = server.address() as net.AddressInfo; - return { - port, - commands, - async [Symbol.asyncDispose]() { - for (const socket of sockets) socket.destroy(); - await new Promise(r => server.close(() => r())); - }, - }; -} - -// --------------------------------------------------------------------------- -// mysql mock -// --------------------------------------------------------------------------- - -function u16le(n: number): Buffer { - return Buffer.from([n & 0xff, (n >> 8) & 0xff]); -} -function u24le(n: number): Buffer { - return Buffer.from([n & 0xff, (n >> 8) & 0xff, (n >> 16) & 0xff]); -} -function u32le(n: number): Buffer { - return Buffer.from([n & 0xff, (n >> 8) & 0xff, (n >> 16) & 0xff, (n >>> 24) & 0xff]); -} -function packet(seq: number, payload: Buffer): Buffer { - return Buffer.concat([u24le(payload.length), Buffer.from([seq]), payload]); -} -function lenencStr(s: string): Buffer { - const buf = Buffer.from(s, "utf-8"); - return Buffer.concat([Buffer.from([buf.length]), buf]); -} - -const CLIENT_PROTOCOL_41 = 1 << 9; -const CLIENT_SECURE_CONNECTION = 1 << 15; -const CLIENT_PLUGIN_AUTH = 1 << 19; -const CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA = 1 << 21; -const CLIENT_DEPRECATE_EOF = 1 << 24; -const SERVER_CAPS = - CLIENT_PROTOCOL_41 | - CLIENT_SECURE_CONNECTION | - CLIENT_PLUGIN_AUTH | - CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA | - CLIENT_DEPRECATE_EOF; - -function handshakeV10(): Buffer { - const authData1 = Buffer.alloc(8, 0x61); - const authData2 = Buffer.alloc(13, 0x62); - authData2[12] = 0; - return packet( - 0, - Buffer.concat([ - Buffer.from([10]), // protocol version - Buffer.from("mock-5.7.0\0"), // server version - u32le(1), // connection id - authData1, // auth-plugin-data-part-1 - Buffer.from([0]), // filler - u16le(SERVER_CAPS & 0xffff), // capability flags lower - Buffer.from([0x2d]), // character set (utf8mb4_general_ci) - u16le(0x0002), // status flags (SERVER_STATUS_AUTOCOMMIT) - u16le((SERVER_CAPS >>> 16) & 0xffff), // capability flags upper - Buffer.from([21]), // length of auth-plugin-data - Buffer.alloc(10, 0), // reserved - authData2, // auth-plugin-data-part-2 - Buffer.from("mysql_native_password\0"), - ]), - ); -} - -function okPacket(seq: number, header = 0x00): Buffer { - return packet(seq, Buffer.from([header, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00])); -} - -// VARCHAR column named "x" (utf8mb4, no flags) -function varcharColumn(): Buffer { - return Buffer.concat([ - lenencStr("def"), - lenencStr(""), - lenencStr("t"), - lenencStr("t"), - lenencStr("x"), - lenencStr("x"), - Buffer.from([0x0c]), // length of fixed fields - u16le(0x2d), // charset utf8mb4_general_ci - u32le(1024), // column length - Buffer.from([0xfd]), // type: VAR_STRING - u16le(0), // flags - Buffer.from([0]), // decimals - Buffer.from([0, 0]), // filler - ]); -} - -// Text-protocol result set for a single-row `select ... as x` (COM_QUERY) -function textResultSet(startSeq: number, value: string): Buffer { - let seq = startSeq; - return Buffer.concat([ - packet(seq++, Buffer.from([1])), // column count - packet(seq++, varcharColumn()), - packet(seq++, lenencStr(value)), // row - okPacket(seq++, 0xfe), // DEPRECATE_EOF terminator - ]); -} - -async function startMysqlServer(onQuery: QueryHandler = () => {}) { - const sockets = new Set(); - const commands: string[] = []; - const server = net.createServer(socket => { - sockets.add(socket); - socket.on("close", () => sockets.delete(socket)); - socket.on("error", () => {}); - let buffered = Buffer.alloc(0); - let authed = false; - socket.write(handshakeV10()); - socket.on("data", chunk => { - buffered = Buffer.concat([buffered, chunk]); - while (buffered.length >= 4) { - const len = buffered[0] | (buffered[1] << 8) | (buffered[2] << 16); - if (buffered.length < 4 + len) break; - const seq = buffered[3]; - const payload = buffered.subarray(4, 4 + len); - buffered = buffered.subarray(4 + len); - if (!authed) { - // HandshakeResponse41 from client → accept unconditionally - authed = true; - socket.write(okPacket(seq + 1)); - continue; - } - const cmd = payload[0]; - if (cmd === 0x03 /* COM_QUERY */) { - const query = payload.subarray(1).toString("utf8"); - commands.push(query); - if (!onQuery(query, socket)) { - socket.write(/^select/i.test(query) ? textResultSet(seq + 1, "1") : okPacket(seq + 1)); - } - } else if (cmd === 0x19 /* COM_STMT_CLOSE */) { - // no response expected - } else { - // COM_QUIT or anything else → close - socket.end(); - } - } - }); - }); - server.listen(0, "127.0.0.1"); - await once(server, "listening"); - const { port } = server.address() as net.AddressInfo; - return { - port, - commands, - async [Symbol.asyncDispose]() { - for (const socket of sockets) socket.destroy(); - await new Promise(r => server.close(() => r())); - }, - }; -} - -// --------------------------------------------------------------------------- -// tests -// --------------------------------------------------------------------------- - -test("postgres: transaction.close({ timeout }) rolls back when pending queries drain before the timeout", async () => { - const slowQuery = Promise.withResolvers(); - await using pg = await startPostgresServer((query, socket) => { - if (query.includes("hold")) { - slowQuery.resolve(socket); - return true; // response withheld until the test releases it - } - }); - await using sql = new SQL({ url: `postgres://u@127.0.0.1:${pg.port}/db`, max: 1, connectionTimeout: 5 }); - - let pendingResult: unknown; - let queryAfterClose: unknown; - const begin = sql.begin(async tx => { - const pending = tx`select 'hold' as x`.simple().execute(); - const socket = await slowQuery.promise; // the query is now in flight - const closed = tx.close({ timeout: 5 }); - // let the pending query finish long before the 5s timeout - socket.write(selectResponse("hold")); - pendingResult = await pending; - await closed; - // the transaction no longer accepts queries (a closed transaction hands - // back a plain rejected promise, not a Query) - queryAfterClose = await (tx`select 1 as x` as Promise).then( - () => "resolved", - (err: any) => err.code, - ); - // close is idempotent - await tx.close(); - }); - - // close() rolled back, so the COMMIT attempt after the callback must fail - await expect(begin).rejects.toMatchObject({ code: "ERR_POSTGRES_CONNECTION_CLOSED" }); - expect(pendingResult).toEqual([{ x: "hold" }]); - expect(queryAfterClose).toBe("ERR_POSTGRES_CONNECTION_CLOSED"); - // the rejected post-close query never reached the wire - expect(pg.commands).not.toContain("select 1 as x"); - expect(pg.commands).toContain("ROLLBACK"); - expect(pg.commands).not.toContain("COMMIT"); -}); - -test("postgres: transaction.close({ timeout }) does not leak an unhandledRejection when a pending query fails", async () => { - const unhandled: unknown[] = []; - const onUnhandled = (err: unknown) => unhandled.push(err); - process.on("unhandledRejection", onUnhandled); - try { - const failingQuery = Promise.withResolvers(); - await using pg = await startPostgresServer((query, socket) => { - if (query.includes("boom")) { - failingQuery.resolve(socket); - return true; - } - }); - await using sql = new SQL({ url: `postgres://u@127.0.0.1:${pg.port}/db`, max: 1, connectionTimeout: 5 }); - - let handledAs: unknown; - const begin = sql.begin(async tx => { - const failing = tx`select 'boom' as x`.simple().execute(); - const handled = failing.then( - () => "resolved", - () => "handled", - ); - const socket = await failingQuery.promise; - const closed = tx.close({ timeout: 5 }); - // fail the pending query during the grace period - socket.write(Buffer.concat([errorResponse("boom"), readyForQuery])); - handledAs = await handled; - await closed; - }); - - await expect(begin).rejects.toMatchObject({ code: "ERR_POSTGRES_CONNECTION_CLOSED" }); - expect(handledAs).toBe("handled"); - expect(pg.commands).toContain("ROLLBACK"); - expect(pg.commands).not.toContain("COMMIT"); - // tear the pool down now, while the unhandledRejection listener is still - // attached (the await using dispose after the finally would be too late); - // close is idempotent so the second dispose at scope exit is a no-op - await sql.close(); - - // the query rejection was handled by the user; close()'s internal - // bookkeeping must not surface it again (one macrotask turn so a report - // queued by the teardown above still lands while the listener is attached) - await Bun.sleep(0); - expect(unhandled).toEqual([]); - } finally { - process.off("unhandledRejection", onUnhandled); - } -}); - -test("postgres: transaction.close({ timeout }) waits for pending savepoints and still rolls back", async () => { - const slowQuery = Promise.withResolvers(); - await using pg = await startPostgresServer((query, socket) => { - if (query.includes("hold")) { - slowQuery.resolve(socket); - return true; - } - }); - await using sql = new SQL({ url: `postgres://u@127.0.0.1:${pg.port}/db`, max: 1, connectionTimeout: 5 }); - - let savepointResult: unknown; - const begin = sql.begin(async tx => { - const savepoint = tx.savepoint(async sp => { - return await sp`select 'hold' as x`.simple().execute(); - }); - const socket = await slowQuery.promise; - const closed = tx.close({ timeout: 5 }); - // the savepoint finishes (including RELEASE SAVEPOINT) before the timeout - socket.write(selectResponse("hold")); - savepointResult = await savepoint; - await closed; - }); - - await expect(begin).rejects.toMatchObject({ code: "ERR_POSTGRES_CONNECTION_CLOSED" }); - expect(savepointResult).toEqual([{ x: "hold" }]); - expect(pg.commands).toContain("SAVEPOINT s0"); - expect(pg.commands).toContain("RELEASE SAVEPOINT s0"); - expect(pg.commands).toContain("ROLLBACK"); - expect(pg.commands).not.toContain("COMMIT"); -}); - -test("postgres: transaction.close({ timeout }) still rolls back when the timeout fires first", async () => { - const slowQuery = Promise.withResolvers(); - await using pg = await startPostgresServer((query, socket) => { - if (query.includes("hold")) { - slowQuery.resolve(socket); - return true; - } - }); - await using sql = new SQL({ url: `postgres://u@127.0.0.1:${pg.port}/db`, max: 1, connectionTimeout: 5 }); - - const begin = sql.begin(async tx => { - const pending = tx`select 'hold' as x`.simple().execute(); - const socket = await slowQuery.promise; - // fractional seconds are accepted by the timeout validation; a small - // value keeps the test fast, and nothing races the timer here: the held - // query is only answered after the timer's cancellation is observed - const closed = tx.close({ timeout: 0.1 }); - // the timer fired once close() cancelled the pending queries - while (!pending.cancelled) { - await Bun.sleep(5); - } - // ROLLBACK can only go out on the wire after the in-flight query - // completes (simple queries never pipeline), so it cannot have reached - // the server yet; answer the held query now (settlement of a cancelled - // in-flight query is not the subject here) - expect(pg.commands).not.toContain("ROLLBACK"); - socket.write(selectResponse("hold")); - await (pending as Promise).catch(() => {}); - await closed; - }); - - await expect(begin).rejects.toMatchObject({ code: "ERR_POSTGRES_CONNECTION_CLOSED" }); - expect(pg.commands).toContain("ROLLBACK"); - expect(pg.commands).not.toContain("COMMIT"); -}); - -test("mysql: transaction.close({ timeout }) rolls back when pending queries drain before the timeout", async () => { - const slowQuery = Promise.withResolvers(); - await using my = await startMysqlServer((query, socket) => { - if (query.includes("hold")) { - slowQuery.resolve(socket); - return true; - } - }); - await using sql = new SQL({ url: `mysql://root@127.0.0.1:${my.port}/db`, max: 1, connectionTimeout: 5 }); - - let pendingResult: unknown; - const begin = sql.begin(async tx => { - const pending = tx`select 'hold' as x`.simple().execute(); - const socket = await slowQuery.promise; - const closed = tx.close({ timeout: 5 }); - socket.write(textResultSet(1, "hold")); - pendingResult = await pending; - await closed; - }); - - await expect(begin).rejects.toMatchObject({ code: "ERR_MYSQL_CONNECTION_CLOSED" }); - expect(pendingResult).toEqual([{ x: "hold" }]); - expect(my.commands).toContain("ROLLBACK"); - expect(my.commands).not.toContain("COMMIT"); -}); diff --git a/test/js/sql/sqlite-sql.test.ts b/test/js/sql/sqlite-sql.test.ts index 6f64615e05f7..3a295a26d7e3 100644 --- a/test/js/sql/sqlite-sql.test.ts +++ b/test/js/sql/sqlite-sql.test.ts @@ -1,7 +1,7 @@ import { randomUUIDv7, SQL } from "bun"; import { Database } from "bun:sqlite"; import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, mock, test } from "bun:test"; -import { bunEnv, bunExe, isDebug, tempDir } from "harness"; +import { isDebug, tempDir } from "harness"; import { existsSync } from "node:fs"; import { rm, stat } from "node:fs/promises"; import { join } from "node:path"; @@ -997,23 +997,6 @@ describe("Query Execution", () => { expect(await db`SELECT count(*) AS n FROM cancel_in_transaction`).toEqual([{ n: 0 }]); }); - test("a query cancelled before it runs leaves its transaction scope when it rejects", async () => { - await using db = new SQL(":memory:"); - await db`CREATE TABLE cancel_then_close (id INTEGER)`; - const transaction = db.begin(async tx => { - await tx`INSERT INTO cancel_then_close VALUES (1)`; - const query = tx`SELECT 1 AS x`; - query.cancel(); - await settle(query); - // close({ timeout }) waits on every query that is still in the scope. A rejected - // query that stays there ends that wait at once, and close() then skips the ROLLBACK. - await tx.close({ timeout: 1 }); - }); - - expect(await settle(transaction)).toEqual({ code: "ERR_SQLITE_CONNECTION_CLOSED", message: "Connection closed" }); - expect(await db`SELECT count(*) AS n FROM cancel_then_close`).toEqual([{ n: 0 }]); - }); - test("a query that tx.close() cancels before it runs rejects when awaited", async () => { await using db = new SQL(":memory:"); let lazy: Promise | undefined; @@ -1365,57 +1348,118 @@ describe("Transactions", () => { const accounts = await sql`SELECT * FROM accounts WHERE id = 1`; expect(accounts[0].balance).toBe(1002); }); + + // The callback ends the transaction by hand, so the ROLLBACK of close() fails. The + // transaction is closing all the same: begin() sends ROLLBACK again and never COMMIT. + test("a transaction whose close() failed is rolled back again, not committed", async () => { + let closeError: unknown; + const outcome = await sql + .begin(async tx => { + await tx`UPDATE accounts SET balance = 0 WHERE id = 1`; + await tx.unsafe("ROLLBACK"); + closeError = await tx.close().then( + () => null, + error => error.message, + ); + }) + .then( + () => "committed", + error => error.message, + ); + + expect({ closeError, outcome }).toEqual({ + closeError: "cannot rollback - no transaction is active", + outcome: "cannot rollback - no transaction is active", + }); + expect(await sql`SELECT balance FROM accounts WHERE id = 1`).toEqual([{ balance: 1000 }]); + }); }); -// Inside sql.begin() a row is inserted, a second query is made, and the callback closes the -// transaction with a timeout. close() waits for the second query, for at most the timeout, -// and then rolls back. The timeout is far longer than the test, so only the second query can -// end the wait. +// Inside sql.begin() a row is inserted and the callback closes the transaction. close() with +// a timeout first waits, for at most the timeout, for the queries that are pending on the +// transaction. The timeout here is far longer than the test, so only those queries can end +// the wait. However the callback calls close(), the row must not be committed. // -// Each shape runs in its own process: a rejection that close() leaves unhandled then shows on -// stderr and in the exit code. The row count comes from a second connection, which sees -// committed rows only. -describe("tx.close({ timeout }) rolls the transaction back", () => { - const fixture = ` - const [file, shape] = process.argv.slice(1); - const sql = new Bun.SQL("sqlite://" + file); - await sql\`CREATE TABLE t (v INTEGER)\`; - const begin = await sql - .begin(async tx => { - await tx\`INSERT INTO t VALUES (1)\`; - if (shape !== "no second query") { - const query = tx\`SELECT 1 AS x\`; - if (shape !== "a second query that nothing awaits") query.cancel(); - if (shape === "a second query that is cancelled before it runs, then awaited") await query.catch(() => {}); - } +// The row count comes from a second connection, which sees committed rows only. bun:test +// also fails these tests if close() leaves a rejection unhandled. +describe("tx.close() rolls the transaction back", () => { + const connectionClosed = "ERR_SQLITE_CONNECTION_CLOSED"; + const orders: Array<[string, (tx: Bun.TransactionSQL) => Promise, string]> = [ + [ + "close({ timeout }) with no second query", + async tx => { + await tx.close({ timeout: 60 }); + }, + connectionClosed, + ], + [ + "close({ timeout }) after a second query that nothing awaits", + async tx => { + tx`SELECT 1 AS x`; + await tx.close({ timeout: 60 }); + }, + connectionClosed, + ], + [ + "close({ timeout }) after a second query that is cancelled before it runs", + async tx => { + tx`SELECT 1 AS x`.cancel(); await tx.close({ timeout: 60 }); + }, + connectionClosed, + ], + [ + "close({ timeout }) after a cancelled second query that the callback awaited", + async tx => { + await tx`SELECT 1 AS x`.cancel().catch(() => {}); + await tx.close({ timeout: 60 }); + }, + connectionClosed, + ], + [ + "close({ timeout }) that the callback does not await", + async tx => { + tx`SELECT 1 AS x`; + tx.close({ timeout: 60 }); + }, + connectionClosed, + ], + [ + "close() that the callback does not await", + async tx => { + tx.close(); + }, + connectionClosed, + ], + [ + "close({ timeout }) that still waits when the callback throws", + async tx => { + tx`SELECT 1 AS x`; + tx.close({ timeout: 60 }); + throw new Error("callback error"); + }, + "callback error", + ], + ]; + + test.each(orders)("%s", async (_, close, expected) => { + await using dir = tempDir("sqlite-tx-close", {}); + const url = `sqlite://${join(String(dir), "db.sqlite")}`; + await using db = new SQL(url); + await db`CREATE TABLE t (v INTEGER)`; + const begin = await db + .begin(async tx => { + await tx`INSERT INTO t VALUES (1)`; + await close(tx); }) - .then(() => "resolved", error => error.code); - const [{ rows }] = await new Bun.SQL("sqlite://" + file)\`SELECT count(*) AS rows FROM t\`; - console.log(JSON.stringify({ begin, rows })); - `; - - test.concurrent.each([ - "no second query", - "a second query that nothing awaits", - "a second query that is cancelled before it runs", - "a second query that is cancelled before it runs, then awaited", - ])("with %s", async shape => { - using dir = tempDir("sqlite-tx-close-timeout", {}); - await using proc = Bun.spawn({ - cmd: [bunExe(), "-e", fixture, join(String(dir), "db.sqlite"), shape], - env: bunEnv, - stdout: "pipe", - stderr: "pipe", - }); - const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + .then( + () => "committed", + error => error.code ?? error.message, + ); - expect({ stdout: stdout.trim(), stderr, exitCode, signalCode: proc.signalCode }).toEqual({ - stdout: JSON.stringify({ begin: "ERR_SQLITE_CONNECTION_CLOSED", rows: 0 }), - stderr: "", - exitCode: 0, - signalCode: null, - }); + await using reader = new SQL(url); + const [{ rows }] = await reader`SELECT count(*) AS rows FROM t`; + expect({ begin, rows }).toEqual({ begin: expected, rows: 0 }); }); }); From 3f666a67407e08da6e07629bddc109d9a59a6f8a Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Wed, 7 Oct 2026 21:42:59 +0000 Subject: [PATCH 07/13] sql: close() clears the shared rollback when its own ROLLBACK fails rollbackTransaction() no longer clears the memo. A close() that still waits when the runner's ROLLBACK fails sees the memo and returns. Before, it sent a second ROLLBACK in the tick between the failure and the end of the runner, and its rejection was unhandled. Tests: the mocks can fail one statement once, for a close() whose ROLLBACK fails and the runner's second ROLLBACK. --- src/js/bun/sql.ts | 31 ++++++------ .../sql-pool-transaction-isolation.test.ts | 48 ++++++++++++++++--- test/js/sql/sqlite-sql.test.ts | 19 ++++++++ 3 files changed, 77 insertions(+), 21 deletions(-) diff --git a/src/js/bun/sql.ts b/src/js/bun/sql.ts index 6b12c58b2411..4c66d4e12b04 100644 --- a/src/js/bun/sql.ts +++ b/src/js/bun/sql.ts @@ -279,6 +279,7 @@ const SQL = function SQL( /// Ends a closing transaction: cancels what is pending, then sends `before` (XA END) and `rollback`. /// Callers memoize it in state.rollback, so the wire sees one ROLLBACK. + /// Declared here and not in onTransactionConnected: one function per SQL instance, not one per transaction. async function rollbackTransaction( state: TransactionState, pooledConnection: PooledConnection, @@ -288,16 +289,10 @@ const SQL = function SQL( for (const query of state.queries) { query.cancel(); } - try { - if (before) { - await unsafeQueryFromTransaction(before, [], pooledConnection, state.queries); - } - await unsafeQueryFromTransaction(rollback, [], pooledConnection, state.queries); - } catch (err) { - // the next caller sends it again - state.rollback = null; - throw err; + if (before) { + await unsafeQueryFromTransaction(before, [], pooledConnection, state.queries); } + await unsafeQueryFromTransaction(rollback, [], pooledConnection, state.queries); state.connectionState |= ReservedConnectionState.closed; } @@ -813,12 +808,18 @@ const SQL = function SQL( } } state.connectionState |= ReservedConnectionState.closing; - return (state.rollback ??= rollbackTransaction( - state, - pooledConnection, - BEFORE_COMMIT_OR_ROLLBACK_COMMAND, - ROLLBACK_COMMAND, - )); + try { + await (state.rollback ??= rollbackTransaction( + state, + pooledConnection, + BEFORE_COMMIT_OR_ROLLBACK_COMMAND, + ROLLBACK_COMMAND, + )); + } catch (err) { + // close() reports the failure, and the runner sends ROLLBACK again when the callback settles + state.rollback = null; + throw err; + } }; transaction_sql[Symbol.asyncDispose] = () => transaction_sql.close(); transaction_sql.options = sql.options; diff --git a/test/js/sql/sql-pool-transaction-isolation.test.ts b/test/js/sql/sql-pool-transaction-isolation.test.ts index 17ab6bc0b70d..f80a949e3aa0 100644 --- a/test/js/sql/sql-pool-transaction-isolation.test.ts +++ b/test/js/sql/sql-pool-transaction-isolation.test.ts @@ -26,8 +26,9 @@ import { type Received = { conn: number; sql: string }; // `onReceived` runs after each statement is recorded. `secure` makes the server accept the -// client's TLS request and speak the protocol over TLS from there on. -type MockOptions = { onReceived?: () => void; secure?: boolean }; +// client's TLS request and speak the protocol over TLS from there on. `failOnce` is a +// statement that the server answers with an error the first time it arrives. +type MockOptions = { onReceived?: () => void; secure?: boolean; failOnce?: string }; type MockServer = (received: Received[], options?: MockOptions) => Promise<{ port: number; server: net.Server }>; function secureSocket(rawSocket: net.Socket): net.Socket { @@ -37,11 +38,23 @@ function secureSocket(rawSocket: net.Socket): net.Socket { return socket; } +// Whether a mock answers this statement with an error. +function failureCheck(failOnce: string | undefined) { + return (sql: string) => { + if (sql === failOnce) { + failOnce = undefined; + return true; + } + return sql.includes("FAIL"); + }; +} + // Both mocks answer every statement at once, except for these markers in the query text: // KILL destroys the socket without answering, // FAIL answers with an error, // HOLD never answers. -const pgMockServer: MockServer = (received, { onReceived, secure } = {}) => { +const pgMockServer: MockServer = (received, { onReceived, secure, failOnce } = {}) => { + const fails = failureCheck(failOnce); let nextConn = 0; return listeningServer(rawSocket => { const connId = nextConn++; @@ -84,7 +97,7 @@ const pgMockServer: MockServer = (received, { onReceived, secure } = {}) => { return; } if (sql.includes("HOLD")) continue; - if (sql.includes("FAIL")) { + if (fails(sql)) { socket.write( Buffer.concat([pgErrorResponse({ S: "ERROR", C: "XX000", M: "mock failure" }), pgReadyForQuery()]), ); @@ -97,7 +110,8 @@ const pgMockServer: MockServer = (received, { onReceived, secure } = {}) => { }); }; -const mysqlMockServer: MockServer = (received, { onReceived, secure } = {}) => { +const mysqlMockServer: MockServer = (received, { onReceived, secure, failOnce } = {}) => { + const fails = failureCheck(failOnce); const COM_QUIT = 0x01; const COM_QUERY = 0x03; let nextConn = 0; @@ -144,7 +158,7 @@ const mysqlMockServer: MockServer = (received, { onReceived, secure } = {}) => { return; } if (sql.includes("HOLD")) return; - if (sql.includes("FAIL")) { + if (fails(sql)) { socket.write(mysqlErrPacket(1, 1105, "HY000", "mock failure")); return; } @@ -939,6 +953,28 @@ describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand, connec expect(pool.received).toEqual([...onConn0(beginCommand, "SELECT 'T1a'", "COMMIT"), ...afterTransaction(0)]); }); + // The server rejects the ROLLBACK of close(). close() reports that, and the transaction is + // still closing: when the callback returns, the runner sends ROLLBACK again and no COMMIT. + test("transaction.close() whose ROLLBACK fails leaves the rollback to the runner", async () => { + await using pool = await closeTestPool({ failOnce: "ROLLBACK" }); + let closeError: unknown; + const outcome = await settledCode( + pool.sql.begin(async tx => { + await tx.unsafe("SELECT 'T1a'"); + closeError = await tx.close().then( + () => null, + error => error.message, + ); + }), + ); + expect({ closeError, outcome }).toEqual({ closeError: "mock failure", outcome: connectionClosedCode }); + await expectConnectionKept(pool); + expect(pool.received).toEqual([ + ...onConn0(beginCommand, "SELECT 'T1a'", "ROLLBACK", "ROLLBACK"), + ...afterTransaction(0), + ]); + }); + // A savepoint whose callback parks until finish() is called. function parkedSavepoint(tx: Bun.TransactionSQL) { const entered = Promise.withResolvers(); diff --git a/test/js/sql/sqlite-sql.test.ts b/test/js/sql/sqlite-sql.test.ts index 3a295a26d7e3..3ccec16d8f7d 100644 --- a/test/js/sql/sqlite-sql.test.ts +++ b/test/js/sql/sqlite-sql.test.ts @@ -1373,6 +1373,25 @@ describe("Transactions", () => { }); expect(await sql`SELECT balance FROM accounts WHERE id = 1`).toEqual([{ balance: 1000 }]); }); + + // Here the callback returns while close({ timeout }) still waits, so begin() sends the + // ROLLBACK that fails and reports it. bun:test fails this test if close() reports the same + // failure a second time, as an unhandled rejection. + test("a ROLLBACK that fails for begin() is not reported again by a close({ timeout }) that still waits", async () => { + const outcome = await sql + .begin(async tx => { + await tx`UPDATE accounts SET balance = 0 WHERE id = 1`; + await tx.unsafe("ROLLBACK"); + tx`SELECT 1 AS x`; + tx.close({ timeout: 60 }); + }) + .then( + () => "committed", + error => error.message, + ); + + expect(outcome).toBe("cannot rollback - no transaction is active"); + }); }); // Inside sql.begin() a row is inserted and the callback closes the transaction. close() with From cafb14563887db0b04c3a38c12ef54d73f36e877 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Wed, 7 Oct 2026 22:41:44 +0000 Subject: [PATCH 08/13] test(sql): pin the execute() path of a query cancelled before it runs --- test/js/sql/sqlite-sql.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/js/sql/sqlite-sql.test.ts b/test/js/sql/sqlite-sql.test.ts index 3ccec16d8f7d..aa861427e294 100644 --- a/test/js/sql/sqlite-sql.test.ts +++ b/test/js/sql/sqlite-sql.test.ts @@ -952,6 +952,8 @@ describe("Query Execution", () => { const query = sql`INSERT INTO cancel_before_execute VALUES (1)`; query.cancel(); query.execute(); + // execute() settles the query in the call. The await below would start it as well. + expect(Bun.peek.status(query)).toBe("rejected"); expect(await settle(query)).toEqual({ code: "ERR_SQLITE_QUERY_CANCELLED", message: "Query cancelled" }); expect(await sql`SELECT count(*) AS n FROM cancel_before_execute`).toEqual([{ n: 0 }]); From 7c3bffda9826531f8a77e78c8f86a214739dac76 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 8 Oct 2026 00:25:58 +0000 Subject: [PATCH 09/13] sql: read the transaction state once in the runner's catch path, one-line comment on rollbackTransaction --- src/js/bun/sql.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/js/bun/sql.ts b/src/js/bun/sql.ts index 4c66d4e12b04..b7a61dfcc23e 100644 --- a/src/js/bun/sql.ts +++ b/src/js/bun/sql.ts @@ -277,9 +277,7 @@ const SQL = function SQL( } } - /// Ends a closing transaction: cancels what is pending, then sends `before` (XA END) and `rollback`. - /// Callers memoize it in state.rollback, so the wire sees one ROLLBACK. - /// Declared here and not in onTransactionConnected: one function per SQL instance, not one per transaction. + /// Rolls a closing transaction back. Declared outside onTransactionConnected: no closure per transaction. async function rollbackTransaction( state: TransactionState, pooledConnection: PooledConnection, @@ -910,8 +908,9 @@ const SQL = function SQL( return resolve(transaction_result); } catch (err) { try { - if (!(state.connectionState & ReservedConnectionState.closed) && needs_rollback) { - if (state.connectionState & ReservedConnectionState.closing) { + const { connectionState } = state; + if (!(connectionState & ReservedConnectionState.closed) && needs_rollback) { + if (connectionState & ReservedConnectionState.closing) { // close() owns the ROLLBACK: join it, or send it if close() still waits await (state.rollback ??= rollbackTransaction( state, From 445615f2345763d3ed5644480f70aa6658b6d155 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 8 Oct 2026 18:15:12 +0000 Subject: [PATCH 10/13] sql: the runner sends its own ROLLBACK when the one of close() failed When the callback settled while the ROLLBACK of close() was in flight, the runner joined that promise. If it failed, the connection went back to the pool with the transaction still open. The runner now waits for the ROLLBACK of close(), and if the transaction is still open after it, sends BEFORE and ROLLBACK itself, as it does for a callback that throws. rollbackTransaction() is a module-level function again and sends its statements through the transaction's own sender, which rejects with the connection-closed error after a disconnect, as on main. close() no longer clears state.rollback: nothing reads a cleared value now. Docs and types: a query cancelled before it starts never runs and rejects when it is awaited or executed. --- docs/runtime/sql.mdx | 2 + packages/bun-types/sql.d.ts | 5 +- src/js/bun/sql.ts | 75 ++++++++++--------- .../sql-pool-transaction-isolation.test.ts | 53 +++++++------ 4 files changed, 76 insertions(+), 59 deletions(-) diff --git a/docs/runtime/sql.mdx b/docs/runtime/sql.mdx index 7335d527fbdb..962521bc6a07 100644 --- a/docs/runtime/sql.mdx +++ b/docs/runtime/sql.mdx @@ -477,6 +477,8 @@ setTimeout(() => query.cancel(), 100); await query; ``` +A query that you cancel before it starts never runs. It rejects with the adapter's `QUERY_CANCELLED` error, for example `ERR_POSTGRES_QUERY_CANCELLED`, when you await it or call `.execute()`. + --- ## Database Environment Variables diff --git a/packages/bun-types/sql.d.ts b/packages/bun-types/sql.d.ts index 36d3d6e60311..bb2587fa54f8 100644 --- a/packages/bun-types/sql.d.ts +++ b/packages/bun-types/sql.d.ts @@ -470,7 +470,10 @@ declare module "bun" { cancelled: boolean; /** - * Cancels the executing query + * Cancels the executing query. + * + * A query that is cancelled before it starts never runs. It rejects with + * the adapter's `QUERY_CANCELLED` error when it is awaited or executed. */ cancel(): Query; diff --git a/src/js/bun/sql.ts b/src/js/bun/sql.ts index b7a61dfcc23e..d7c1709755f0 100644 --- a/src/js/bun/sql.ts +++ b/src/js/bun/sql.ts @@ -43,8 +43,8 @@ interface TransactionState { reject: (err: Error) => void; storedError?: Error | null | undefined; queries: Set>; - /// The ROLLBACK of a closing transaction. close() and the transaction runner share it. - rollback?: Promise | null; + /// The latest ROLLBACK of a closing transaction. close() or the runner starts it, and the other one reads it. + rollback?: Promise; } /// Bound as `this` to both callbacks of a reserve({ signal }) call, so each can @@ -82,6 +82,23 @@ function waitForPendingWork(pending: PromiseLike[], timeout: number): P return promise; } +/// Rolls a closing transaction back. `run` is the transaction's sender of BEGIN, COMMIT and ROLLBACK. +async function rollbackTransaction( + state: TransactionState, + run: (statement: string) => PromiseLike, + before: string | null, + rollback: string, +) { + for (const query of state.queries) { + query.cancel(); + } + if (before) { + await run(before); + } + await run(rollback); + state.connectionState |= ReservedConnectionState.closed; +} + function adapterFromOptions(options: Bun.SQL.__internal.DefinedOptions): Adapter | ListenableAdapter { switch (options.adapter) { case "postgres": @@ -277,23 +294,6 @@ const SQL = function SQL( } } - /// Rolls a closing transaction back. Declared outside onTransactionConnected: no closure per transaction. - async function rollbackTransaction( - state: TransactionState, - pooledConnection: PooledConnection, - before: string | null, - rollback: string, - ) { - for (const query of state.queries) { - query.cancel(); - } - if (before) { - await unsafeQueryFromTransaction(before, [], pooledConnection, state.queries); - } - await unsafeQueryFromTransaction(rollback, [], pooledConnection, state.queries); - state.connectionState |= ReservedConnectionState.closed; - } - function onTransactionDisconnected(this: TransactionState, err: Error) { const reject = this.reject; this.connectionState |= ReservedConnectionState.closed; @@ -806,18 +806,12 @@ const SQL = function SQL( } } state.connectionState |= ReservedConnectionState.closing; - try { - await (state.rollback ??= rollbackTransaction( - state, - pooledConnection, - BEFORE_COMMIT_OR_ROLLBACK_COMMAND, - ROLLBACK_COMMAND, - )); - } catch (err) { - // close() reports the failure, and the runner sends ROLLBACK again when the callback settles - state.rollback = null; - throw err; - } + return (state.rollback = rollbackTransaction( + state, + run_internal_transaction_sql, + BEFORE_COMMIT_OR_ROLLBACK_COMMAND, + ROLLBACK_COMMAND, + )); }; transaction_sql[Symbol.asyncDispose] = () => transaction_sql.close(); transaction_sql.options = sql.options; @@ -908,13 +902,20 @@ const SQL = function SQL( return resolve(transaction_result); } catch (err) { try { - const { connectionState } = state; - if (!(connectionState & ReservedConnectionState.closed) && needs_rollback) { - if (connectionState & ReservedConnectionState.closing) { - // close() owns the ROLLBACK: join it, or send it if close() still waits - await (state.rollback ??= rollbackTransaction( + const closing = state.connectionState & ReservedConnectionState.closing; + if (closing && needs_rollback) { + try { + // the ROLLBACK that close() has in flight, if any + await state.rollback; + } catch { + // close() reports that failure, and the runner sends its own ROLLBACK below + } + } + if (!(state.connectionState & ReservedConnectionState.closed) && needs_rollback) { + if (closing) { + await (state.rollback = rollbackTransaction( state, - pooledConnection, + run_internal_transaction_sql, BEFORE_COMMIT_OR_ROLLBACK_COMMAND, ROLLBACK_COMMAND, )); diff --git a/test/js/sql/sql-pool-transaction-isolation.test.ts b/test/js/sql/sql-pool-transaction-isolation.test.ts index f80a949e3aa0..0f4e5eb3b0b4 100644 --- a/test/js/sql/sql-pool-transaction-isolation.test.ts +++ b/test/js/sql/sql-pool-transaction-isolation.test.ts @@ -953,27 +953,38 @@ describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand, connec expect(pool.received).toEqual([...onConn0(beginCommand, "SELECT 'T1a'", "COMMIT"), ...afterTransaction(0)]); }); - // The server rejects the ROLLBACK of close(). close() reports that, and the transaction is - // still closing: when the callback returns, the runner sends ROLLBACK again and no COMMIT. - test("transaction.close() whose ROLLBACK fails leaves the rollback to the runner", async () => { - await using pool = await closeTestPool({ failOnce: "ROLLBACK" }); - let closeError: unknown; - const outcome = await settledCode( - pool.sql.begin(async tx => { - await tx.unsafe("SELECT 'T1a'"); - closeError = await tx.close().then( - () => null, - error => error.message, - ); - }), - ); - expect({ closeError, outcome }).toEqual({ closeError: "mock failure", outcome: connectionClosedCode }); - await expectConnectionKept(pool); - expect(pool.received).toEqual([ - ...onConn0(beginCommand, "SELECT 'T1a'", "ROLLBACK", "ROLLBACK"), - ...afterTransaction(0), - ]); - }); + // The server rejects the ROLLBACK of close(), and close() reports that. The transaction is + // still open, so the runner sends ROLLBACK once more before the connection goes back to the + // pool, and it sends no COMMIT. + test.each([ + { when: "after close() rejected", awaitClose: true }, + { when: "while the ROLLBACK of close() is in flight", awaitClose: false }, + ])( + "the runner sends ROLLBACK again when the ROLLBACK of close() fails and the callback settles $when", + async ({ awaitClose }) => { + await using pool = await closeTestPool({ failOnce: "ROLLBACK" }); + let closeError: Promise | undefined; + const outcome = await settledCode( + pool.sql.begin(async tx => { + await tx.unsafe("SELECT 'T1a'"); + closeError = tx.close().then( + () => null, + error => error.message, + ); + if (awaitClose) await closeError; + }), + ); + expect({ closeError: await closeError, outcome }).toEqual({ + closeError: "mock failure", + outcome: connectionClosedCode, + }); + await expectConnectionKept(pool); + expect(pool.received).toEqual([ + ...onConn0(beginCommand, "SELECT 'T1a'", "ROLLBACK", "ROLLBACK"), + ...afterTransaction(0), + ]); + }, + ); // A savepoint whose callback parks until finish() is called. function parkedSavepoint(tx: Bun.TransactionSQL) { From 09211d7b801aa5a9096f282b3d41d614993939f7 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 8 Oct 2026 18:21:50 +0000 Subject: [PATCH 11/13] types: cancel() cancels a query, also one that has not started --- packages/bun-types/sql.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/bun-types/sql.d.ts b/packages/bun-types/sql.d.ts index bb2587fa54f8..2b9b102698cc 100644 --- a/packages/bun-types/sql.d.ts +++ b/packages/bun-types/sql.d.ts @@ -470,7 +470,7 @@ declare module "bun" { cancelled: boolean; /** - * Cancels the executing query. + * Cancels the query. * * A query that is cancelled before it starts never runs. It rejects with * the adapter's `QUERY_CANCELLED` error when it is awaited or executed. From 0fef54d7675454daef4cb63c1df9291c947a280a Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 8 Oct 2026 18:53:32 +0000 Subject: [PATCH 12/13] sql: the runner reads state.rollback once, so close() and the runner never both send ROLLBACK The catch path of the runner awaited state.rollback also when it was unset. That gave up one microtask, and a close({ timeout }) that left its wait in that gap sent a ROLLBACK of its own before the runner sent one. The runner now reads state.rollback once. If it is unset, the runner stores its own ROLLBACK in the same tick. --- src/js/bun/sql.ts | 8 ++++--- .../sql-pool-transaction-isolation.test.ts | 23 +++++++++++++++++++ test/js/sql/sqlite-sql.test.ts | 16 ++++++++++++- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/js/bun/sql.ts b/src/js/bun/sql.ts index d7c1709755f0..a927bea318ff 100644 --- a/src/js/bun/sql.ts +++ b/src/js/bun/sql.ts @@ -903,10 +903,12 @@ const SQL = function SQL( } catch (err) { try { const closing = state.connectionState & ReservedConnectionState.closing; - if (closing && needs_rollback) { + // no await between this read and the write below: close() can leave its wait in any tick + const closeRollback = state.rollback; + if (closeRollback && needs_rollback) { try { - // the ROLLBACK that close() has in flight, if any - await state.rollback; + // the ROLLBACK that close() has in flight + await closeRollback; } catch { // close() reports that failure, and the runner sends its own ROLLBACK below } diff --git a/test/js/sql/sql-pool-transaction-isolation.test.ts b/test/js/sql/sql-pool-transaction-isolation.test.ts index 0f4e5eb3b0b4..05c5c8ea4cb4 100644 --- a/test/js/sql/sql-pool-transaction-isolation.test.ts +++ b/test/js/sql/sql-pool-transaction-isolation.test.ts @@ -899,6 +899,29 @@ describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand, connec expect(pool.received).toEqual([...onConn0(beginCommand, "SELECT 'pending'", "ROLLBACK"), ...afterTransaction(0)]); }); + // The pending query ends the wait of close() and the callback within a few microtasks of each + // other. Each then() moves the callback one microtask later, so the runner and close() meet + // in every order. Only one of the two may send the rollback. + test.each([0, 1, 2, 3])( + "transaction.close({ timeout }) and a callback that awaits the pending query behind %d then() calls send one rollback", + async hops => { + await using pool = await closeTestPool(); + let closed: Promise | undefined; + const outcome = await settledCode( + pool.sql.begin(async tx => { + let pending: Promise = tx.unsafe("SELECT 'pending'").execute(); + for (let hop = 0; hop < hops; hop++) pending = pending.then(rows => rows); + closed = tx.close({ timeout: 60 }); + await pending; + }), + ); + expect(outcome).toBe(connectionClosedCode); + await closed; + await expectConnectionKept(pool); + expect(pool.received).toEqual([...onConn0(beginCommand, "SELECT 'pending'", "ROLLBACK"), ...afterTransaction(0)]); + }, + ); + // The callback's own error reaches begin(), and the runner and close() share one rollback. test("a callback that throws while transaction.close({ timeout }) waits keeps its error", async () => { await using pool = await closeTestPool(); diff --git a/test/js/sql/sqlite-sql.test.ts b/test/js/sql/sqlite-sql.test.ts index aa861427e294..1c42405665de 100644 --- a/test/js/sql/sqlite-sql.test.ts +++ b/test/js/sql/sqlite-sql.test.ts @@ -1405,7 +1405,8 @@ describe("Transactions", () => { // also fails these tests if close() leaves a rejection unhandled. describe("tx.close() rolls the transaction back", () => { const connectionClosed = "ERR_SQLITE_CONNECTION_CLOSED"; - const orders: Array<[string, (tx: Bun.TransactionSQL) => Promise, string]> = [ + type Order = [name: string, close: (tx: Bun.TransactionSQL) => Promise, expected: string]; + const orders: Order[] = [ [ "close({ timeout }) with no second query", async tx => { @@ -1445,6 +1446,19 @@ describe("tx.close() rolls the transaction back", () => { }, connectionClosed, ], + // Each then() moves the callback one microtask later against the wait of close(). + ...[0, 1, 2, 3].map( + (hops): Order => [ + `close({ timeout }) that the callback does not await, while it awaits the second query behind ${hops} then() calls`, + async tx => { + let second: Promise = tx`SELECT 1 AS x`; + for (let hop = 0; hop < hops; hop++) second = second.then(rows => rows); + tx.close({ timeout: 60 }); + await second; + }, + connectionClosed, + ], + ), [ "close() that the callback does not await", async tx => { From 174ae0aea84bb163188bf8fe1c8a0394769c5cd8 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 9 Oct 2026 00:51:16 +0000 Subject: [PATCH 13/13] sql: close({ timeout }) settles with the rollback that the runner sends for it When the callback settles while close({ timeout }) waits, the runner sends the ROLLBACK. close() returned as soon as its own wait ended, so it could resolve before that ROLLBACK had an answer. close() now waits for it. A failure of that ROLLBACK is reported by begin() only. --- src/js/bun/sql.ts | 14 +++- .../sql-pool-transaction-isolation.test.ts | 71 ++++++++++++++++--- 2 files changed, 73 insertions(+), 12 deletions(-) diff --git a/src/js/bun/sql.ts b/src/js/bun/sql.ts index a927bea318ff..05bb88a9119c 100644 --- a/src/js/bun/sql.ts +++ b/src/js/bun/sql.ts @@ -801,8 +801,18 @@ const SQL = function SQL( // set before the wait: the callback can settle while close() waits state.connectionState |= ReservedConnectionState.closing; await waitForPendingWork([...transactionQueries, ...transactionSavepoints], timeout); - // a disconnect ended the transaction, or the runner already rolls it back - if (state.connectionState & ReservedConnectionState.closed || state.rollback) return; + // a disconnect or the runner ended the transaction + if (state.connectionState & ReservedConnectionState.closed) return; + const runnerRollback = state.rollback; + if (runnerRollback) { + try { + // the runner rolls the transaction back: close() settles when that ROLLBACK has settled + await runnerRollback; + } catch { + // begin() reports that failure + } + return; + } } } state.connectionState |= ReservedConnectionState.closing; diff --git a/test/js/sql/sql-pool-transaction-isolation.test.ts b/test/js/sql/sql-pool-transaction-isolation.test.ts index 05c5c8ea4cb4..12915d0d4923 100644 --- a/test/js/sql/sql-pool-transaction-isolation.test.ts +++ b/test/js/sql/sql-pool-transaction-isolation.test.ts @@ -27,8 +27,14 @@ import { type Received = { conn: number; sql: string }; // `onReceived` runs after each statement is recorded. `secure` makes the server accept the // client's TLS request and speak the protocol over TLS from there on. `failOnce` is a -// statement that the server answers with an error the first time it arrives. -type MockOptions = { onReceived?: () => void; secure?: boolean; failOnce?: string }; +// statement that the server answers with an error the first time it arrives. `answerLater` +// keeps the answer to the first statement that equals `sql` until `when` resolves. +type MockOptions = { + onReceived?: (sql: string) => void; + secure?: boolean; + failOnce?: string; + answerLater?: { sql: string; when: Promise }; +}; type MockServer = (received: Received[], options?: MockOptions) => Promise<{ port: number; server: net.Server }>; function secureSocket(rawSocket: net.Socket): net.Socket { @@ -49,12 +55,26 @@ function failureCheck(failOnce: string | undefined) { }; } +// Writes the answer to a statement, at once or after `answerLater.when`. +function answerWriter(answerLater: MockOptions["answerLater"]) { + return (socket: net.Socket, sql: string, answer: Buffer) => { + if (sql !== answerLater?.sql) { + socket.write(answer); + return; + } + const { when } = answerLater; + answerLater = undefined; + when.then(() => socket.write(answer)); + }; +} + // Both mocks answer every statement at once, except for these markers in the query text: // KILL destroys the socket without answering, // FAIL answers with an error, // HOLD never answers. -const pgMockServer: MockServer = (received, { onReceived, secure, failOnce } = {}) => { +const pgMockServer: MockServer = (received, { onReceived, secure, failOnce, answerLater } = {}) => { const fails = failureCheck(failOnce); + const answer = answerWriter(answerLater); let nextConn = 0; return listeningServer(rawSocket => { const connId = nextConn++; @@ -91,27 +111,30 @@ const pgMockServer: MockServer = (received, { onReceived, secure, failOnce } = { if (type !== "Q") continue; const sql = body.subarray(0, body.indexOf(0)).toString("utf8"); received.push({ conn: connId, sql }); - onReceived?.(); + onReceived?.(sql); if (sql.includes("KILL")) { socket.destroy(); return; } if (sql.includes("HOLD")) continue; if (fails(sql)) { - socket.write( + answer( + socket, + sql, Buffer.concat([pgErrorResponse({ S: "ERROR", C: "XX000", M: "mock failure" }), pgReadyForQuery()]), ); continue; } - socket.write(Buffer.concat([pgCommandComplete("SELECT 0"), pgReadyForQuery()])); + answer(socket, sql, Buffer.concat([pgCommandComplete("SELECT 0"), pgReadyForQuery()])); } }); } }); }; -const mysqlMockServer: MockServer = (received, { onReceived, secure, failOnce } = {}) => { +const mysqlMockServer: MockServer = (received, { onReceived, secure, failOnce, answerLater } = {}) => { const fails = failureCheck(failOnce); + const answer = answerWriter(answerLater); const COM_QUIT = 0x01; const COM_QUERY = 0x03; let nextConn = 0; @@ -152,17 +175,17 @@ const mysqlMockServer: MockServer = (received, { onReceived, secure, failOnce } if (payload[0] === COM_QUERY) { const sql = payload.subarray(1).toString("utf8"); received.push({ conn: connId, sql }); - onReceived?.(); + onReceived?.(sql); if (sql.includes("KILL")) { socket.destroy(); return; } if (sql.includes("HOLD")) return; if (fails(sql)) { - socket.write(mysqlErrPacket(1, 1105, "HY000", "mock failure")); + answer(socket, sql, mysqlErrPacket(1, 1105, "HY000", "mock failure")); return; } - socket.write(mysqlOkPacket(1)); + answer(socket, sql, mysqlOkPacket(1)); } else if (payload[0] === COM_QUIT) { socket.end(); } @@ -867,6 +890,34 @@ describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand, connec }, ); + // The callback returns while close({ timeout }) waits, so the runner sends the rollback. The + // pending query has its answer when the rollback reaches the server, so the wait of close() + // is over by then. close() resolves only when the rollback has its answer too. + test("transaction.close({ timeout }) resolves after the rollback that the runner sends for it", async () => { + const rollbackArrived = Promise.withResolvers(); + const rollbackAnswer = Promise.withResolvers(); + await using pool = await closeTestPool({ + onReceived: sql => { + if (sql === "ROLLBACK") rollbackArrived.resolve(); + }, + answerLater: { sql: "ROLLBACK", when: rollbackAnswer.promise }, + }); + let closed: Promise | undefined; + const outcome = settledCode( + pool.sql.begin(async tx => { + tx.unsafe("SELECT 'pending'").execute(); + closed = tx.close({ timeout: 60 }); + }), + ); + await rollbackArrived.promise; + expect(Bun.peek.status(closed!)).toBe("pending"); + rollbackAnswer.resolve(); + expect(await outcome).toBe(connectionClosedCode); + await closed; + await expectConnectionKept(pool); + expect(pool.received).toEqual([...onConn0(beginCommand, "SELECT 'pending'", "ROLLBACK"), ...afterTransaction(0)]); + }); + test("transaction.close() that the callback does not await rolls back, and nothing commits after it", async () => { await using pool = await closeTestPool(); let closed: Promise | undefined;