Skip to content

[Bug]: ConnectionHook dedupes by id() of pooled connections and never prunes them #31396

Closed

Description

@Ariznawlll

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:

  1. ** 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.
  2. 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).
  3. Double registration / redundant triggers: with both connect and before_cursor_execute firing (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 pool

Growth 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

  1. Track by the SQLAlchemy-provided record (e.g., stash a flag on connection_record in the connect handler — the sanctioned pattern), not by id() of the driver connection.
  2. Drop the before_cursor_execute registration entirely once the connect-handler owns the state (or keep it only as a retry-on-invalidated hook using context.connection.record).
  3. Regression: force pool_recycle to 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

  1. added
    kind/bug
    on Oct 27, 2026
  2. added this to the MO-202610 milestone on Oct 27, 2026
  3. added theissue type on Oct 27, 2026
  4. Ariznawlll commented on Oct 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    Verified fixed on current main (2026-10-27): connection_hooks.py now listens on the checkout event and stores its once-per-connection marker in connection_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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions