Skip to content

fix(query): give a timed-out statement's connection back to the pool - #659

Merged
EVWorth merged 1 commit into
mainfrom
fix/timeout-connection-leak
Sep 12, 2026
Merged

EVWorth merged 1 commit into
mainfrom
fix/timeout-connection-leak

Conversation

@EVWorth

@EVWorth EVWorth commented Sep 12, 2026

Copy link
Copy Markdown
Owner

Closes #658. Found by running the EXPLAIN integration suite against MariaDB for the first time.

What happens

The timeout dropped the result stream and then killed the statement. Dropping a partially-consumed stream leaves sqlx to drain the connection's remaining packets before it can go back to the pool — and on MariaDB that drain never finishes. The connection is checked out for good.

The consequence is worse than a leak. Measured on MariaDB 11.8, pool_max = 4, query_timeout_secs = 1, SELECT SLEEP(5) five times:

timeout 1 took 1.00s -> Timeout(...)
timeout 2 took 1.00s -> Timeout(...)
timeout 3 took 1.00s -> Timeout(...)
timeout 4 took 5.00s -> Timeout(...)   ← the sleep's duration, not the limit
timeout 5 took 5.00s -> Timeout(...)

After pool_max timeouts there is nothing left to acquire, so the next one waits for a statement to end naturally and then reports "exceeded the 1s timeout" — the setting silently stops doing what it says. disconnect never returns either, because pool.close() waits for connections that are still out.

MySQL happened to recover, which is why nothing caught it: every integration test in the tree ran against MySQL only.

What was ruled out first

  • The KILL does reach the server — PROCESSLIST is clean immediately after the timeout, on both.
  • SELECT CONNECTION_ID() decodes as u64 on MariaDB, so the thread id is not lost.
  • KILL QUERY works through the prepared-statement protocol on MariaDB.
  • Nothing is left running server-side; the sessions show as idle.

The fix

Kill first, then read the stream to its end. That gives sqlx the server's "query interrupted" error, which ends the stream properly and returns the connection. The drain is bounded at five seconds, so a server that says nothing costs one grace period rather than a connection that never comes back.

Test

Ten timeouts through a pool of two, asserting each returns at its deadline rather than at the statement's, then that disconnect returns within ten seconds. Against the old code on MariaDB it fails from the third attempt.

query_timeout.rs now takes MAS_TEST_PORT like the other integration files, so both servers can be run. Passing on MySQL 8.0.46 and MariaDB 11.8; the EXPLAIN suite now passes on both too.

The timeout dropped the result stream and then killed the statement.
Dropping it leaves sqlx to drain the connection's remaining packets
before it can return to the pool, and on MariaDB that drain never
finishes — the connection is checked out for good.

The consequence is worse than a leak. With pool_max connections lost,
the next timeout has nothing to acquire, so it waits for a statement to
end naturally and reports "exceeded the 1s timeout" after five seconds;
`disconnect` then hangs for ever on `pool.close()`. Measured on MariaDB
11.8 with pool_max=4 and a 1s timeout against SELECT SLEEP(5): the first
three attempts returned at 1s, the fourth and fifth at 5s.

MySQL happened to recover, which is why nothing caught it: every
integration test in the tree ran against MySQL only.

Killing first and then reading the stream to its end gives sqlx the
server's "query interrupted" error, which ends the stream properly and
returns the connection. The drain is bounded at five seconds, so a
server that says nothing costs one grace period rather than a connection
that never comes back.

The new test runs ten timeouts through a pool of two and asserts each
returns at its deadline rather than at the statement's, then that
`disconnect` returns. It fails against the old code on MariaDB from the
third attempt. `query_timeout.rs` takes MAS_TEST_PORT now, as the other
integration files do, so both servers can be run.

Closes #658.
@EVWorth
EVWorth merged commit 319399f into main Sep 12, 2026
12 checks passed
@EVWorth
EVWorth deleted the fix/timeout-connection-leak branch September 12, 2026 05:10
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.

[P1][bug] A timed-out query never returns its connection on MariaDB — pool_max timeouts exhaust the pool and disconnect hangs

1 participant