Repository navigation
Cursor async iterator rejects when its connection closes between batches - fixes #1330 - #1331
Open
AnkitJhadev wants to merge 1 commit into
Open
AnkitJhadev wants to merge 1 commit into
AnkitJhadev wants to merge 1 commit into
Conversation
…hes - fixes porsager#1330 Each next() of the iterator gave the query a reject tied to its own promise, which had already resolved with the last batch while the loop body ran. An error from the connection closing then was lost, and the following next() returned done, so `for await … of query.cursor(n)` ended with a partial result and no error. The error is now kept and thrown from the following next(), as cursor(n, fn) rejects with it. Same bug as porsager#1166, which still reproduced on 4.1.1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
I ran this against current master (it merges cleanly onto 9697f3e) on PostgreSQL 16 to check both close paths discussed in #1330:
So it covers both callers of the stale (I used AI assistance to run this.) |
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 #1330. Same bug as #1166, which was closed by the v4 rewrite but still reproduces on 4.1.1.
Cause
Each
next()of the iterator fromquery.cursor(n)replacedthis.rejectwith one tied to its own promise. While the loop body runs, that promise has already resolved with the last batch, so when the connection closes,connection.jscalls arejectthat does nothing. The error is lost, and the followingnext()returns{ done: true }onexecuted && !active.for await … of query.cursor(n)then ends with a partial result and no error, whilecursor(n, fn)rejects as expected.Fix
src/query.js: eachnext()notes whether it is still waiting for a batch. Ifrejectruns after its promise has resolved, the error is kept, and the followingnext()throws it. An error while waiting still rejects at once, as before. The callback form,connection.jsandpool.jsare unchanged.Tests
Two tests in
tests/cursors.jsread 100 rows in batches of 10 and terminate the backend withpg_terminate_backendafter 20:beginBoth fail on
masterwith'57P01 20' != 'no error 20'and pass with the fix. (A cursor insidesql.beginalready rejected, because theCOMMITfails, so the second test usessql.reserve().)Run locally against PostgreSQL 17:
/tmp, which isn't available in my Docker setup and fail the same way onmasternpm run test:unit,node tests/simulate.js(2000 seeds) andeslint --max-warnings 0 src testspassA changelog entry is added under Unreleased.
🤖 Generated with Claude Code