Repository navigation
fix: reject only the query that fails to build and keep pipelined responses in order - #22
Merged
Merged
Conversation
…ponses in order submit() registers a query as active or pipelined before it builds it. When the build threw (an undefined parameter value, which raises UNDEFINED_VALUE, too many parameters, or a throwing debug callback) the catch rejected the active query instead of the one that failed, and wrote a Sync only when nothing was pipelined. With another query running, the running query received the build error, the failing query was never written and never settled, and every later response was matched to the query before it, so later queries resolved with each other's results. The catch now rejects the failing query itself. If it was queued behind another query it is dropped from the pipeline, so nothing waits for a reply that will never come, and no Sync is written. A Sync is written only when the failing query is the active query on an idle connection, where it gives that query its ReadyForQuery. Because no Sync is sent while another sequence is open, it cannot close the extended-protocol sequence of a retried describeFirst query or be ignored in copy mode. submit() now returns whether the pipeline still has room, instead of always true. Rejected: always writing a Sync for a placeholder. PostgreSQL ignores Sync during COPY FROM STDIN, so a placeholder queued behind a copy never gets its ReadyForQuery and later queries get shifted results and then hang. Inside a retried describeFirst sequence, which has no Sync of its own, the placeholder's Sync closes that sequence early. Also rejected: building before registering the query. On an idle connection nothing would then produce a ReadyForQuery, and the pool would keep the connection busy forever. A throwing debug callback on an idle connection already worked, since the failing query was the active one and its Sync gave it the ReadyForQuery. That test and the idle stats test pass on main and are regression guards. Behind a running query it hung before this change and now rejects only that query. The microtask rethrow for the other callbacks from #13 is still needed. Closes #20. Fixes porsager#1082 and porsager#729. Tests: build failure behind a running query, in the middle of a pipeline, on a reserved handle, behind copy from stdin, undefined value in a transaction and behind a running query, throwing debug idle, pipelined and on retry, stats back to idle.
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 #20: a query that fails while it is being built no longer takes another query's error or response. Addresses porsager#1082 (unsettled transaction pipeline promise on a parameter validation error) and porsager#729 (error swap between a completing and a failing query).
submit()puts a query in the connection's line before building it. When building threw, the oldcatchhad three effects:Synconly when nothing was pipelined.Later replies were then matched to the wrong queries. Triggers are an
undefinedvalue, more than 65534 parameters, or a throwingdebugcallback.The
catchis now:Syncis written only when the failing query was alone on the connection, where it provides that query'sReadyForQuery. NoSyncis ever written while another exchange is open.max_pipeline.The review rejected always writing a
Syncfor the failed query. PostgreSQL ignoresSyncduringCOPY FROM STDIN, so later queries got shifted results and then hung. Inside a retried describe-first query, it closed that exchange early.The review also found two existing paths where a connection accepts queries mid-exchange: a retry, and COPY. They are out of scope here and tracked in #21.
Local validation (Docker, Node 24.21, PostgreSQL 17.11):
main, including upstream's own Unsettled transaction pipeline promise on parameter validation error porsager/postgres#1082 reproduction (sql.beginwith${undefined}), which hangs there. The other two (a throwingdebugon an idle connection, and stats back to idle) pass onmainand stay as regression guards.Sync.