Skip to content

Commit 68160a2

Browse files
authored
Fix #2556 by keeping callback errors from interfering with cleanup (#2753)
* Fix #2556 (handleRowDescription of null) by keeping callback errors from interfering with cleanup * Added regression test for #2556
1 parent 28ac2a1 commit 68160a2

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

packages/pg/lib/query.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,14 @@ class Query extends EventEmitter {
135135
return this.handleError(this._canceledDueToError, con)
136136
}
137137
if (this.callback) {
138-
this.callback(null, this._results)
138+
try {
139+
this.callback(null, this._results)
140+
}
141+
catch(err) {
142+
process.nextTick(() => {
143+
throw err
144+
})
145+
}
139146
}
140147
this.emit('end', this._results)
141148
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict'
2+
var helper = require('./../test-helper')
3+
var assert = require('assert')
4+
5+
var callbackError = new Error('TEST: Throw in callback')
6+
7+
const suite = new helper.Suite()
8+
9+
suite.test('it should cleanup client even if an error is thrown in a callback', (done) => {
10+
// temporarily replace the test framework's uncaughtException handlers
11+
// with a custom one that ignores the callbackError
12+
let original_handlers = process.listeners('uncaughtException')
13+
process.removeAllListeners('uncaughtException')
14+
process.on('uncaughtException', (err) => {
15+
if (err != callbackError) {
16+
original_handlers[0](err)
17+
}
18+
})
19+
20+
// throw an error in a callback and verify that a subsequent query works without error
21+
var client = helper.client()
22+
client.query('SELECT NOW()', (err) => {
23+
assert(!err)
24+
setTimeout(reuseClient, 50)
25+
throw callbackError
26+
})
27+
28+
function reuseClient() {
29+
client.query('SELECT NOW()', (err) => {
30+
assert(!err)
31+
32+
// restore the test framework's uncaughtException handlers
33+
for (let handler of original_handlers) {
34+
process.on('uncaughtException', handler)
35+
}
36+
37+
client.end(done)
38+
})
39+
}
40+
})

0 commit comments

Comments
 (0)