Skip to content

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

Open
AnkitJhadev wants to merge 2 commits into
porsager:masterfrom
AnkitJhadev:fix-cursor-iterator-close-1330
Open

AnkitJhadev wants to merge 2 commits into
porsager:masterfrom
AnkitJhadev:fix-cursor-iterator-close-1330

Conversation

@AnkitJhadev

@AnkitJhadev AnkitJhadev commented Nov 16, 2026 •

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

Three tests in tests/cursors.js read 100 rows in batches of 10 and close the connection after 20, waiting for onclose before the loop continues:

  • a pooled connection, backend terminated with pg_terminate_backend (FATAL 57P01, then close)
  • a reserved connection inside begin, backend terminated the same way
  • a socket cut through a TCP proxy, so no ErrorResponse arrives (expects CONNECTION_CLOSED)

All three fail on master with 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.

@joshuavivo

Copy link
Copy Markdown
Contributor

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.)

@AnkitJhadev

Copy link
Copy Markdown
Author

Thanks @joshuavivo, that's a really thorough check, and it's great to see both close paths confirmed on current master, including next() after the error and the pool staying usable.

You're right that both tests only exercise the ErrorResponse path via pg_terminate_backend. I'd like to add a test for the socket drop with no ErrorResponse. If you could share your TCP proxy probe, that would be really helpful. I'll add the test to this PR (waiting for onclose before resuming the loop, as @lukasberbuer suggested), check that it fails on master and passes with the fix, and update the description.

Thanks again for taking the time to test this!

…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.
…rResponse

The two earlier tests terminate the backend, so the server sends a FATAL
ErrorResponse before the connection closes. This one cuts the socket
through a TCP proxy between batches, so no ErrorResponse arrives, and
expects CONNECTION_CLOSED instead of a silent partial result.
@AnkitJhadev
AnkitJhadev force-pushed the fix-cursor-iterator-close-1330 branch from c237f83 to 9e4e152 Compare November 23, 2026 05:28
@AnkitJhadev

Copy link
Copy Markdown
Author

Hi @porsager, I've added a third test for the socket drop with no ErrorResponse that Joshua suggested, so both close paths are covered. The CI run needs your approval since this is my first PR here. Happy to change anything if you'd like a different approach.

@joshuavivo

Copy link
Copy Markdown
Contributor

Checked the new test at 9e4e152 on Postgres 16. It covers the socket drop I described, so you don't need my proxy probe. With only the test file added to master it fails 3/3 ('CONNECTION_CLOSED 20' != 'no error 20'), and Postgres logs an unexpected EOF with no FATAL, so it does take the no-ErrorResponse path. With the fix it passes 3/3, as do the two pg_terminate_backend tests, also when merged onto current master. The wider integration suite has the same failures on master and on this PR, none in the cursor tests, all from my local setup.

One small thing, not a blocker and not something for this PR: an error that arrives while the consumer is inside the loop body is held until the next next(), and return() doesn't check it, so a break at that moment drops it. In a quick script, cutting the socket and then breaking out of the loop returned with no error, on master and on this PR alike, so it isn't a regression. It might be worth a follow-up issue.

Written with AI assistance (Claude ran the tests and drafted this comment).

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