Skip to content

Bun.SQL: a graceful close() after reserved.release() does not wait for the queries of the reserved connection #44039

Closed as not planned

Description

@robobun

What version of Bun is running?

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";
const settle = p => p.then(rows => rows[0].x, e => e.code ?? e.message);

{
  const sql = new SQL(process.env.DATABASE_URL, { max: 1 });
  const reserved = await sql.reserve();
  const query = settle(reserved`select 1 as x`.execute());
  reserved.release();
  await sql.close();
  console.log("execute(), release(), sql.close():", await query);
}
{
  const sql = new SQL(process.env.DATABASE_URL, { max: 1 });
  const reserved = await sql.reserve();
  const query = settle(reserved`select 1 as x`.then(rows => rows));
  reserved.release();
  await sql.close();
  console.log("then(), release(), sql.close():", await query);
}

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

  1. added
    bugSomething isn't working
    sqlSomething to do with `sql` in the "bun" module (postgres, sqlite & mysql)
    on Sep 26, 2026
  2. Jarred-Sumner commented on Oct 8, 2026

    @Jarred-Sumner
    Collaborator

    Use handoff tool. GitHub issues are for user-facing issues and a user did not run into this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingsqlSomething to do with `sql` in the "bun" module (postgres, sqlite & mysql)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions