You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bun.SQL: a graceful close() after reserved.release() does not wait for the queries of the reserved connection #44039
1.4.3-canary.1+3fc38e6e5. The code is the same on main at 36cd151.
What platform is your computer?
Linux x64 (the code is JavaScript in src/js/bun/sql.ts and src/js/internal/sql/shared.ts, it is the same on every platform)
What steps can reproduce the bug?
A local PostgreSQL or MySQL is necessary. SQLite has no reserved connections.
import{SQL}from"bun";constsettle=p=>p.then(rows=>rows[0].x,e=>e.code??e.message);{constsql=newSQL(process.env.DATABASE_URL,{max: 1});constreserved=awaitsql.reserve();constquery=settle(reserved`select 1 as x`.execute());reserved.release();awaitsql.close();console.log("execute(), release(), sql.close():",awaitquery);}{constsql=newSQL(process.env.DATABASE_URL,{max: 1});constreserved=awaitsql.reserve();constquery=settle(reserved`select 1 as x`.then(rows=>rows));reserved.release();awaitsql.close();console.log("then(), release(), sql.close():",awaitquery);}
What is the expected behavior?
Both lines print 1.
close() with no timeout is a graceful close. packages/bun-types/sql.d.ts says "with no timeout it waits for all queries to finish first". Both queries started before release() and before close().
What do you see instead?
With PostgreSQL:
execute(), release(), sql.close(): ERR_POSTGRES_CONNECTION_CLOSED
then(), release(), sql.close(): connection must be a PostgresSQLConnection
With MySQL:
execute(), release(), sql.close(): ERR_MYSQL_CONNECTION_CLOSED
then(), release(), sql.close(): connection must be a MySQLConnection
The first query was sent. close() does not wait for the answer. The second query was not sent, and its error is an internal message with no code.
Without the sql.close() call, both queries resolve.
Additional information
reserved.release() gives the connection back to the pool at once. It does not wait for the queries that are in flight on the connection (state.queries in src/js/bun/sql.ts). After that, the pool does not count these queries: hasPendingQueries() in src/js/internal/sql/shared.ts reads the waiting queue, the reserved queue and totalQueries. So a graceful close() sees nothing pending and closes the connection.
The second query is lazy. It reaches the connection one promise job after then(). By then close() has closed the connection, and the native call rejects the dead connection object.
Related reports about a query on a reserved connection or on a transaction: #43245, #43265. The same contract for queries of the pool: #43887.
Activity
Jarred-Sumner commentedon Oct 8, 2026
Use handoff tool. GitHub issues are for user-facing issues and a user did not run into this issue