Repository navigation
Description
DRAFT 19 (SDK, 2026-10-27) — to be filed
Title: [Bug]: ConnectionHook tracks executed connections by id() with an unbounded set — id-reuse skips hooks; set never clears
Description
ConnectionHook (connection_hooks.py) ensures feature-enabling SQL (experimental flags) runs once per pooled DBAPI connection. Its bookkeeping:
self._executed_connections = set() # never pruned
...
conn_id = id(dbapi_connection) # python object identity
if conn_id not in self._executed_connections:
self.execute_sync_with_connection(...)
self._executed_connections.add(conn_id)The same id()-set pattern is used in both event handlers, and the hook is registered on both connect and before_cursor_execute events.
Defects:
- ** id()-reuse false-negatives**: after a pooled connection is recycled/GC'd, a new DBAPI connection can get the same memory address → same id() → the hook believes it already ran for this connection and skips the experimental-flag SET. Symptoms: fulltext/vector features intermittently "off" on some pooled connections — precisely the nondeterministic modern-flags behavior these hooks exist to prevent.
- Unbounded growth: connections are removed from neither on close nor on invalidate (pool recycle after 3600s in default configs — each recycle adds ids forever). Long-lived CLI services grow the set indefinitely (small objects, but a leak pattern that also worsens the id-reuse probability).
- Double registration / redundant triggers: with both
connectandbefore_cursor_executefiring (before_cursor fires on every statement until the id is seen), the before_cursor path re-checks id on every statement executed on every pooled connection, holding a lock-free mutable shared set on hot paths; and since the set can be added by the connect handler, most before_cursor checks are dead weight — while after (1) misfires, neither handler re-runs, making the "safety-net" both wasteful and ineffective at the same time.
The right lifecycle hook is the SQLAlchemy ConnectionRecord passed into the event (stable per pooled connection, cleaned by pool on recycle), or ConnectionPoolEntry; using raw id() of a driver connection is the documented misuse of object identity.
Reproduction
Conceptual (no server):
# fresh check-out ran hook: id(x) = 140234...
pool_args: connection closes, object freed; new object lands at same addr
# next check-out: id(new_conn) in _executed_connections → hook SKIPPED
# → connection lacks SET experimental_fulltext_index = 1 (or IVF/HNSW flags)
# fulltext/vector queries intermittently fail on this connection from poolGrowth side: run a workload with pool_recycle=60s for days → the set accumulates every 60-second recycle (ids never pruned).
Impact
- Experimental-feature flags (the very flags the fulltext/vector SDK features depend on —
SET experimental_fulltext_index = 1,experimental_hnsw_index, IVF variants) become per-connection nondeterministic: same client, same code, sometimes-fulltext-on sometimes-off. An intermittent-class bug that wastes days to diagnose and is invisible in short tests (memory addresses rarely collide in short runs). - Long-running services (aiohttp/FastAPI with a client per process) silently leak the book-keeping set.
- The double event-registration increases per-statement overhead for no correctness gain.
Expected behavior
- Track by the SQLAlchemy-provided record (e.g., stash a flag on
connection_recordin theconnecthandler — the sanctioned pattern), not byid()of the driver connection. - Drop the
before_cursor_executeregistration entirely once the connect-handler owns the state (or keep it only as a retry-on-invalidated hook usingcontext.connection.record). - Regression: force
pool_recycleto 1s, run N check-out/execute cycles, assert every checked-out connection received the flag SET (e.g., by counting executions on a stub executor) — with today's code this test fails intermittently by construction.
Activity
Ariznawlll commentedon Oct 27, 2026
Verified fixed on current main (2026-10-27): connection_hooks.py now listens on the
checkoutevent and stores its once-per-connection marker inconnection_record.info(cleared by SQLAlchemy when the physical connection is replaced) — exactly the ConnectionRecord-based lifecycle approach this report's expected-behavior described. The id()-of-dbapi_connection set is gone. Closing as fixed.