Skip to content

Commit 5508c0e

Browse files
fix: Prevent closing the portal twice (#2609)
Fixes #2119
1 parent 998f573 commit 5508c0e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Diff for: packages/pg-cursor/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ class Cursor extends EventEmitter {
8686
}
8787

8888
_closePortal() {
89+
if (this.state === 'done') return
90+
8991
// because we opened a named portal to stream results
9092
// we need to close the same named portal. Leaving a named portal
9193
// open can lock tables for modification if inside a transaction.
@@ -97,6 +99,8 @@ class Cursor extends EventEmitter {
9799
if (this.state !== 'error') {
98100
this.connection.sync()
99101
}
102+
103+
this.state = 'done'
100104
}
101105

102106
handleRowDescription(msg) {
@@ -213,7 +217,6 @@ class Cursor extends EventEmitter {
213217
}
214218

215219
this._closePortal()
216-
this.state = 'done'
217220
this.connection.once('readyForQuery', function () {
218221
cb()
219222
})

Diff for: packages/pg-query-stream/test/async-iterator.ts

+12
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,17 @@ if (!process.version.startsWith('v8')) {
117117
client.release()
118118
await pool.end()
119119
})
120+
121+
it('supports breaking with low watermark', async function () {
122+
const pool = new pg.Pool({ max: 1 })
123+
const client = await pool.connect()
124+
125+
for await (const _ of client.query(new QueryStream('select TRUE', [], { highWaterMark: 1 }))) break
126+
for await (const _ of client.query(new QueryStream('select TRUE', [], { highWaterMark: 1 }))) break
127+
for await (const _ of client.query(new QueryStream('select TRUE', [], { highWaterMark: 1 }))) break
128+
129+
client.release()
130+
await pool.end()
131+
})
120132
})
121133
}

0 commit comments

Comments
 (0)