Skip to content

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

Closed

Description

@EVWorth

What

On MariaDB, a statement stopped by query_timeout_secs leaves its pooled connection checked out for good. The timeout itself works — the call returns at the deadline and the statement is killed on the server — but the connection never comes back.

Evidence

A profile with pool_max = 4 and query_timeout_secs = 1, running SELECT SLEEP(5) five times against MariaDB 11.8:

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 full SLEEP, not the 1s limit
timeout 5 took 5.00s -> Timeout(...)

By the fourth attempt every connection in the pool is one of the abandoned ones, so the acquire itself waits for a statement to finish naturally — and the "timeout" the user gets is the sleep's own duration. The setting silently stops working after pool_max uses.

manager.disconnect() then never returns: pool.close() waits for connections that are still checked out. That is what hangs explain_integration.rs when run with MAS_TEST_PORT=13308.

The same code on MySQL 8.0.46 recovers immediately, which is why nothing caught it: every integration test in the tree runs against MySQL only.

What is not the cause

Checked, so the next person does not have to:

  • The KILL does reach the server. SELECT COUNT(*) FROM information_schema.PROCESSLIST WHERE INFO LIKE 'SELECT SLEEP%' is 0 immediately after the timeout, on both servers.
  • 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 (tested directly).
  • Nothing is left running server-side; the sessions show as idle.

Cause

executor.rs drops the result stream and then kills the statement. Dropping a partially-consumed stream leaves sqlx to drain the remaining packets before the connection can go back to the pool, and on MariaDB that drain never completes after the kill — the server does not send whatever sqlx is waiting for.

Fix

Kill first, then keep reading the stream until it ends, so sqlx sees the server's "query interrupted" error and the connection is returned cleanly. Bound the drain, so a server that sends nothing costs one grace period rather than a permanently lost connection.

Acceptance

  • Ten consecutive timeouts on a pool_max = 2 connection each return at the deadline, on both servers.
  • disconnect after a timeout returns promptly, on both servers.
  • explain_integration.rs and query_timeout.rs both pass with MAS_TEST_PORT=13308.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/connection

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions