From d1de1e573799f9f75f6026e9f3a048c31a079d0b Mon Sep 17 00:00:00 2001 From: Dev M Date: Wed, 9 Sep 2026 22:35:54 +0000 Subject: [PATCH] fix(pg): do not treat Sync as connection ending Connection.sync() was setting _ending=true on every extended-query Sync. That flag is meant for disconnect (Terminate / end()), so after the first parameterized query ECONNRESET and EPIPE were swallowed for the life of the connection. Keep _ending only on end() and connect-timeout teardown. Fixes #3769 --- packages/pg/lib/connection.js | 3 ++- .../pg/test/unit/connection/error-tests.js | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/pg/lib/connection.js b/packages/pg/lib/connection.js index 62d38fa69..4d4767abb 100644 --- a/packages/pg/lib/connection.js +++ b/packages/pg/lib/connection.js @@ -195,7 +195,8 @@ class Connection extends EventEmitter { } sync() { - this._ending = true + // Sync is the extended-query protocol barrier, not a disconnect. + // Only end()/Terminate (and connect-timeout teardown) should set _ending. this._send(syncBuffer) } diff --git a/packages/pg/test/unit/connection/error-tests.js b/packages/pg/test/unit/connection/error-tests.js index 04f1c3f4b..71ca36566 100644 --- a/packages/pg/test/unit/connection/error-tests.js +++ b/packages/pg/test/unit/connection/error-tests.js @@ -29,6 +29,28 @@ suite.test('connection emits ECONNRESET errors during normal operation', functio con.stream.emit('error', e) }) +suite.test('connection emits ECONNRESET errors after Sync (Sync is not disconnect)', function (done) { + const con = new Connection({ stream: new MemoryStream() }) + con.connect() + // Extended-query Sync used to incorrectly set _ending and swallow resets (#3769) + con.sync() + assert.equal(con._ending, false) + assert.emits(con, 'error', function (err) { + assert.equal(err.code, 'ECONNRESET') + done() + }) + const e = new Error('Connection Reset') + e.code = 'ECONNRESET' + con.stream.emit('error', e) +}) + +suite.test('connection does not set _ending when calling sync()', function () { + const con = new Connection({ stream: new MemoryStream() }) + con.connect() + con.sync() + assert.equal(con._ending, false) +}) + suite.test('connection does not emit ECONNRESET errors during disconnect', function (done) { const con = new Connection({ stream: new MemoryStream() }) con.connect()