fix(memory): release SQLite engine config entries when engines are collected - #4210
Merged
seratch merged 1 commit intoAug 5, 2026
Merged
Conversation
…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
approved these changes
Aug 5, 2026
seratch
enabled auto-merge (squash)
August 5, 2026 13:09
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SQLAlchemySession._configure_sqlite_engineremembers that an engine already has the SQLiteconnectlistener by storingid(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 getsPRAGMA busy_timeoutorPRAGMA journal_mode = WAL, which is exactly the configuration_run_sqlite_write_with_retryexists to complement. The same set also grows for the lifetime of the process, whichfrom_urlmakes easy to hit because it creates and discards an engine per session.This registers a
weakref.finalizecallback that discards the entry when the sync engine is collected, matching the guardMongoDBSessionalready documents for its ownid()-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-livedfrom_urlsessions. Both fail on main and pass with the fix, anduv run pytest tests/extensions/memory/test_sqlalchemy_session.pyis green at 36 passed along withmake format,make lint, andmake typecheck.