Skip to content

Cursor async iterator rejects when its connection closes between batches - fixes #1330 - #1331

Open
AnkitJhadev wants to merge 1 commit into
porsager:masterfrom
AnkitJhadev:fix-cursor-iterator-close-1330
Open

AnkitJhadev wants to merge 1 commit into
porsager:masterfrom
AnkitJhadev:fix-cursor-iterator-close-1330

Conversation

@AnkitJhadev

Copy link
Copy Markdown

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 from query.cursor(n) replaced this.reject with 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.js calls a reject that does nothing. The error is lost, and the following next() returns { done: true } on executed && !active. for await … of query.cursor(n) then ends with a partial result and no error, while cursor(n, fn) rejects as expected.

Fix

src/query.js: each next() notes whether it is still waiting for a batch. If reject runs after its promise has resolved, the error is kept, and the following next() throws it. An error while waiting still rejects at once, as before. The callback form, connection.js and pool.js are unchanged.

Tests

Two tests in tests/cursors.js read 100 rows in batches of 10 and terminate the backend with pg_terminate_backend after 20:

  • a pooled connection
  • a reserved connection inside begin

Both fail on master with '57P01 20' != 'no error 20' and pass with the fix. (A cursor inside sql.begin already rejected, because the COMMIT fails, so the second test uses sql.reserve().)

Run locally against PostgreSQL 17:

  • all cursor tests pass
  • full suite: everything passes except the 6 tests that connect through the Unix socket in /tmp, which isn't available in my Docker setup and fail the same way on master
  • npm run test:unit, node tests/simulate.js (2000 seeds) and eslint --max-warnings 0 src tests pass

A changelog entry is added under Unreleased.

🤖 Generated with Claude Code

…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>
@joshuavivo

Copy link
Copy Markdown

I ran this against current master (it merges cleanly onto 9697f3e) on PostgreSQL 16 to check both close paths discussed in #1330:

between batches master this PR
backend terminated (FATAL 57P01, then close) loop ends after 20 rows, no error throws 57P01
socket cut with no ErrorResponse (TCP proxy closed) loop ends after 20 rows, no error throws CONNECTION_CLOSED
terminated without waiting for the close throws 57P01 throws 57P01
next() again after the thrown error – done: true, pool still usable
full read, and an early break unchanged unchanged

So it covers both callers of the stale reject, including a socket drop with no ErrorResponse. Both tests in the PR use pg_terminate_backend, so that second path isn't exercised. A test for it may be worth adding, and I can share the probe. All 43 cursor tests pass on the merge.

(I used AI assistance to run this.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cursor() async iterator ends without error when the connection closes between batches

2 participants