Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed

- **Canceling a `sql.batch` queued behind another query honors `cancel_timeout`.** `cancelBatch` did not start the silence timer that `cancel()` starts for a queued query, so the batch and its `cancel()` stayed pending when the earlier query never finished.
- **A number for a `timestamptz`, `timestamp` or `date` parameter is written as epoch milliseconds again** (#1324). 4.0 sent a number as its digits before the server had named the parameter's type, so `${ Date.now() }` for a timestamp failed with `22008`, where v3 wrote it as that instant. The first time a statement with a number parameter runs, the pool now waits for the server's parameter types, at the cost of one round trip, and remembers them for that statement.

### Performance
Expand Down
9 changes: 7 additions & 2 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,13 @@ export function Connection(ready, options, events) {

function cancelBatch(query) {
const x = sent.find(x => x.batch && x.batch.query === query)
if (x)
return x === sent[0] ? cancelling(x) : new Promise((resolve, reject) => x.cancel = { resolve, reject })
if (x) {
if (x === sent[0])
return cancelling(x)

quiet(x)
return new Promise((resolve, reject) => x.cancel = { resolve, reject })
}

const first = pending.find(x => x.batch && x.batch.query === query)
if (first) {
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/cancel.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,29 @@ t('A canceled sql.batch leaves no cancel_timeout timer behind', async() => {
await clock.run(300)
return ['57014,0', code + ',' + clock.pending]
})

t('Canceling a sql.batch queued behind another query honors cancel_timeout', async() => {
const clock = Clock()
, server = Server({ clock, random: Random(1), delay: 0, split: 0 })
, sql = postgres({
host: 'sim', port: 5432, user: 'sim', database: 'sim', max: 1, prepare: true, fetch_types: false,
connect_timeout: 2, max_lifetime: 0, idle_timeout: 0, cancel_timeout: 0.1, timers: clock.timers,
socket: () => server.socket()
})

sql`select ${ 1 }::int as id, ${ 0 }::int as scope`.catch(() => {})
await clock.run(100)
sql`select ${ 2 }::int as id, ${ 0 }::int as scope sleep(${ 100000 }::int)`.catch(() => {})
await clock.run(110)
const q = sql.batch([sql`select ${ 3 }::int as id, ${ 0 }::int as scope`])
let done = false
let canceled = false
q.then(() => done = true, () => done = true)
await clock.run(120)
q.cancel().then(() => canceled = true, () => canceled = true)
await clock.run(1000)
const result = [done, canceled]
sql.end({ timeout: 0 })
await clock.run(1100)
return [[true, true] + '', result + '']
})
Loading