Conversation
close() only short-circuited when the cursor was 'done'. Once handleError has put it in the 'error' state, close() sent a Close for the portal and waited for a readyForQuery, but no Sync follows that Close (handleError already sent one), so the wait could only end if the error's own readyForQuery had not arrived yet. When it had, close() never settled. That covers both reported cases: - the connection is lost mid-read (pg_terminate_backend, a failover): the socket is dead, so nothing will ever arrive; - an ordinary query error followed by close(), e.g. in a finally block: the error's readyForQuery has already been handled. In the 'error' state the portal no longer exists on the server (it is dropped at the Sync handleError sends, or with the connection), so there is nothing to close. Settle straight away, as for 'done'. Fixes brianc#2642
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 #2642
Cursor#close()only returns early when the cursor isdone. OncehandleErrorhas moved it toerror,close()sends a Close for the portal and waits forreadyForQuery. No Sync follows that Close (handleErroralready sent one), so the wait only ends if the error's ownreadyForQueryhas not been handled yet. When it has,close()never settles.That covers the reported case and a more common one:
pg_terminate_backend, a failover): the socket is dead, so nothing arrives;close(), for example in afinallyblock after the rejectedread().In the
errorstate the portal no longer exists on the server (it is dropped at the Sync thathandleErrorsends, or with the connection), so there is nothing to close.close()now settles straight away, as it does fordone.Tests: two cases in
packages/pg-cursor/test/close.js(close after a query error once the client has drained; close after the backend is terminated). Both time out without the change and pass with it. pg-cursor (45) and pg-query-stream (40) pass against Postgres 16, and eslint and prettier are clean.#2806 added a failing test for this issue in 2022; this PR includes a fix alongside its own tests.