globality-corp / microcosm-postgres

Opinionated persistence with PostgreSQL
Apache License 2.0
4 stars 7 forks source link

SessionContext does not support multi-thread well #79

Closed raphaelfeng closed 3 months ago

raphaelfeng commented 5 years ago

The session is a class variable, so if

  1. thread a open session
  2. thread b open session
  3. thread a close session

SessionContext.session would be None to thread b.

Can we use scoped session as described in https://docs.sqlalchemy.org/en/13/orm/contextual.html?

cdm-ium commented 5 years ago

Hello!

We have not had problems with this because the way we use this in microcosm-flask is not threaded.

Do you have any sample code that causes issues with sessions and threading? I see how what you're describing would cause problems, but for the overall health of the ecosystem, we would like to know how exactly you are using microcosm-postgres in a multi-threaded environment for more context.

raphaelfeng commented 5 years ago

Thanks for the quick reply. The threaded option is enabled by default in flask 1.1.0 https://github.com/pallets/flask/blob/1.1.0/src/flask/app.py#L941. So it can cause issue when using with flask's default option.

cdm-ium commented 5 years ago

Can you give me some specific repro steps/problem statement? We've been using this library for years now and haven't run into any issues with threading, although perhaps you've found an issue we ourselves have encountered but didn't realize the root cause for.