Skip to content

fix: stop pool max-lifetime retirement from killing in-flight queries - #653

Merged
ridafkih merged 1 commit into
mainfrom
fix/pool-lifetime-inflight-query
Aug 13, 2026
Merged

fix: stop pool max-lifetime retirement from killing in-flight queries#653
ridafkih merged 1 commit into
mainfrom
fix/pool-lifetime-inflight-query

Conversation

@ridafkih

Copy link
Copy Markdown
Owner

Bun 1.3.14's pool fails a connection the instant maxLifetime elapses — onMaxLifetimeTimeout never checks for a query in flight — so healthy queries die with ERR_POSTGRES_LIFETIME_TIMEOUT and nothing retries them (oven-sh/bun#30646; fix oven-sh/bun#30648 unmerged, no release changes this). With our 1800s lifetime this killed the better-auth session read on a ~30-minute cadence (500 on every authed route) plus cron ingest and worker jobs; Loki shows bursts clustered at :02/:32, matching connection cohorts aging out together. Setting the lifetime to 0 removes the failure mode entirely; idleTimeout still reaps idle connections, so nothing accumulates.

  • Rejected blanket retry on ERR_POSTGRES_LIFETIME_TIMEOUT: unsafe for non-idempotent writes (a statement can commit server-side before the socket dies), and reads-only classification has no central choke point.
  • Rejected call-site retry on the session read: leaves cron/worker failures in place and patches a symptom the config change removes at the source.
  • New live test proves the mechanism (non-zero maxLifetime kills an in-flight pg_sleep), pins the shipped pool config at 0 (red before, green after), and shows 0 leaves in-flight queries untouched.

Note: the same driver race exists for idleTimeout (~2k worker job failures/day as Idle timeout reached after 30s) — left for a follow-up since disabling it changes pool capacity behavior.

Fixes #620

🤖 Generated with Claude Code

https://claude.ai/code/session_013BQkcrAZNXz7au7KkkT7JV

Bun 1.3.14 retires a pooled connection the instant maxLifetime elapses,
mid-query included (oven-sh/bun#30646, fix unmerged), failing healthy
queries with ERR_POSTGRES_LIFETIME_TIMEOUT on a 30-minute cadence. In
production this killed better-auth session reads, returning 500 on every
authed route, and failed cron ingest and worker sync jobs. Disable
age-based retirement; idleTimeout still reaps idle connections.

Fixes #620

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013BQkcrAZNXz7au7KkkT7JV
@ridafkih
ridafkih merged commit 8e970bb into main Aug 13, 2026
5 checks passed
@ridafkih
ridafkih deleted the fix/pool-lifetime-inflight-query branch August 13, 2026 21:49
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.

Postgres max-lifetime connection recycling kills in-flight session queries and 500s every authed route

1 participant