transact-rs/sqlx · warning
failed to close setup connection
Error message
failed to close setup connection
What it means
sqlx's test harness (`#[sqlx::test]` setup) applies fixture files to a freshly created test database, then closes the setup connection. This panic fires from `.expect()` on `conn.close()` if the connection fails to close gracefully. It is a post-fixture teardown failure, not a fixture failure.
Source
Thrown at sqlx-core/src/testing/mod.rs:274
.expect("failed to connect to test database");
if let Some(migrator) = args.migrator {
migrator
.run_direct(None, &mut conn, false)
.await
.expect("failed to apply migrations");
}
for fixture in args.fixtures {
(&mut conn)
.execute(fixture.contents)
.await
.unwrap_or_else(|e| panic!("failed to apply test fixture {:?}: {:?}", fixture.path, e));
}
conn.close()
.await
.expect("failed to close setup connection");
}
View on GitHub (pinned to 03af8bcc57)
Solutions
- Retry the test; if it is flaky, check DB server logs for dropped connections (idle_timeout, max_connections).
- Point DATABASE_URL at a local database or increase server-side connection limits/timeouts.
- Check network stability (proxies, docker port mappings) between test runner and DB.
- Update sqlx; if the close failure is caused by a driver bug, a newer patch release may fix it.
Defensive patterns
Strategy: retry
Try / catch
// The harness panics via .expect, so guard at the test level:
// run flaky tests with a retry wrapper or `cargo test -- --retry` in CI,
// and verify DB reachability before the suite:
let mut conn = MySqlPool::connect(&url).await.expect("DB unreachable before tests");
conn.close().await; // sanity-check close works Prevention
- Run integration tests against a local containerized DB, not a shared remote server
- Raise server idle_timeout / max_connections to survive test-suite duration
- Avoid proxies/NAT layers that reset idle TCP connections mid-suite
When it happens
Trigger: Calling `setup_test_db` (via `#[sqlx::test]` with fixtures) where the final `conn.close()` returns an Err — typically because the connection was already terminated (server dropped it, network blip, or the fixture code left the connection in a broken state).
Common situations: Flaky CI runs against remote/shared MySQL or Postgres servers; server-side idle timeout or max_connections killing the setup connection mid-test; proxy/firewall resetting TCP connections; killing the database container while tests run.
Related errors
- cleanup_test() invoked outside `#[sqlx::test]`
- DATABASE_URL must be set
- failed to parse DATABASE_URL
- cleanup_test() invoked outside `#[sqlx::test]`
- DATABASE_URL must be set
AI-assisted analysis of transact-rs/sqlx@03af8bcc57 (2026-09-03).
Data as JSON: /api/errors/b3a4d08258c5116d.
Report an issue: GitHub.