fix: prevent UNSAFE_TRANSACTION under concurrent sql.begin() - #1207
Open
dominikwagner wants to merge 1 commit into
Open
fix: prevent UNSAFE_TRANSACTION under concurrent sql.begin()#1207dominikwagner wants to merge 1 commit into
dominikwagner wants to merge 1 commit into
Conversation
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 the
UNSAFE_TRANSACTIONrace reported in #823 (likely related to #827).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: