Skip to content

v2.6.0 Change Details

Theme: fix of the connection leak when a Result is not properly closed (Result.__del__ discards the connection + pool bookkeeping) and standardization of SQL execution usage. Abandoned unclosed Results no longer pin connections permanently, and disconnect() with default settings no longer hangs because of them; docs are unified to the safe standard forms.

Bug Fixes

1. Result.del discards the connection + pool bookkeeping

Before the fix: Result.__del__ only cleared the error reference and emitted a ResourceWarning, without returning the connection. When a Result left unclosed after explicit await r() / fetch_one() / fetch_many() was GC'd, its connection was pinned forever by aiomysql Pool._used (a strong-reference set):

  • After max_pool_size leaks, subsequent execute() calls hung forever in the acquire queue;
  • disconnect() with defaults (disconnect_timeout=None) hung forever in wait_closed();
  • Socket fds, transport buffers and the full result-set buffer stayed resident until process exit.

After the fix: when __del__ detects an unclosed cursor, it synchronously discards the connection and completes pool bookkeeping:

  • Synchronous conn.close() (closes the transport only, no COM_QUIT): the server terminates the query on that connection and rolls back uncommitted transactions; data already auto-committed under autocommit=True is unaffected;
  • Synchronous pool.release(conn): a closed connection is only removed from _used and never put back into the idle queue (aiomysql semantics), so pool bookkeeping always converges and disconnect() no longer hangs because of this;
  • Discard instead of returning for reuse: __del__ cannot await and cannot drain unread streaming rows; returning the dirty connection would pollute the next user (Previous unbuffered result was left incomplete warning + blocking drain, even 2013 / Packet sequence number wrong);
  • Emits a ResourceWarning with updated wording: "connection discarded (not reused), uncommitted transactions will be rolled back by the server";
  • GC is silent for Results already closed / never executed / auto-released after DDL; never awaits, never resurrects references; conn.close and pool.release each guard their own exceptions (safe when the loop is already closed, the pool already terminated, etc.).

Behavior Changes

  • GC safety net semantics: abandoned (garbage-collected) unclosed Results no longer cause permanent connection leaks or disconnect() hangs; the cost is that the connection is discarded and not reused (the safety net only fires on misuse). Results still referenced and unclosed are not GC leaks, and the disconnect() waiting semantics are unchanged;
  • disconnect_timeout default None unchanged: a finite timeout is still recommended when convergence guarantees are needed;
  • ResourceWarning wording change: from "connection not returned to the pool to avoid pollution" to "connection discarded (not reused), uncommitted transactions will be rolled back by the server".

Usage Standardization

SQL execution recommends only the unified safe forms (connections are closed/released automatically in all of them): async with, async for (including break), fetch_all(), iterate(), and automatic release of statements without result sets (DDL/DML). The bare-execution usage "r = engine.execute(...) → await r() → fetch_one() without close" is no longer recommended. The APIs themselves are not removed (__call__ is the internal execution entry; the fetch family is safe inside contexts); standardization lives at the documentation layer:

  • result.md: new "Standard Usage (Unified Forms)" section (whitelist + standard examples + not-recommended usage statement); "Commit and Connection Release Semantics" gains a GC safety net entry;
  • best-practices.md: new ❌ counter-example of bare execution + fetch without close; "Important Notes" updated to v2.6.0 semantics;
  • api.md: Result __await__() section clarified as not implemented (await result raises TypeError; the docs previously claimed support incorrectly); __call__() repositioned as the internal execution entry with standalone use marked not recommended; fetch_one()/fetch_many() gain usage constraints; new __del__() entry;
  • faq.md: disconnect-hang Q&A updated — GC'd unclosed Results no longer cause an infinite wait; only Results still referenced and unclosed keep it waiting.

Scope Boundaries

  • disconnect_timeout default None unchanged; no new configuration options;
  • Result.__await__ remains unimplemented;
  • AsMysql constructor singleton and v1 untouched; __call__, fetch_one, fetch_many APIs not removed/disabled.