fix: prevent UNSAFE_TRANSACTION under concurrent sql.begin() - #1206
Closed
dominikwagner wants to merge 1 commit into
Closed
dominikwagner wants to merge 1 commit into
dominikwagner wants to merge 1 commit into
Conversation
Three code paths in src/connection.js and src/index.js can clear or overwrite connection.reserved while BEGIN is in flight, causing a second sql.begin() to send BEGIN on the same connection. PostgreSQL warns 25001 'there is already a transaction in progress' and the driver throws UNSAFE_TRANSACTION. - execute(): fire onexecute unconditionally when write() succeeds, and return false so no more queries pipeline onto the connection - drain(): don't call onopen() on a reserved connection - handler(): always queue onexecute queries (BEGIN) instead of dispatching onto busy connections Reproduction in tests/race-condition.js: pool max: 3, max_pipeline: 2, hold all 3 connections busy with pg_sleep, fire 6 regular queries + 5 sql.begin() concurrently, repeat 20 times. Before: 12-16 iterations fail with 25001 warning from PostgreSQL. After: 20/20 pass. Fixes porsager#823
Author
|
Sorry, opened by mistake. Will re-submit properly. |
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.
Root cause
When you call
sql.begin(), the driver:BEGINto PostgreSQLCOMMITand un-reserves the connectionUnder concurrent load, the "reserved" mark can be lost or overwritten before step 4 finishes. Another
sql.begin()grabs the same connection and sends its ownBEGIN. PostgreSQL sees a secondBEGINwhile already in a transaction, warns25001"there is already a transaction in progress", and the driver throwsUNSAFE_TRANSACTION.Three code paths in
src/connection.jsandsrc/index.jscan produce this:execute()short-circuit —onexecute(which setsconnection.reserved) is at the tail of an&&chain. Whensent.length >= max_pipeline(orwrite()returns false), the chain short-circuits andonexecuteis skipped, so the connection is never marked reserved.handler()pipelining — a queuedBEGINcan be dispatched onto a busy connection that already holds another transaction.drain()on reserved connection — the socket drain callback can callonopen()on a connection that is currently reserved, returning it to the pool mid-transaction.Fix
Three small, focused changes:
src/connection.jsexecute()onexecuteunconditionally whenwrite()succeeds, and returnfalseso the connection is marked full (no further pipelining onto it).src/connection.jsdrain()onopen()on a reserved connection.src/index.jshandler()onexecutequery (BEGIN) onto a busy connection — always queue it.Reproduction
tests/race-condition.jsreproduces the bug against unmodified PostgreSQL on localhost. It runs the scenario 20 times: poolmax: 3, max_pipeline: 2, hold all 3 connections busy withpg_sleep, then fire 6 regular queries + 5sql.begin()concurrently.25001"there is already a transaction in progress"Setup: