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..2b9b102698cc 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 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 64bf08c13cb8..05bb88a9119c 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 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 @@ -63,6 +67,38 @@ 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; +} + +/// 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": @@ -493,39 +529,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 @@ -604,7 +624,7 @@ const SQL = function SQL( }; let savepoints = 0; - let transactionSavepoints = new Set(); + let transactionSavepoints = new Set>(); let BEGIN_COMMAND: string; let ROLLBACK_COMMAND: string; @@ -778,37 +798,30 @@ const SQL = function SQL( } if (timeout > 0 && (transactionQueries.size > 0 || transactionSavepoints.size > 0)) { - const { promise, resolve } = Promise.withResolvers(); - // race all queries vs timeout - 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); + // set before the wait: the callback can settle while close() waits + state.connectionState |= ReservedConnectionState.closing; + await waitForPendingWork([...transactionQueries, ...transactionSavepoints], timeout); + // 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 } - 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(() => { - clearTimeout(timer); - resolve(); - }); - return promise; + return; + } } } - 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; + state.connectionState |= ReservedConnectionState.closing; + 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; @@ -878,6 +891,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) { @@ -897,11 +912,31 @@ const SQL = function SQL( return resolve(transaction_result); } catch (err) { try { + const closing = state.connectionState & ReservedConnectionState.closing; + // 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 + await closeRollback; + } catch { + // close() reports that failure, and the runner sends its own ROLLBACK below + } + } if (!(state.connectionState & ReservedConnectionState.closed) && needs_rollback) { - if (BEFORE_COMMIT_OR_ROLLBACK_COMMAND) { - await run_internal_transaction_sql(BEFORE_COMMIT_OR_ROLLBACK_COMMAND); + if (closing) { + await (state.rollback = rollbackTransaction( + state, + run_internal_transaction_sql, + 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); 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/sql-pool-transaction-isolation.test.ts b/test/js/sql/sql-pool-transaction-isolation.test.ts index 6e20d7a26293..12915d0d4923 100644 --- a/test/js/sql/sql-pool-transaction-isolation.test.ts +++ b/test/js/sql/sql-pool-transaction-isolation.test.ts @@ -5,90 +5,193 @@ // 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. `failOnce` is a +// 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 { + rawSocket.pause(); + const socket = new tls.TLSSocket(rawSocket, { isServer: true, ...tlsCert }); + socket.on("error", () => {}); + 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"); + }; +} -// Query text containing "KILL" destroys the socket without answering. -const pgMockServer: MockServer = received => { +// 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, answerLater } = {}) => { + const fails = failureCheck(failOnce); + const answer = answerWriter(answerLater); 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?.(sql); + if (sql.includes("KILL")) { + socket.destroy(); + return; + } + if (sql.includes("HOLD")) continue; + if (fails(sql)) { + answer( + socket, + sql, + Buffer.concat([pgErrorResponse({ S: "ERROR", C: "XX000", M: "mock failure" }), pgReadyForQuery()]), + ); + continue; + } + answer(socket, sql, Buffer.concat([pgCommandComplete("SELECT 0"), pgReadyForQuery()])); + } + }); + } }); }; -const mysqlMockServer: MockServer = received => { +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; - 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?.(sql); + if (sql.includes("KILL")) { + socket.destroy(); + return; + } + if (sql.includes("HOLD")) return; + if (fails(sql)) { + answer(socket, sql, mysqlErrPacket(1, 1105, "HY000", "mock failure")); + return; + } + answer(socket, sql, mysqlOkPacket(1)); + } else if (payload[0] === COM_QUIT) { + socket.end(); + } + }); }); - }); - socket.on("error", () => {}); + } }); }; @@ -109,11 +212,44 @@ 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; + // 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'"] }, + }, ]; +// 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 +264,7 @@ const rejectedBeforeBegin = [ }, ]; -describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand }) => { +describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand, connectionClosedCode, distributed }) => { const options = (port: number): Bun.SQL.Options => ({ adapter, hostname: "127.0.0.1", @@ -392,6 +528,656 @@ describe.each(adapters)("$adapter", ({ adapter, mockServer, beginCommand }) => { } }); + // 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, 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(); + } + + // 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"; + }); + const closed = reserved.close({ timeout: 60 }); + expect(await t1).toBe("t1"); + await closed; + reserved.release(); + await expectSlotReturned(pool); + expect(pool.received).toEqual([ + { conn: 0, sql: beginCommand }, + { conn: 0, sql: "SELECT 'T1a'" }, + { conn: 0, sql: "COMMIT" }, + ...afterTransaction(1), + ]); + }); + + 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 + .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), + ]); + }); + + // 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(); + 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(); + // 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: "COMMIT" }, + ...afterTransaction(0), + ]); + }, + ); + + // 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(); + }, + ); + + // 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)]); + }, + ); + + // 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; + 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 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(); + 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)]); + }); + + // 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) { + 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/sqlite-sql.test.ts b/test/js/sql/sqlite-sql.test.ts index ed0567c186fb..1c42405665de 100644 --- a/test/js/sql/sqlite-sql.test.ts +++ b/test/js/sql/sqlite-sql.test.ts @@ -915,6 +915,102 @@ 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(); + // 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 }]); + }); + + // 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 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. @@ -1254,6 +1350,152 @@ 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 }]); + }); + + // 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 +// 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. +// +// 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"; + type Order = [name: string, close: (tx: Bun.TransactionSQL) => Promise, expected: string]; + const orders: Order[] = [ + [ + "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, + ], + // 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 => { + 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( + () => "committed", + error => error.code ?? error.message, + ); + + await using reader = new SQL(url); + const [{ rows }] = await reader`SELECT count(*) AS rows FROM t`; + expect({ begin, rows }).toEqual({ begin: expected, rows: 0 }); + }); }); describe("SQLite-specific features", () => { 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"),