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, anddisconnect()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_sizeleaks, subsequentexecute()calls hung forever in the acquire queue; disconnect()with defaults (disconnect_timeout=None) hung forever inwait_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 underautocommit=Trueis unaffected; - Synchronous
pool.release(conn): a closed connection is only removed from_usedand never put back into the idle queue (aiomysql semantics), so pool bookkeeping always converges anddisconnect()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 incompletewarning + blocking drain, even 2013 / Packet sequence number wrong); - Emits a
ResourceWarningwith 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.closeandpool.releaseeach 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 thedisconnect()waiting semantics are unchanged; disconnect_timeoutdefaultNoneunchanged: 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 resultraises 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_timeoutdefaultNoneunchanged; no new configuration options;Result.__await__remains unimplemented;- AsMysql constructor singleton and v1 untouched;
__call__,fetch_one,fetch_manyAPIs not removed/disabled.