Repository navigation
Add answer_timeout for a server that stops answering - #1278
GrahamCampbell wants to merge 2 commits into
Conversation
d44b76f to
e59b08f
Compare
|
I've rebased, and pushed a correction. |
|
Thanks Graham! I've been looking closer at this case and some of the pitfalls, and I've gone ahead and added The queries reject with CANCEL_UNANSWERED as the cause, since whether they ran is unknown, and the pool reconnects. Wouldn't that also cover your case here? |
|
This is now in master so you can check if it covers your need too. |
|
Thanks, that covers the case I opened this for. With While checking it against master I came across four things you may want to look at before it's released. The promise from the cancel request is dropped when Now that A query that times out while it is pipelined behind a slower one gets its cancel when it reaches the head of the connection, and by then it has usually run already, so the cancel stops whatever runs next on that backend, often another caller's query, while the timed out query resolves with its rows rather than with The type read that runs when a query first needs a type doesn't go through I can send the tests I used for any of these if that helps. Reproduction: the dropped cancel promiseAgainst a PostgreSQL with import net from 'net'
import postgres from 'postgres'
let silent = false
const relay = net.createServer(client => {
client.on('error', () => {})
if (silent)
return
const server = net.connect(5432, 'localhost')
server.on('error', () => {})
client.on('data', x => silent || server.write(x))
server.on('data', x => silent || client.write(x))
client.on('close', () => server.destroy())
server.on('close', () => client.destroy())
})
await new Promise(r => relay.listen(0, 'localhost', r))
const sql = postgres({ host: 'localhost', port: relay.address().port, ssl: 'require', query_timeout: 1 })
await sql`select 1`
silent = true
const start = Date.now()
await sql`select 1`.then(
() => console.log('resolved'),
e => console.log('rejected', e.code, e.cause && e.cause.code, 'after', Date.now() - start, 'ms')
)
console.log('still running')
await sql.end({ timeout: 0 })
relay.close()Reproduction: a cancel on the pipelined BEGINAgainst a local PostgreSQL. Every transaction On 4.0.0 every rejected transaction had written nothing. import postgres from 'postgres'
const sql = postgres({ max: 1, connection: { application_name: 'victim' } })
, other = postgres({ max: 1 })
await other`drop table if exists begin_cancel`
await other`create table begin_cancel (i int)`
let spraying = true
const spray = (async() => {
while (spraying)
await other`select pg_cancel_backend(pid) from pg_stat_activity where application_name = 'victim'`
})()
const outcomes = {}
for (let i = 0; i < 3000; i++) {
const told = await sql.begin(async sql => {
await sql`insert into begin_cancel values (${ i })`
await sql`insert into begin_cancel values (${ i })`
}).then(() => 'resolved', e => 'rejected ' + e.code + ' (' + e.query + ')')
const [{ n }] = await other`select count(*)::int as n from begin_cancel where i = ${ i }`
const key = told + ', ' + n + ' rows written'
outcomes[key] = (outcomes[key] || 0) + 1
}
spraying = false
await spray
console.log(outcomes)
await other`drop table begin_cancel`
await sql.end()
await other.end() |
|
Fantastic feedback and repros! Thank you! |
|
Thanks Graham, all four were real, and the reproductions made them quick to pin down - pushed fixes now |
When the server stops answering while its connection stays up, as when it stalls or something in between keeps the connection open and drops the traffic, a query sent on that connection never settles, and nor does any query pipelined behind it.
keep_alivecannot close the connection because the other end still answers its probes,connect_timeoutcovers only connecting,queue_timeoutcovers only waiting for a connection, and the error the server'sstatement_timeoutwould send never arrives.This adds
answer_timeout, in seconds and off by default. A connection that is waiting for an answer and receives nothing for that long is closed, and its queries reject withANSWER_TIMEOUT, so the next query gets a new connection. The wait starts when a query is written and starts again with every chunk received while an answer is still owed, so a query the server keeps answering with rows or notices runs as long as it takes and only silence is timed. A query that runs longer than that without sending anything back is cut off too, so the option belongs abovestatement_timeout. It does not time a reserved connection, whose holder may keep it busy as long as it likes (a session lock wait, for instance), nor a connection while the server is waiting on the client: during a cursor's callback, acopy ... from stdinbeing written, or a stream whose reader has paused it. The README and the types document the option and the error, and the tests cover a connection that goes silent and the cases that are not timed.Reproduction
Against PostgreSQL 17, through a relay that keeps both sockets open and drops every byte once
silence()is called. On 4.0.0, which ignores the unknownanswer_timeoutoption:With this change: