uralbash / sqlalchemy_mptt

SQLAlchemy nested sets mixin (MPTT)
http://sqlalchemy-mptt.readthedocs.io
MIT License
196 stars 32 forks source link

Is mptt_sessionmaker thread-safe? #34

Closed fayazkhan closed 10 years ago

fayazkhan commented 10 years ago

Hi, Looking at the code, it seems that TreesManager.after_flush_postexec operates on a global _WeakDictBasedSet object. Maybe it should be single _WeakDictBasedSet for each session instead?

--- Want to back this issue? **[Place a bounty on it!](https://www.bountysource.com/issues/5204181-is-mptt_sessionmaker-thread-safe?utm_campaign=plugin&utm_content=tracker%2F1148440&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F1148440&utm_medium=issues&utm_source=github).
fayazkhan commented 10 years ago

This test shows part of the problem


    def test_flush_with_transient_nodes_present(self):
        transient_node = self.model(ppk=1, parent=None)
        self.session.add(transient_node)
        try:
            self.session.flush()
        except IntegrityError:
            pass
        self.session.rollback()
        self.session.add(self.model(ppk=46, parent=None))
        self.session.flush()

Can occur in multiple ways in a multithreaded environment with a scoped_session.

uralbash commented 10 years ago

I have not tried to use it with multithreaded environment, thanks for the pull request