Skip to content

Commit 8c2e17d

Browse files
committed
Fix end timeout race condition (backport)
1 parent 7d9fea9 commit 8c2e17d

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

lib/connection.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ function Connection(options = {}) {
7979
}
8080

8181
function destroy() {
82-
error(errors.connection('DESTROYED', options))
82+
const err = errors.connection('DESTROYED', options)
83+
backend.query && backend.query.reject(err)
84+
error(err)
8385
socket.destroy()
8486
}
8587

lib/index.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,10 @@ function Postgres(a, b) {
322322

323323
let destroy
324324

325-
if (timeout === 0)
326-
return ended = Promise.all(all.map(c => c.destroy())).then(() => undefined)
327-
328325
return ended = Promise.race([
329326
Promise.all(all.map(c => c.end()))
330327
].concat(
331-
timeout > 0
328+
timeout === 0 || timeout > 0
332329
? new Promise(r => destroy = setTimeout(() => (all.map(c => c.destroy()), r()), timeout * 1000))
333330
: []
334331
))

tests/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,13 @@ t('Connection destroyed', async() => {
419419
return ['CONNECTION_DESTROYED', await sql``.catch(x => x.code)]
420420
})
421421

422+
t('Connection destroyed with query before', async() => {
423+
const sql = postgres(options)
424+
let error = sql`select pg_sleep(0.2)`.catch(err => err.code)
425+
sql.end({ timeout: 0 })
426+
return ['CONNECTION_DESTROYED', await error]
427+
})
428+
422429
t('Message not supported', async() => {
423430
await sql`create table test (x int)`
424431
return ['MESSAGE_NOT_SUPPORTED', await sql`copy test to stdout`.catch(x => x.code)]

0 commit comments

Comments
 (0)