Skip to content

fix(pg-cursor): settle read() when rowCount exceeds remaining rows - #3786

Open
dyk1454683243-sudo wants to merge 1 commit into
brianc:masterfrom
dyk1454683243-sudo:cursor/fix-cursor-read-hang-2949-a551
Open

dyk1454683243-sudo wants to merge 1 commit into
brianc:masterfrom
dyk1454683243-sudo:cursor/fix-cursor-read-hang-2949-a551

Conversation

@dyk1454683243-sudo

Copy link
Copy Markdown

Fixes #2949

Problem

cursor.read(rowCount) never called its callback when rowCount was larger than the remaining (or total) rows. A table with 100 rows and read(1000) hung instead of returning the leftover rows.

PostgreSQL reports portal exhaustion with CommandComplete, not PortalSuspended. pg-cursor only delivered the current batch from PortalSuspended or ReadyForQuery. After CommandComplete the cursor closed the portal and sent Sync, then waited for ReadyForQuery. If that message was never dispatched to the cursor, read() never settled.

A late RowDescription could also reset a busy cursor to idle and start a queued execute, overwriting the in-flight callback. Queued read() calls were never drained when the portal was exhausted.

Fix

In packages/pg-cursor:

  • Deliver the in-flight read() as soon as CommandComplete arrives (shorter or empty batch).
  • Resolve any queued reads with [] once the portal is exhausted.
  • Do not start a queued execute from RowDescription / noData while a read is already in flight.
  • Clear the current callback synchronously so a later ReadyForQuery cannot deliver the same batch twice.

Tests

packages/pg-cursor/test/read-exhaustion.js:

  • Unit: oversized read() settles on CommandComplete without ReadyForQuery.
  • Unit: leftover rows after a partial page, then an oversized read().
  • Unit: queued reads settle with [] after exhaustion.
  • Unit: late RowDescription does not overwrite an in-flight oversized read.
  • Integration: 100 rows, read(1000) returns 100, then [].
  • Integration: read(40) then read(1000) returns the remaining 60.

Claim check

Searched open and recently updated PRs on brianc/node-postgres and dyk1454683243-sudo/node-postgres for #2949, cursor.read hang, and rowCount. Issue #2949 is still open with no linked closer. No existing PR owns this fix.

Verification

  • packages/pg-cursor: 49 passing (including the new cases)
  • packages/pg-query-stream: 40 passing
  • eslint on the changed files: clean

CommandComplete means the portal is exhausted, but read() only
delivered rows from PortalSuspended or ReadyForQuery. If the client
never dispatched ReadyForQuery, an oversized read hung.

Settle the in-flight callback (and any queued reads) when the portal
is exhausted, and do not start a queued execute while one is already
in flight.

Co-authored-by: David <dyk1454683243-sudo@users.noreply.github.com>
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.read(rowCount) will hang if rowCount larger than total rows' size

2 participants