Repository navigation
Cursor async iterator rejects when its connection closes between batches - fixes #1330 - #1331
AnkitJhadev wants to merge 2 commits into
Conversation
|
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.) |
|
Thanks @joshuavivo, that's a really thorough check, and it's great to see both close paths confirmed on current master, including You're right that both tests only exercise the ErrorResponse path via 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.
c237f83 to
9e4e152
Compare
|
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. |
|
Checked the new test at 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 Written with AI assistance (Claude ran the tests and drafted this comment). |
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
Three tests in
tests/cursors.jsread 100 rows in batches of 10 and close the connection after 20, waiting foronclosebefore the loop continues:pg_terminate_backend(FATAL57P01, then close)begin, backend terminated the same wayErrorResponsearrives (expectsCONNECTION_CLOSED)All three fail on
masterwithno error 20and 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.