forked from brianc/node-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
fix(pg-cursor): settle read() when rowCount exceeds remaining rows #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dyk1454683243-sudo
wants to merge
1
commit into
master
Choose a base branch
from
cursor/fix-cursor-read-hang-2949-a551
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+198
−14
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| const assert = require('assert') | ||
| const EventEmitter = require('events') | ||
| const Cursor = require('../') | ||
| const pg = require('pg') | ||
|
|
||
| class TestConnection extends EventEmitter { | ||
| constructor() { | ||
| super() | ||
| this.calls = { | ||
| close: 0, | ||
| execute: 0, | ||
| sync: 0, | ||
| } | ||
| } | ||
|
|
||
| parse() {} | ||
| bind() {} | ||
| describe() {} | ||
| flush() {} | ||
|
|
||
| execute() { | ||
| this.calls.execute++ | ||
| } | ||
|
|
||
| close() { | ||
| this.calls.close++ | ||
| } | ||
|
|
||
| sync() { | ||
| this.calls.sync++ | ||
| } | ||
| } | ||
|
|
||
| const INT4 = { name: 'num', dataTypeID: 23 } | ||
|
|
||
| const pushIntRows = (cursor, values) => { | ||
| for (const value of values) { | ||
| cursor.handleDataRow({ fields: [String(value)] }) | ||
| } | ||
| } | ||
|
|
||
| const settleOrTimeout = (promise, ms = 150) => | ||
| Promise.race([ | ||
| promise.then((value) => ({ value })), | ||
| new Promise((resolve) => setTimeout(() => resolve({ timeout: true }), ms)), | ||
| ]) | ||
|
|
||
| describe('read() when rowCount exceeds remaining rows (#2949)', function () { | ||
| it('settles as soon as the portal is exhausted', async function () { | ||
| const cursor = new Cursor('SELECT num FROM generate_series(1, 2) num') | ||
| const connection = new TestConnection() | ||
| cursor.submit(connection) | ||
|
|
||
| const read = cursor.read(1000) | ||
| cursor.handleRowDescription({ fields: [INT4] }) | ||
| pushIntRows(cursor, [1, 2]) | ||
| cursor.handleCommandComplete({ text: 'SELECT 2' }) | ||
|
|
||
| const outcome = await settleOrTimeout(read) | ||
| assert.ok(!outcome.timeout, 'read() hung after CommandComplete when rowCount exceeded remaining rows') | ||
| assert.deepStrictEqual(outcome.value, [{ num: 1 }, { num: 2 }]) | ||
| assert.strictEqual(cursor.state, 'done') | ||
| assert.deepStrictEqual(connection.calls, { close: 1, execute: 1, sync: 1 }) | ||
| }) | ||
|
|
||
| it('returns a shorter batch when only some rows remain', async function () { | ||
| const cursor = new Cursor('SELECT num FROM generate_series(1, 5) num') | ||
| const connection = new TestConnection() | ||
| cursor.submit(connection) | ||
| cursor.handleRowDescription({ fields: [INT4] }) | ||
|
|
||
| const first = cursor.read(3) | ||
| pushIntRows(cursor, [1, 2, 3]) | ||
| cursor.handlePortalSuspended() | ||
| assert.deepStrictEqual(await first, [{ num: 1 }, { num: 2 }, { num: 3 }]) | ||
|
|
||
| const remaining = cursor.read(1000) | ||
| pushIntRows(cursor, [4, 5]) | ||
| cursor.handleCommandComplete({ text: 'SELECT 5' }) | ||
|
|
||
| const outcome = await settleOrTimeout(remaining) | ||
| assert.ok(!outcome.timeout, 'read() hung when fetching more rows than remained') | ||
| assert.deepStrictEqual(outcome.value, [{ num: 4 }, { num: 5 }]) | ||
| }) | ||
|
|
||
| it('settles queued reads with leftover or empty rows once the portal is exhausted', async function () { | ||
| const cursor = new Cursor('SELECT num FROM generate_series(1, 2) num') | ||
| const connection = new TestConnection() | ||
| cursor.submit(connection) | ||
|
|
||
| const first = cursor.read(1000) | ||
| const queued = cursor.read(1000) | ||
| cursor.handleRowDescription({ fields: [INT4] }) | ||
| pushIntRows(cursor, [1, 2]) | ||
| cursor.handleCommandComplete({ text: 'SELECT 2' }) | ||
|
|
||
| const firstOutcome = await settleOrTimeout(first) | ||
| const queuedOutcome = await settleOrTimeout(queued) | ||
| assert.ok(!firstOutcome.timeout, 'in-flight read hung after the portal was exhausted') | ||
| assert.ok(!queuedOutcome.timeout, 'queued read hung after the portal was exhausted') | ||
| assert.deepStrictEqual(firstOutcome.value, [{ num: 1 }, { num: 2 }]) | ||
| assert.deepStrictEqual(queuedOutcome.value, []) | ||
| }) | ||
|
|
||
| it('keeps an in-flight oversized read when row description arrives late', async function () { | ||
| const cursor = new Cursor('SELECT num FROM generate_series(1, 2) num') | ||
| const connection = new TestConnection() | ||
| cursor.submit(connection) | ||
|
|
||
| const first = cursor.read(1000) | ||
| const queued = cursor.read(5) | ||
| cursor.handleRowDescription({ fields: [INT4] }) | ||
| pushIntRows(cursor, [1, 2]) | ||
| cursor.handleCommandComplete({ text: 'SELECT 2' }) | ||
|
|
||
| const firstOutcome = await settleOrTimeout(first) | ||
| const queuedOutcome = await settleOrTimeout(queued) | ||
| assert.ok(!firstOutcome.timeout, 'in-flight read was overwritten when row description arrived') | ||
| assert.ok(!queuedOutcome.timeout, 'queued read hung after a late row description') | ||
| assert.deepStrictEqual(firstOutcome.value, [{ num: 1 }, { num: 2 }]) | ||
| assert.deepStrictEqual(queuedOutcome.value, []) | ||
| assert.strictEqual(connection.calls.execute, 1) | ||
| }) | ||
| }) | ||
|
|
||
| describe('read() against PostgreSQL when rowCount exceeds remaining rows (#2949)', function () { | ||
| beforeEach(function (done) { | ||
| const client = (this.client = new pg.Client()) | ||
| client.connect(done) | ||
| }) | ||
|
|
||
| afterEach(function () { | ||
| this.client.end() | ||
| }) | ||
|
|
||
| it('returns all 100 rows when asked for 1000', function (done) { | ||
| const cursor = this.client.query(new Cursor('SELECT generate_series as num FROM generate_series(1, 100)')) | ||
| cursor.read(1000, function (err, rows) { | ||
| assert.ifError(err) | ||
| assert.strictEqual(rows.length, 100) | ||
| assert.strictEqual(rows[0].num, 1) | ||
| assert.strictEqual(rows[99].num, 100) | ||
| cursor.read(1000, function (err, empty) { | ||
| assert.ifError(err) | ||
| assert.strictEqual(empty.length, 0) | ||
| done() | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| it('returns the leftover rows after a partial page', async function () { | ||
| const cursor = this.client.query(new Cursor('SELECT generate_series as num FROM generate_series(1, 100)')) | ||
| const first = await cursor.read(40) | ||
| assert.strictEqual(first.length, 40) | ||
| const remaining = await cursor.read(1000) | ||
| assert.strictEqual(remaining.length, 60) | ||
| assert.strictEqual(remaining[0].num, 41) | ||
| assert.strictEqual(remaining[59].num, 100) | ||
| assert.deepStrictEqual(await cursor.read(1000), []) | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an empty query such as a comment has two reads issued before the first completes,
handleEmptyQuery()leads directly tohandleReadyForQuery()without a precedingCommandComplete. 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 👍 / 👎.