Skip to content

Commit a5daa3e

Browse files
committed
Fix .end() hanging as pending forever
1 parent 219fa27 commit a5daa3e

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ function postgresSocket(options, {
322322
return Promise.resolve()
323323
},
324324
end: () => {
325-
return new Promise(r => socket ? socket.end(r) : r())
325+
return new Promise(r => socket && !closed ? socket.once('close', r) : r())
326326
},
327327
connect
328328
}

tests/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,16 @@ t('Catches connection config errors', async() => {
10871087
]
10881088
})
10891089

1090+
t('Catches connection config errors with end', async() => {
1091+
const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' })
1092+
1093+
return [
1094+
'wat',
1095+
await sql`select 1`.catch((e) => e.message),
1096+
await sql.end()
1097+
]
1098+
})
1099+
10901100
t('Catches query format errors', async() => [
10911101
'wat',
10921102
await sql.unsafe({ toString: () => { throw new Error('wat') } }).catch((e) => e.message)

0 commit comments

Comments
 (0)