Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2556 by keeping callback errors from interfering with cleanup #2753

Merged
merged 2 commits into from Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/pg/lib/query.js
Expand Up @@ -135,7 +135,14 @@ class Query extends EventEmitter {
return this.handleError(this._canceledDueToError, con)
}
if (this.callback) {
this.callback(null, this._results)
try {
this.callback(null, this._results)
}
catch(err) {
process.nextTick(() => {
throw err
})
}
}
this.emit('end', this._results)
}
Expand Down
40 changes: 40 additions & 0 deletions packages/pg/test/integration/gh-issues/2556-tests.js
@@ -0,0 +1,40 @@
'use strict'
var helper = require('./../test-helper')
var assert = require('assert')

var callbackError = new Error('TEST: Throw in callback')

const suite = new helper.Suite()

suite.test('it should cleanup client even if an error is thrown in a callback', (done) => {
// temporarily replace the test framework's uncaughtException handlers
// with a custom one that ignores the callbackError
let original_handlers = process.listeners('uncaughtException')
process.removeAllListeners('uncaughtException')
process.on('uncaughtException', (err) => {
if (err != callbackError) {
original_handlers[0](err)
}
})

// throw an error in a callback and verify that a subsequent query works without error
var client = helper.client()
client.query('SELECT NOW()', (err) => {
assert(!err)
setTimeout(reuseClient, 50)
throw callbackError
})

function reuseClient() {
client.query('SELECT NOW()', (err) => {
assert(!err)

// restore the test framework's uncaughtException handlers
for (let handler of original_handlers) {
process.on('uncaughtException', handler)
}

client.end(done)
})
}
})