Skip to content

Bound reconnects when connections close while fetching types - #1279

Merged
porsager merged 2 commits into
porsager:masterfrom
GrahamCampbell:types-read-close
Nov 1, 2026
Merged

porsager merged 2 commits into
porsager:masterfrom
GrahamCampbell:types-read-close

Conversation

@GrahamCampbell

@GrahamCampbell GrahamCampbell commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

On 4.0.0 a connection that closes while the pool's array and domain types query runs is replaced at once, without backoff and without connect_timeout. When the peer closes each new connection at that query, a query waiting for a connection never settles, and the pool opens new connections, each a full startup, as fast as the server takes them for as long as the peer keeps closing. This finishes #1193: v4 bounds a close during startup, but by the time the types query runs opened() has already reset retries and failingSince, and the close goes through onclose(), which only grows the pool again.

This change counts a connection that closes while it loads types as a failed attempt, as failed() counts a failed startup, so backoff paces the reconnects and the waiting queries reject with CONNECTION_CLOSED once connect_timeout has passed, or at once while the pool is ending. The retry state is reset once a connection can serve, which is when the types are in rather than when its startup completes, and after a failed attempt the pool waits for a connection that is still loading types rather than opening another beside it. With connect_timeout disabled (0, null or false), such an attempt is retried with backoff for as long as it takes, as the option says, and so is a startup that closes or is refused for too many connections, which before failed the waiting queries at once.

The existing test for a backend that dies while fetching array types waited for typarray, which v4's types query does not contain, so it never reached that query, and it now waits for pg_type. The tests at tests/connection.js:566 and :696 on master also wait for typarray and so never reach the types query either, and this changes neither.

Reproduction

Against PostgreSQL 17, with a relay in front of it that closes each connection when the types query arrives:

import net from 'node:net'
import postgres from 'postgres'

// Server and credentials from PGHOST, PGPORT, PGUSER, PGPASSWORD and PGDATABASE.
// A relay in front of the server closes each connection when the driver's types query arrives.
let connections = 0

const relay = net.createServer(client => {
  connections++
  const server = net.connect(process.env.PGPORT || 5432, process.env.PGHOST || 'localhost')
  client.on('data', x => x.includes('pg_catalog.pg_type') ? (server.destroy(), client.end()) : server.write(x))
  server.on('data', x => client.write(x))
  client.on('error', () => server.destroy()).on('close', () => server.destroy())
  server.on('error', () => client.destroy())
})
await new Promise(r => relay.listen(0, '127.0.0.1', r))

const sql = postgres({ host: '127.0.0.1', port: relay.address().port, max: 1, connect_timeout: 3 })
const start = Date.now()
const outcome = await Promise.race([
  sql`select 1`.then(() => 'resolved', e => 'rejected ' + e.code),
  new Promise(r => setTimeout(r, 10000, 'still pending'))
])
console.log(outcome, 'after', ((Date.now() - start) / 1000).toFixed(1), 's and', connections, 'connections')
process.exit(0)

On 4.0.0:

still pending after 10.0 s and 5541 connections

With this change:

rejected CONNECTION_CLOSED after 3.2 s and 6 connections

@porsager
porsager merged commit 00dd316 into porsager:master Nov 1, 2026
36 checks passed
@porsager

porsager commented Nov 1, 2026

Copy link
Copy Markdown
Owner

Very nice - Thank you !

@GrahamCampbell
GrahamCampbell deleted the types-read-close branch November 1, 2026 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants