fix(node-postgres): release pool client when BEGIN rejects in transaction()#6032
Open
allocsys wants to merge 1 commit into
Open
fix(node-postgres): release pool client when BEGIN rejects in transaction()#6032allocsys wants to merge 1 commit into
allocsys wants to merge 1 commit into
Conversation
…tion() BEGIN previously ran before the try/finally, so a rejected BEGIN (e.g. connection dropped right after pool.connect()) skipped both the catch (ROLLBACK) and the finally (release()), leaking the checked-out pool client. Repeated failures could exhaust the pool. Moves BEGIN inside the try/finally and tracks whether it succeeded via a `began` flag, only attempting ROLLBACK if a transaction actually started. Client is now released on every exit path once pool.connect() has succeeded. Matches the existing fix already present in the 0.x line's node-postgres session.ts. Fixes drizzle-team#6023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #6023
Problem
NodePgSession.transaction()checks a client out of apg.Pool, then runsBEGINbefore entering thetry/finallythat callsrelease(). IfBEGINrejects (e.g. the connection drops right after checkout), control skips both thecatch(which wouldROLLBACK) and thefinally(which releases the client) — the checked-out client is never returned to the pool. Repeated failures can exhaust it.Fix
Move
BEGINinside the existingtry/finallyand track whether it actually succeeded via abeganflag, soROLLBACKis only attempted once a transaction has genuinely started, whilerelease()still runs on every exit path:Verification
Ran the minimal repro from the issue (a
FailingPool/client stub whosequeryalways rejects, simulating a dropped connection onBEGIN) directly against thesrcin both states:main:released: 0— leak reproducesreleased: 1— client released, no leakNote: the currently-published
drizzle-orm@0.45.2(npmlatest) already contains an equivalent guard, so current stable users aren't affected — this bringsmainin line with that existing fix. Left more detail on this in the issue thread.Scope note
Nested transactions (
NodePgTransaction.transaction(), the savepoint path) have a similar "no guard around the initial statement" shape, but there's no pool client to leak there since it reuses the existing session — left out of this PR to keep it minimal/single-purpose. Happy to follow up separately if maintainers want that covered too.