Skip to content

Commit 46cdf9e

Browse files
uap-devcharmander
andauthored
[fix] fix unhandled callback error for submittables (#3589)
* fixes bug where submittable without callback throws error * add querystream to test remove comments * fix test * Revert unrelated reformatting --------- Co-authored-by: Charmander <[email protected]>
1 parent 7472fbd commit 46cdf9e

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

packages/pg-query-stream/test/error.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,24 @@ describe('error recovery', () => {
170170
conn.release()
171171
await pool.end()
172172
})
173+
174+
it('does not crash when query_timeout fires without callback', async () => {
175+
const client = new Client({ query_timeout: 50 })
176+
await client.connect()
177+
178+
const stream = new QueryStream('SELECT pg_sleep(10)')
179+
180+
// cursor.on('error') fires synchronously when handleError is called
181+
// stream.on('error') fires asynchronously via destroy()
182+
// Use cursor for immediate error detection
183+
const errorPromise = new Promise<Error>((resolve) => {
184+
stream.cursor.on('error', resolve)
185+
})
186+
187+
client.query(stream)
188+
189+
const error = await errorPromise
190+
assert.equal(error.message, 'Query read timeout')
191+
await client.end()
192+
})
173193
})

packages/pg/lib/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ class Client extends EventEmitter {
628628
}
629629

630630
if (readTimeout) {
631-
queryCallback = query.callback
631+
queryCallback = query.callback || (() => {})
632632

633633
readTimeoutTimer = setTimeout(() => {
634634
const error = new Error('Query read timeout')

packages/pg/lib/native/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ Client.prototype.query = function (config, values, callback) {
184184
}
185185

186186
if (readTimeout) {
187-
queryCallback = query.callback
187+
queryCallback = query.callback || (() => {})
188188

189189
readTimeoutTimer = setTimeout(() => {
190190
const error = new Error('Query read timeout')
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict'
2+
3+
const helper = require('./test-helper')
4+
const Query = require('../../../lib/query')
5+
const assert = require('assert')
6+
const suite = new helper.Suite()
7+
const test = suite.test.bind(suite)
8+
9+
test('query timeout with Submittable without callback delivers error via handleError', function (done) {
10+
const client = helper.client()
11+
client.connectionParameters = { query_timeout: 10 }
12+
13+
const query = new Query({ text: 'SELECT 1' })
14+
query.handleError = (err) => {
15+
assert.equal(err.message, 'Query read timeout')
16+
done()
17+
}
18+
19+
client.connection.emit('readyForQuery')
20+
client.query(query)
21+
})
22+
23+
test('query timeout with Submittable with callback delivers error via callback', function (done) {
24+
const client = helper.client()
25+
client.connectionParameters = { query_timeout: 10 }
26+
27+
const query = new Query({ text: 'SELECT 1' })
28+
client.connection.emit('readyForQuery')
29+
30+
client.query(query, (err) => {
31+
assert.equal(err.message, 'Query read timeout')
32+
done()
33+
})
34+
})

0 commit comments

Comments
 (0)