Repository navigation
Description
SQLAlchemy-Continuum version
1.6.0
SQLAlchemy version
2.0.51
Database backend and driver
any
Python version
3.14
What happened, and what did you expect instead?
Exception happened under multi-threaded congested workload
make_versioned() registers Continuum's listeners at the class level (on sa.orm.Mapper, sa.orm.session.Session, sa.engine.Engine), so a single process-wide VersioningManager singleton is shared by every session on every thread. That singleton keeps per-transaction state in two global mutable dicts:
- manager.units_of_work — Connection -> UnitOfWork
- manager.session_connection_map — Session -> Connection
Several code paths iterate these dicts while other threads mutate them — e.g. clear, clear_connection, _uow_from_conn, and track_cloned_connections all do for c in dict(self.units_of_work): .... Under a thread pool where each thread runs its own session against a shared Engine (shared connection pool), this races and raises, typically:
RuntimeError: dictionary changed size during iteration
or a KeyError from a connection entry deleted by another thread mid-lookup.
Minimal, complete, verifiable example
import threading
import sqlalchemy as sa
from sqlalchemy.orm import declarative_base, sessionmaker
from sqlalchemy_continuum import make_versioned
make_versioned(user_cls=None)
Base = declarative_base()
class Article(Base):
__tablename__ = 'article'
__versioned__ = {}
id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
name = sa.Column(sa.Unicode(255))
engine = sa.create_engine('postgresql://postgres@localhost/continuum_test')
sa.orm.configure_mappers()
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
errors = []
def worker():
try:
for _ in range(50):
session = Session()
article = Article(name='x')
session.add(article)
session.commit() # writes a version row
article.name = 'y'
session.commit() # writes another
session.close()
except Exception as e: # noqa: BLE001
errors.append(e)
threads = [threading.Thread(target=worker) for _ in range(8)]
for t in threads:
t.start()
for t in threads:
t.join()
assert not errors, errors # fails: dictionary changed size during iteration / KeyError
Running this against a real connection pool (postgres/mysql) surfaces intermittent crashes from concurrent mutation of manager.units_of_work / manager.session_connection_map.Full stack trace (if any)
Traceback (most recent call last):
...
File "sqlalchemy/orm/session.py", line 1814, in __exit__
self.close()
File "sqlalchemy/orm/session.py", line 2529, in close
self._close_impl(invalidate=False)
File "sqlalchemy/orm/session.py", line 2598, in _close_impl
transaction.close(invalidate)
File "<string>", line 2, in close
File "sqlalchemy/orm/state_changes.py", line 137, in _go
ret_value = fn(self, *arg, **kw)
File "sqlalchemy/orm/session.py", line 1422, in close
transaction.close()
File "sqlalchemy/engine/base.py", line 2599, in close
self._do_close()
File "sqlalchemy/engine/base.py", line 2737, in _do_close
self._close_impl()
File "sqlalchemy/engine/base.py", line 2723, in _close_impl
self._connection_rollback_impl()
File "sqlalchemy/engine/base.py", line 2715, in _connection_rollback_impl
self.connection._rollback_impl()
File "sqlalchemy/engine/base.py", line 1112, in _rollback_impl
self.dispatch.rollback(self)
File "sqlalchemy/event/attr.py", line 516, in __call__
fn(*args, **kw)
File "sqlalchemy_continuum/manager.py", line 405, in clear_connection
uow = self.units_of_work[connection]
KeyError: <sqlalchemy.engine.base.Connection object at 0x7f290031bd90>
Activity
dPeS commentedon Jul 9, 2026
+1