From 11d39c6f9ee1679435cf7642b5f0a42493e66521 Mon Sep 17 00:00:00 2001 From: Charmander <~@charmander.me> Date: Tue, 4 Oct 2022 23:42:58 -0700 Subject: [PATCH 1/2] pg-cursor: Add failing test for errors on queued reads --- packages/pg-cursor/test/error-handling.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/pg-cursor/test/error-handling.js b/packages/pg-cursor/test/error-handling.js index f6edef6d5..22620bd83 100644 --- a/packages/pg-cursor/test/error-handling.js +++ b/packages/pg-cursor/test/error-handling.js @@ -19,6 +19,23 @@ describe('error handling', function () { }) }) }) + + it('errors queued reads', async () => { + const client = new pg.Client() + await client.connect() + + const cursor = client.query(new Cursor('asdfdffsdf')) + + const immediateRead = cursor.read(1) + const queuedRead1 = cursor.read(1) + const queuedRead2 = cursor.read(1) + + assert(await immediateRead.then(() => null, (err) => err)) + assert(await queuedRead1.then(() => null, (err) => err)) + assert(await queuedRead2.then(() => null, (err) => err)) + + client.end() + }) }) describe('read callback does not fire sync', () => { From 8a5747ffb589695eabda7f75f7545cf64d14cc27 Mon Sep 17 00:00:00 2001 From: Charmander <~@charmander.me> Date: Wed, 5 Oct 2022 00:09:53 -0700 Subject: [PATCH 2/2] pg-cursor: Fix errors being sent to only half the queue --- packages/pg-cursor/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/pg-cursor/index.js b/packages/pg-cursor/index.js index 9bbda641a..d3c0266b0 100644 --- a/packages/pg-cursor/index.js +++ b/packages/pg-cursor/index.js @@ -171,8 +171,10 @@ class Cursor extends EventEmitter { } // dispatch error to all waiting callbacks for (let i = 0; i < this._queue.length; i++) { - this._queue.pop()[1](msg) + const queuedCallback = this._queue[i][1] + queuedCallback.call(this, msg) } + this._queue.length = 0 if (this.listenerCount('error') > 0) { // only dispatch error events if we have a listener