Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/pg/lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@ class Query extends EventEmitter {
connection.close({ type: 'S', name: this.name })
connection.sync()

this.handleError(err, connection)
// defer the error until readyForQuery so the query settles exactly
// once; reporting it here would let handleReadyForQuery settle the
// same query again as a success when the server answers our sync
this._canceledDueToError = err
return
}

Expand Down
23 changes: 23 additions & 0 deletions packages/pg/test/unit/client/throw-in-bind-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ suite.test('calls callback with error when bind throws', function (done) {
},
})
)
// the server answers the sync sent after the failed bind
con.emit('readyForQuery')
})

suite.test('emits error event when bind throws (no callback)', function (done) {
Expand All @@ -67,6 +69,7 @@ suite.test('emits error event when bind throws (no callback)', function (done) {
done()
})
client.query(query)
con.emit('readyForQuery')
})

suite.test('send close when bind throws', function (done) {
Expand All @@ -83,4 +86,24 @@ suite.test('send close when bind throws', function (done) {
},
})
)
con.emit('readyForQuery')
})

suite.test('settles the query exactly once when bind throws', function (done) {
const { client, con } = setupClient()
con.emit('readyForQuery')
let calls = 0
client.query(
new Query({
text: 'select $1',
values: ['x'],
callback: function () {
calls++
},
})
)
// the server answers the sync sent after the failed bind
con.emit('readyForQuery')
assert.equal(calls, 1, 'callback should be called exactly once')
done()
})
Loading