fix(pg-cursor): settle read() when rowCount exceeds remaining rows - #1
dyk1454683243-sudo wants to merge 1 commit into
Conversation
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>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d9e4e430ac
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (this.state === 'idle') { | ||
| this._shiftQueue() |
There was a problem hiding this comment.
Do not execute queued reads while handling ReadyForQuery
When an empty query such as a comment has two reads issued before the first completes, handleEmptyQuery() leads directly to handleReadyForQuery() without a preceding CommandComplete. This call therefore changes the busy cursor to idle and promotes the second read here, sending another Execute; handleReadyForQuery() then immediately marks the cursor done and removes it as the client's active query. The promoted callback has already been removed from _queue, so it hangs, and its eventual backend response is handled as an unexpected message. Terminal calls to _sendRows() must not shift queued work, or the cursor must be marked done before calling it.
Useful? React with 👍 / 👎.
Fixes brianc#2949
Upstream PR: brianc#3786
Problem
cursor.read(rowCount)never called its callback whenrowCountwas larger than the remaining (or total) rows. A table with 100 rows andread(1000)hung instead of returning the leftover rows.PostgreSQL reports portal exhaustion with
CommandComplete, notPortalSuspended.pg-cursoronly delivered the current batch fromPortalSuspendedorReadyForQuery. AfterCommandCompletethe cursor closed the portal and sentSync, then waited forReadyForQuery. If that message was never dispatched to the cursor,read()never settled.A late
RowDescriptioncould also reset a busy cursor to idle and start a queued execute, overwriting the in-flight callback. Queuedread()calls were never drained when the portal was exhausted.Fix
In
packages/pg-cursor:read()as soon asCommandCompletearrives (shorter or empty batch).[]once the portal is exhausted.RowDescription/noDatawhile a read is already in flight.ReadyForQuerycannot deliver the same batch twice.Tests
packages/pg-cursor/test/read-exhaustion.js:read()settles onCommandCompletewithoutReadyForQuery.read().[]after exhaustion.RowDescriptiondoes not overwrite an in-flight oversized read.read(1000)returns 100, then[].read(40)thenread(1000)returns the remaining 60.Verification
packages/pg-cursor: 49 passing (including the new cases)packages/pg-query-stream: 40 passingeslinton the changed files: clean