Skip to content

Commit c28febe

Browse files
committed
Fix native client end() to wait for in-flight pipeline queries
The native client end() was immediately terminating the connection, causing "Connection terminated" errors for queries still in the pipeline. Now waits for the drain event before closing when pipelining is active. Also fix pipeline mode to use sendQueryParams instead of sendQuery, since PostgreSQL rejects simple query protocol in pipeline mode.
1 parent 141d3bc commit c28febe

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

packages/pg/lib/native/client.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,25 @@ Client.prototype.end = function (cb) {
261261
})
262262
}
263263

264-
this.native.end(function () {
265-
self._connected = false
264+
const doEnd = function () {
265+
self.native.end(function () {
266+
self._connected = false
266267

267-
self._errorAllQueries(new Error('Connection terminated'))
268+
self._errorAllQueries(new Error('Connection terminated'))
268269

269-
process.nextTick(() => {
270-
self.emit('end')
271-
if (cb) cb()
270+
process.nextTick(() => {
271+
self.emit('end')
272+
if (cb) cb()
273+
})
272274
})
273-
})
275+
}
276+
277+
// If pipelining has in-flight or queued queries, wait for them to drain before closing
278+
if (this.pipelining && (this._pipeliningInFlight || this._queryQueue.length > 0)) {
279+
this.once('drain', doEnd)
280+
} else {
281+
doEnd()
282+
}
274283
return result
275284
}
276285

0 commit comments

Comments
 (0)