Skip to content

fix(node-postgres): release pool client when transaction BEGIN rejects#6030

Open
xianjianlf2 wants to merge 1 commit into
drizzle-team:mainfrom
xianjianlf2:fix/node-postgres-release-on-begin-failure-6023
Open

fix(node-postgres): release pool client when transaction BEGIN rejects#6030
xianjianlf2 wants to merge 1 commit into
drizzle-team:mainfrom
xianjianlf2:fix/node-postgres-release-on-begin-failure-6023

Conversation

@xianjianlf2

Copy link
Copy Markdown

Problem

With the node-postgres driver, a transaction whose BEGIN statement rejects (e.g. the connection dies right after checkout, or a server-side error) leaks the checked-out pool client: in node-postgres/session.ts, await tx.execute(sql\begin...`)runs *before* thetry/finallywhosefinallyreleases the client, so a rejectingBEGINskipsrelease()` entirely. Under load this exhausts the pool.

Closes #6023

Fix

Move the BEGIN inside the outer try/finally so the client is always released, while keeping ROLLBACK gated on BEGIN having succeeded (no rollback is attempted for a transaction that never started).

Testing

New DB-free tests/node-postgres-transaction.test.ts with a mock pool: BEGIN rejects → client released exactly once and no ROLLBACK issued (red before the fix: released: 0); commit path unchanged; callback throwing → ROLLBACK + release. Full drizzle-orm unit suite: 14 files, 569/569 passing; tsc --noEmit and dprint check clean.

Move the BEGIN statement inside the try/finally that releases the
checked-out pool client. Previously, if BEGIN rejected (e.g. the
connection was interrupted right after checkout), control never reached
the finally block, so the client was never returned to the pool and
repeated failures could exhaust it. ROLLBACK is still only issued after
BEGIN has succeeded.

Closes drizzle-team#6023
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.

[BUG]: node-postgres pool client is not released when transaction BEGIN rejects

1 participant