v2.4.0 Change Details¶
Theme: memory retention and event loop binding fixes (risks 4/6 from the memory analysis report). No new public API;
result.errorsemantics and Engine cross-loop behavior change. Analysis report:asmysql_v2_final_memory_analysis.mdat the repository root.
Behavior Changes¶
1. result.error.__traceback__ is now None¶
When an error is stored (execution failure, COMMIT/ROLLBACK failure), the exception's __traceback__ is uniformly set to None, severing the "Result → exception → traceback frame → Result" reference cycle:
- Before: holding
result.errorlong-term (e.g. an error audit list) also retained the whole Result and the frame's local variables (conn/values, etc.), and__del__never fired; even with no holder, the Result had to wait for a cyclic GC pass to be reclaimed. - After: the Result's reference count can reach zero immediately; holding the error no longer keeps the Result alive.
- Unaffected:
error_no,error_msg,result.query; the D4 "first error wins" semantics (a ROLLBACK failure does not overwrite the original error). - Trade-off: the original call stack is lost; re-raising the exception produces a fresh traceback at the raise site. Use
error_no/error_msgand the SQL for troubleshooting.
2. Clear error for live pools across event loops¶
An aiomysql pool is bound to its event loop at creation time. With the pool still connected, calling connect(), accessing engine.pool, or calling execute() in a new event loop now raises:
Previously this scenario either silently reused the wrong-loop pool (then failed in obscure ways) or raised asyncio's cryptic RuntimeError. Synchronous access without a running event loop keeps the legacy behavior.
New Features¶
Engine reuse across event loops¶
The internal connect lock (_connect_lock) is automatically rebuilt when the event loop changes, supporting the "disconnect in the original loop → connect in the new loop" reuse pattern:
engine = Engine(url="mysql://root:pass@127.0.0.1:3306/")
async def job():
await engine.connect()
async with engine.execute("SELECT 1") as result:
print(await result.fetch_one())
await engine.disconnect()
asyncio.run(job()) # Event loop 1
asyncio.run(job()) # Event loop 2: the lock is rebuilt and the pool is recreated
Before the fix, once the lock experienced contention it bound to that loop, and a second asyncio.run() raised RuntimeError: ... is bound to a different event loop (Python 3.12's Lock contended path validates the loop while the uncontended fast path does not, so the failure was intermittent).
Semantic boundaries:
- Sharing a live pool across loops is still unsupported (an aiomysql hard constraint); disconnect first;
- An abandoned Engine whose loop has closed without disconnecting is not guaranteed to be cleanable in a new loop;
- Concurrent
connect()calls within the same loop are still serialized by the lock; behavior is unchanged.
Bug Fixes¶
- Risk 4 (Result ↔ traceback reference cycle): the three error storage sites (
__call__/__commit/__rollback) converge into__store_error, which severs the frame chain on storage. - Risk 6 (
_connect_lockbound to the event loop): loop-aware lock rebuilding + a live-pool cross-loop guard (connect()and thepoolproperty).
Documentation¶
result.md/api.md: note thaterror.__traceback__isNone, with troubleshooting guidance.connection.md/api.md: new "Reuse Across Event Loops" section and constraints.faq.md: two new Q&As (why error has no traceback; reusing an Engine across asyncio.run).
Testing¶
- Mock:
TestErrorTracebackRetention(traceback severed, Result collectable while only the error is held),TestEngineCrossLoop(reconnect after contention across two loops, clear error for a live pool across loops, lock identity within one loop); red-green verified (the 4 target cases failed before the fix). - Integration:
test_cross_loop_reconnect(shared Engine, twoasyncio.run()cycles, each with concurrent connect → SELECT 1 → disconnect, real MySQL). - Full suite 430 passed; D4 first-error-wins covered by the existing M10 regression case.