Repository navigation
Conversation
A query is lazy. execute() gives it to the pool at once. then(), catch(), finally() and run() give it to the pool one promise job later. `await query` gives it to the pool two promise jobs later. No test covered this timing. No test covered the job in which each start settles, the order of the starts, cancel() in the tick of the start, run(), the unhandledRejection events of a failed query, a start inside onclose, or the async context that a function-valued password observes. These tests pass with no change to src.
SQLite tests are in sqlite-sql.test.ts. PostgreSQL and MySQL tests use a
real server (describeWithContainer). Each test has a table of its own, and
a second pool reads the table after the close. A row shows that the server
got the statement.
Three cases per driver need a mock: a close({ timeout }) that expires with
a sent query, a server that never answers, and a refused connection.
The "Query start" pins keep the exact promise job only for execute(),
then(), catch(), finally() and run(). The other ways to start a query are
pinned relative to then().
The docs and the JSDoc of close() say which queries close() waits for.
StatusDraft. It is one of two designs for #43887. The decision that it needs is in #43887 (comment). The other design is #44105, which is smaller and which I recommend. How to reproduceNo server is necessary: import { SQL } from "bun";
const sql = new SQL("sqlite://:memory:");
await sql`create table t (x)`;
const inserted = sql`insert into t values (1) returning x`.then(
rows => rows[0].x,
e => e.code,
);
await sql.close();
console.log(await inserted);
PostgreSQL and MySQL behave the same with PR: #44040 |
The PostgreSQL and MySQL tests of close() and of the start of a lazy query are now in sql-close-started-queries.test.ts. The tests of a block share one table. Each test writes rows with an id of its own. sql-close-pending-connection.test.ts and sql-onconnect-onclose-throw.test.ts are as on the base again.
|
Updated 7:14 AM PT - Sep 27th, 2026
✅ @robobun, your commit 46b61c2b6e38560be9fb2be9ac42575fcf8bffb8 passed in 🧪 To try this PR locally: bunx bun-pr 44040That installs a local version of the PR into your bun-44040 --bun |
|
@robobun wake up!! |
|
@robobun wake up!! |
|
@Jarred-Sumner This PR is complete and CI is green. It is a draft only because it needs one decision from a maintainer. It is the second of two fixes for #43887. The first one is #44105, and I recommend #44105: it is smaller (source +8 -51, against +115 -5 here), and each query gets cheaper. The comparison and the steps to merge are in #44105 (comment).
|
|
A query started with This design keeps the cause, the one promise job that #44105 deletes the wait. The query reaches the pool in the call of The guarantee is the same. A query that was started before Nothing from |
Part of #43887
Problem
await sql.close(); // await all queries to finish(docs/runtime/sql.mdx:976,packages/bun-types/sql.d.ts:683). A query that starts in the tick ofclose()rejects withERR_*_CONNECTION_CLOSEDand is never sent.then(),catch(),finally()andrun()give the query to the pool one promise job later (#runAsync,src/js/internal/sql/query.ts).close()looks for pending queries at once.query.execute().Fix
#runAsync()lists a pool query on its adapter during that job. Aclose()that waits gives the listed queries to the pool first.Querykeeps its timing: the pins pass with and without the fix.test/js/sql/sqlite-sql.test.tsandsql-close-started-queries.test.ts(75 tests fail without the fix). Alsotest/js/sql/.close()/end()is rejected and never sent #43887 asks a maintainer to choose.Background
Queryis a lazy promise: it runs when a caller starts it. postgres.js pairs the same delay withawait 1inend().close(), as in postgres.js: SQLite then closes one job after the call.Downsides
close()now opens a pool that has no connection: up tomaxsockets andpassword()calls, and a wait of up toconnectionTimeout.q.then(); sql.close(); q.cancel()on a connected pool now sends the query..bun_builtinsgrows by 2,003 B.Notes
Guarantee.
then(),catch(),finally(),run()orexecute()was called beforeclose(),end()orSymbol.asyncDispose. Also the statement ofsql.commitDistributed()andsql.rollbackDistributed().close(). A query that onlyawait,Promise.all(),Promise.race()orPromise.resolve()starts, because they callthen()in a later promise job.Promise.all([q, sql.close()]), whereclose()runs beforePromise.alltouches the query.close({ timeout: 0 })closes at once and sends nothing.q.then(); sql.end(), for a start afterend(), and for a query that onlyawaitstarts. Measured against a local PostgreSQL on Node.js and on Bun.Open decisions for a maintainer (asked on #43887).
close()runs no started query while the callback of asql.begin()has its transaction open. The statement would run inside that transaction, andclose()rolls the transaction back. The query rejects as before. If the answer is "run it anyway", the exception and its test go away.SQLiteAdapter.close()ignoredtimeoutbefore. It now reads atimeoutthat is loosely equal to 0 as "do not run the started queries". It validates nothing.Mechanism.
[_nextStarted]). Its two ends are on the adapter (firstStarted,lastStarted). A start allocates nothing. Aclose()visits only the queries of its own pool.await undefined. To await a promise readspromise.constructor, and code of the caller can run there (an accessor onPromise.prototype). It could start a query inside the start of another query.handOffStartedQueries(adapter)sets the status bithandedOffand calls the handler of the query. When the one job of that query ends, the query sees the bit and does nothing more.password), and that code can callclose().BaseSQLAdapter.close()readsclosedafter the hand-off. When the hand-off closed the pool, it returns the pending close of sql: a later close() waits for the pending close, for at most its timeout #43959.SQLiteAdapter.close()reads_closedagain after the hand-off.SQLiteAdapter.close()readsoptions.timeoutbefore it reads_closed. A getter can callclose()there.db.inTransactiononly when a query is in the list, and insidetry.Bun.ModuleGraph.dispose()closes the database under the adapter, and the read then throws.SQLITE_CANTOPEN), asexecute()does.sql.commitDistributed()andsql.rollbackDistributed()ended inreturn await sql.unsafe(...). That starts the query one promise job after the call. They now call.then()on the query, which starts it in the call.Measurements. Release builds of the base branch (#43959) and of this PR from the same tree. Only four files in
src/jsdiffer.#runAsyncfor each pool queryQuery, every queryawait q: SQLite, PostgreSQL, MySQLq.execute(): SQLite, PostgreSQL, MySQLawait tx\...``: SQLite, PostgreSQL, MySQLawait q: SQLite, PostgreSQLq.execute(): SQLite, PostgreSQL#runAsyncquery.js,shared.js,sqlite.js,sql.js.text.bun_builtinsexecute()starts runs 4 more.Promise.$resolve()made.BUN_JSC_dumpGeneratedBytecodes=1.heapStats().objectTypeCounts, slope between 500 and 1,500 queries, types with an integer slope.gdbcounting breakpoint onJSC::runInternalMicrotask, slope between 1,000 and 2,000 queries.perf,valgrind,strace,bloatyandhyperfineare not installed in the build container, so there are no counts of native instructions or of syscalls.What changes besides the bug. Each row has a test.
close()password()not calledpassword()calledmaxtimes,onconnectandonclosein pairsERR_*_CONNECTION_CLOSEDat onceERR_*_CONNECTION_TIMEOUTafterconnectionTimeoutERR_*_CONNECTION_CLOSEDERR_*_CONNECTION_REFUSEDq.then(); sql.close(); q.cancel(), connected poolERR_*_QUERY_CANCELLED, not sentERR_*_QUERY_CANCELLED, not sentERR_*_QUERY_CANCELLED, not sentpassword, whenclose()opens the poolclose()close({ timeout })that expires, with a started queryclose()A pool has no connection when it never connected, and also after
idleTimeoutclosed its connections.Tests.
execute(),then(),catch(),finally()andrun()reach the database and settle. The other ways to start a query are pinned relative tothen(). Each pin fails when its property is broken. If sql: start a query in the call of then(), catch(), finally() and run() #44105 is the choice, these pins go away with this PR.describeWithContainer,postgres_plainandmysql_plain) in a new file,sql-close-started-queries.test.ts. Each test writes rows with an id of its own, and a second pool reads them after the close. Three cases per driver need a mock: a timeout that expires with a sent query, a server that never answers, a refused connection.sqlite-sql.test.tsand 49 insql-close-started-queries.test.ts.test/js/sql/: the same tests fail with the PR and on the base in this container. Two leak fixtures time out in a debug build, and the local MariaDB words two error messages differently from MySQL.test/integration/bun-types/bun-types.test.ts: 21 pass.Self-review. Two reviews, 25 concerns, 21 addressed.
Promise.prototype.constructorwas replaced, andclose()threw afterBun.ModuleGraph.dispose().onclosefired twice on SQLite when a getter fortimeoutcalledclose(), and a started query gotERR_SQLITE_CONNECTION_CLOSEDin place of the error of an open that failed..execute()insql.commitDistributed()andsql.rollbackDistributed(). Measured on PostgreSQL: with.execute(), a call insideonclosefails with "connection must be a PostgresSQLConnection", and a wrappedsql.unsafethat returns a plain promise throwsTypeError..then()keeps both cases as they are.close()insideonclosewith a started query. The hand-off reaches the connection that is still closing, and the query rejects with "connection must be a PostgresSQLConnection". The base rejects it withERR_POSTGRES_CONNECTION_CLOSED. With sql: finish the pool slot's close transition before calling the user's onclose #43958 applied the query runs on a new connection, so sql: finish the pool slot's close transition before calling the user's onclose #43958 must merge first.idleTimeoutormaxLifetimeclosed. Seven clauses have no test that fails without them, for example the two halves of the guard inBaseSQLAdapter.close().BaseSQLAdapter.close()and inSQLiteAdapter.close(). The review asks for one place.Other PRs.
close({ timeout: 0 }). This PR adds the drain that sql: close({ timeout: 0 }) closes at once (gate on presence, not truthiness) #33740 removed.closeHandlersset and the same two hooks toSQLiteAdapter. The second one to land needs a small merge.oncloseand the removal of the dead connection. It does not touch the same lines.Not in this PR.
sql.file(): Bun.SQL: the query ofsql.file()is rejected when a gracefulclose()follows in the same tick #44038.release()gave back beforeclose(): Bun.SQL: a gracefulclose()afterreserved.release()does not wait for the queries of the reserved connection #44039.sql.begin()in the tick ofclose()rejects: sql(sqlite): reject an in-flight begin() when the instance is closed #43116.reserved.commitDistributed()andtxtwins keepreturn await. They run on a connection that the caller holds, andclose()waits for that connection.[human-review] gate passed · iteration 1 · 8 files touched
fails on main (without fix)
passes on PR (with fix)
diff hotspot
gate history · 1 passed · 1 rejected · iteration 1
evidence per changed file