Repository navigation
fix(pg): keep the connection usable after an error in a rows-limited query - #3825
Open
official-burak wants to merge 1 commit into
Open
official-burak wants to merge 1 commit into
official-burak wants to merge 1 commit into
Conversation
…query With the rows option, Query drives the extended protocol with Flush instead of Sync (_getRows) and only handleCommandComplete / handleEmptyQuery send the terminating Sync. When the server answers with an ErrorResponse instead, it discards messages until a Sync arrives and withholds ReadyForQuery - handleError sent nothing, so readyForQuery never returned and every later query on the connection queued forever. Restore the error-path Sync by tracking the one Sync a rows-limited portal owes, sent by whichever of handleCommandComplete, handleEmptyQuery or handleError observes the end of the sequence. The flag also keeps exactly one Sync from going out when an ErrorResponse follows a CommandComplete (a timed-out query on some backends) or when a row-parse error is surfaced on ReadyForQuery, where a second Sync would produce a second ReadyForQuery and let a later query settle mid-flight. Fixes brianc#3707
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.
What
Fixes #3707 — an error during a query that runs with the
rowsoption permanently wedges the connection. The query rejects correctly, butReadyForQuerynever comes back and every later query on that client queues forever.Why it happens
With
rowsset,Query.prepare()drives the extended protocol with Flush, not Sync:_getRows()sendsExecute(rows=N)+connection.flush(), and again on everyPortalSuspended. The terminating Sync is only sent fromhandleCommandComplete/handleEmptyQuery. When the server answers with anErrorResponseinstead, it enters ignore-till-sync — it discards everything until a Sync arrives and withholdsReadyForQuery.handleErrorsent nothing, so the RFQ never arrives andclient.readyForQuerystaysfalse.The
// need to sync after error during a prepared statementcomment that sat at the top ofhandleErrorwas a fossil of the pre-pipelining code, which did sync there — the Oct 2020 redesign (d31486f / dd3ce61) moved Sync into_getRows()and dropped the error-path call, correct for the normal prepared path (Sync already pipelined) but not for rows-mode. pg-cursor kept itshandleErrorsync for exactly this reason, which is why cursors recover from errors and the corerowspath does not.The fix
Track the one Sync a rows-limited portal owes, and let whichever handler observes the end of the extended-protocol sequence send it:
_getRows()sets_syncOwedin the rows branch (Flush instead of Sync)_sendOwedSync()sends it and clears the flag — called fromhandleCommandComplete,handleEmptyQueryand nowhandleErrorThe flag makes the send exactly once. That matters in two spots where an error is preceded by a
CommandComplete:CommandCompleteandErrorResponse(the case 9c678e1 originally guarded) —handleCommandCompletealready synced, sohandleErrormust not send a secondhandleReadyForQueryafterCommandCompletealready synced — same thingA second Sync would produce a second
ReadyForQuery, letting the next queued query settle on the first frame mid-flight — the same class of bug #2420 fixed in pg-cursor.Two side effects worth calling out, both intentional:
prepare()are unaffected: that path already sendsClose+Syncbefore callinghandleError, and since_getRows()never ran,_syncOwedis stillfalse— no double sendConnection._send()no-ops on a non-writable stream so a dead socket is safeCanceling a rows-limited query (
client.cancel()via a second client) also lands on this path — theErrorResponsefor the cancel now gets its Sync, so the client stays usable after a canceled paged query as well.Tests
Three integration tests in
query-error-handling-prepared-statement-tests.js(skipped in native mode — the fix is in the JSQuery):ReadyForQuerywas emitted for the failed sequencerowsset — the same wedge through a different entry pointVerified locally against PostgreSQL 16: the new tests hang on
master(suite timeout kills them — the wedge) and pass with the fix; the fullpgunit and integration suites show no regressions (the only local failures —password-callback-tests,ssl-tests,gh-issues/2085— fail identically onmasterin this environment).