Skip to content

Commit 60dc30f

Browse files
committed
Fix end timeout race condition
1 parent 373c73f commit 60dc30f

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
@@ -97,7 +97,9 @@ function Connection(options = {}) {
9797
}
9898

9999
function destroy() {
100-
error(errors.connection('DESTROYED', options))
100+
const err = errors.connection('DESTROYED', options)
101+
backend.query && backend.query.reject(err)
102+
error(err)
101103
socket.destroy()
102104
}
103105

lib/index.js

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

355355
let destroy
356356

357-
if (timeout === 0)
358-
return ended = Promise.all(all.map(c => c.destroy())).then(() => undefined)
359-
360357
return ended = Promise.race([
361358
Promise.resolve(arrayTypesPromise).then(() => Promise.all(all.map(c => c.end())))
362359
].concat(
363-
timeout > 0
360+
timeout === 0 || timeout > 0
364361
? new Promise(r => destroy = setTimeout(() => (all.map(c => c.destroy()), r()), timeout * 1000))
365362
: []
366363
))

tests/index.js

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

428+
t('Connection destroyed with query before', async() => {
429+
const sql = postgres(options)
430+
let error = sql`select pg_sleep(0.2)`.catch(err => err.code)
431+
sql.end({ timeout: 0 })
432+
return ['CONNECTION_DESTROYED', await error]
433+
})
434+
428435
t('Message not supported', async() => {
429436
await sql`create table test (x int)`
430437
return ['MESSAGE_NOT_SUPPORTED', await sql`copy test to stdout`.catch(x => x.code)]

0 commit comments

Comments
 (0)