Skip to content

fix(memory): release SQLite engine config entries when engines are collected - #4210

Merged
seratch merged 1 commit into
openai:mainfrom
adityasingh2400:fix/sqlalchemy-sqlite-config-registry
Aug 5, 2026
Merged

fix(memory): release SQLite engine config entries when engines are collected#4210
seratch merged 1 commit into
openai:mainfrom
adityasingh2400:fix/sqlalchemy-sqlite-config-registry

Conversation

@adityasingh2400

Copy link
Copy Markdown
Contributor

SQLAlchemySession._configure_sqlite_engine remembers that an engine already has the SQLite connect listener by storing id(engine.sync_engine) in a class-level set, but that set holds no reference to the engine, so the entry outlives the object. CPython reuses addresses, so a later engine allocated at the same address takes the early return and never gets PRAGMA busy_timeout or PRAGMA journal_mode = WAL, which is exactly the configuration _run_sqlite_write_with_retry exists to complement. The same set also grows for the lifetime of the process, which from_url makes easy to hit because it creates and discards an engine per session.

This registers a weakref.finalize callback that discards the entry when the sync engine is collected, matching the guard MongoDBSession already documents for its own id()-keyed registry. Two regression tests cover it: one asserts the entry is gone after the engine is collected, and one asserts the registry returns to its baseline size after 25 short-lived from_url sessions. Both fail on main and pass with the fix, and uv run pytest tests/extensions/memory/test_sqlalchemy_session.py is green at 36 passed along with make format, make lint, and make typecheck.

…llected

SQLAlchemySession memoizes "this engine already has the SQLite PRAGMA
listener" as id(engine.sync_engine) in a class-level set that holds no
reference to the engine. The entry outlives the engine, so a later engine
allocated at the same address is treated as already configured and never
receives PRAGMA busy_timeout or PRAGMA journal_mode = WAL. The set also
grows for the lifetime of the process.

Register a weakref.finalize callback that discards the entry when the sync
engine is collected, matching the guard MongoDBSession already documents
for its own id()-keyed registry.
@seratch seratch added this to the 0.20.x milestone Aug 5, 2026
@seratch
seratch enabled auto-merge (squash) August 5, 2026 13:09
@seratch
seratch merged commit afd1a26 into openai:main Aug 5, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants