Skip to content

feat: support await using on the pool and reserved connections - #53

Closed
okomarov wants to merge 1 commit into
mainfrom
feat/async-dispose
Closed

okomarov wants to merge 1 commit into
mainfrom
feat/async-dispose

Conversation

@okomarov

Copy link
Copy Markdown

Closes #52.

await using sql = postgres()         // sql.end() when the scope exits
using reserved = await sql.reserve() // reserved.release() when the scope exits
  • The pool gets [Symbol.asyncDispose] calling end(), on the top-level instance only: reserved and transaction handles are built by the same Sql() and must never end the pool (tested).
  • Reserved connections get [Symbol.dispose] and [Symbol.asyncDispose], both calling release().
  • Transaction, savepoint and cursor handles are already scoped by callbacks or for await. sql.listen() and sql.subscribe() handles could map to unlisten and unsubscribe later.
  • Each symbol is set only when the runtime defines it. The using syntax needs Node.js 24+; on Node 22 the methods can be called directly, and the syntax test passes without running.
  • Types for all three; types/package.json moves @types/node to ^22 to match engines.

Also carries 045695c (tracer compatibility), which is on local main but not yet pushed.

The pool gets [Symbol.asyncDispose], which calls end() with no timeout,
and reserved connections get [Symbol.dispose] and [Symbol.asyncDispose],
which call release(). `await using sql = postgres()` and
`using reserved = await sql.reserve()` can no longer leak the pool or a
reservation.

The pool method is set only on the top-level instance, never in Sql(),
because Sql() also builds reserved and transaction handles and disposing
those must not end the pool. Transaction handles, cursors, listen and
subscribe are not disposable: begin() and for await already scope the
first two. Both end() and release() are idempotent, so disposing after
an explicit call is harmless. Each symbol is set only when the runtime
defines it.

The types bump @types/node to ^22 to match engines, since the symbols
are declared there.
@okomarov

Copy link
Copy Markdown
Author

Landed on main as 610a84a (the same change as 76f8c5f, with the upstream references added to the message).

@okomarov okomarov closed this Oct 13, 2026
@okomarov
okomarov deleted the feat/async-dispose branch October 13, 2026 23:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[SHIPPED in 4.5.0] await using on the pool and reserved connections

1 participant