Skip to content

Fail fast on refused database connections - #394

Merged
taylorotwell merged 1 commit into
0.xfrom
fail-fast-on-refused-database-connections
Dec 1, 2026
Merged

taylorotwell merged 1 commit into
0.xfrom
fail-fast-on-refused-database-connections

Conversation

@taylorotwell

Copy link
Copy Markdown
Member

Problem

When a configured PostgreSQL or MySQL server isn't running (for example, running laravel migrate in a fresh app before setting up Postgres), SQLx's pool catches io::ErrorKind::ConnectionRefused and retries it with exponential backoff for the full acquire_timeout (30 seconds by default), then discards the I/O error and returns PoolTimedOut ("pool timed out while waiting for an open connection").

Fix

  • In crates/database/src/db/pools.rs, when a non-SQLite pool is still connecting after 3 seconds, probe the host directly with options.connect().
    • If the probe gets ConnectionRefused, abort pool.acquire() immediately and return that I/O error instead of waiting 30 seconds for PoolTimedOut.
    • If the probe succeeds, close it cleanly and let pool.acquire() finish.
    • Transient startup errors (such as PostgreSQL 57P03 / 53300) still let SQLx keep retrying up to acquire_timeout.
  • Format refused connection errors with the connection name and target endpoint (host:port or Unix socket):
    couldn't connect to database `postgres` at `127.0.0.1:5432`; make sure the database server is running: error communicating with database: Connection refused (os error 111)
    
  • Add a regression test in tests/migrations.rs verifying migrate fails fast against an unbound local port with the default 30s acquire_timeout.

SQLx's pool retries ConnectionRefused with exponential backoff for the
full acquire_timeout (30s by default), then discards the I/O error and
returns PoolTimedOut.

When a non-SQLite pool is still connecting after 3 seconds, probe the
host directly and abort immediately on ConnectionRefused with the real
I/O error and target host/port (or socket) in the message.

Amp-Thread-ID: https://ampcode.com/threads/T-4b34fcd3-0d8f-4878-a303-28fa30e6d865
Co-authored-by: Taylor Otwell <taylor@laravel.com>
@taylorotwell
taylorotwell merged commit b57ebe4 into 0.x Dec 1, 2026
11 checks passed
@taylorotwell
taylorotwell deleted the fail-fast-on-refused-database-connections branch December 1, 2026 21:34
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