0017 — Bun's `idleTimeout` kills in-flight statements: an api statement over 20 s dies
Status: candidate
Opened: 2026-09-16 · Area: platform (Bun SQL connection plumbing, packages/db/src/bun-sql)
Reopen trigger: the first ERR_POSTGRES_IDLE_TIMEOUT in the api, the workers or a lane (a report, a log
line, or a flaky integration test); or Bun shipping the fix, whichever comes first.
Evidence
Section titled “Evidence”Bun’s idleTimeout is not “how long an idle connection may sit in the pool”. Through Bun 1.4.2 (the
latest release) it fires while a statement is still running, as soon as that many seconds pass with no
bytes arriving from the server — the timer is armed on socket quiet, not on connection idleness.
Upstream: oven-sh/bun#30646, still open; the fix in #30648 is unmerged.
Reproduced locally 2026-09-16, one connection, nothing else in flight:
new SQL(url, { idleTimeout: 5 })+select pg_sleep(8)→ERR_POSTGRES_IDLE_TIMEOUT,"Idle timeout reached after 5s".new SQL(url, { idleTimeout: 0 })+ the same statement → completes.
createBunSqlClient (packages/db/src/bun-sql/index.ts) defaults idleTimeout: 20, tuned for the api’s
short request-path statements and its shared pool. Under the bug that default is really a 20-second
ceiling on every statement the api, the workers and the lrs run — a slow report query, a wide import
transaction or a big delete behind an erasure pass that runs past 20 s does not slow down, it fails.
It first bit an OPERATOR script, not the api: Anton’s predecessor:import run died in the bing-mt step on
a 5 000-row page that took longer than 20 s. The ruling there (2026-09-16) was narrow and is already
implemented — every one-shot CLI opens its clients through getOperatorBunSqlClient /
createOperatorBunSqlClient, which set idleTimeout: 0 (maxLifetime stays at Bun’s default 0 too); a
one-shot job has no reconnect logic and the operator at the terminal is its timeout. The api and the
workers deliberately still carry the 20 s default.
What makes the api’s exposure sharp rather than theoretical: the failure arrives as
ERR_POSTGRES_IDLE_TIMEOUT on an ALREADY-ESTABLISHED connection, so isTransientConnectError classifies
it as not transient and withTransientConnectRetry does not retry it — correctly, because the
statement may well have run and committed server-side before the client stopped listening. A request that
trips it fails outright, and a mutation that trips it is of genuinely unknown outcome.
Nothing has reported it in the api yet; the exposure is latent and bounded by the fact that every request-path statement we have is far under 20 s.
Disposition
Section titled “Disposition”Candidate, no decision. The options, in the order they would be considered:
- Wait for Bun and bump. #30648 fixes the timer to re-arm only when the connection is genuinely idle. When it lands in a release, take the Bun bump and this item closes with no code change — the cleanest outcome and the reason this is not being designed around today.
- Raise or disable the api’s default before then.
idleTimeout: 0on the shared pool removes the ceiling but also removes the only client-side reaping of connections that have rotted behind a load balancer;maxLifetime(Bun’s default 0, uncapped) would have to take that job, with the server side’s ownidle_in_transaction_session_timeout/tcp_keepalivesas the backstop. A middle setting (60–120 s) buys headroom without giving up reaping. Each needs a decision about what the pool is actually protecting against, which is why none is taken here. - Per-call escape. A long statement could take a dedicated timer-off client (what the operator helpers do) at the few call sites known to be slow. Cheapest to reason about, but it puts the knowledge of “this query is slow” in the caller, where it rots.
Also parked with this item: the integration lanes and tools/scripts e2e runners open their clients at
idleTimeout: 1, which under the same bug is a 1-second statement ceiling. Their statements are short and
no flake has been traced to it, but it is the same footgun and would be swept with whatever fix lands.