-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
run_transaction session leak #52
Comments
Hi @ansgarstrother sorry you're running into this issue. I think that example code you found may be out of date. Here are the docs for running CockroachDB with SQLAlchemy: https://www.cockroachlabs.com/docs/stable/build-a-python-app-with-cockroachdb-sqlalchemy.html At the bottom of that page, there is a link to a repo with some examples of how to use CockroachDB with SQLAlchemy: https://github.com/cockroachdb/examples-orms/tree/master/python/sqlalchemy Those examples still use Just so I better understand the issue, was there a reason you decided to make the change? Was the code not performing as you expected with |
We were having issues where we need to retry. We posted on the forum here and were told to make the change outlined above. |
Hi @ansgarstrother, sorry, I gave you incorrect information above. The code you found in that blog post is correct, and In this case, there must be something else going on. What sort of tests are you running? This sort of issue might happen if you were running a load test with an unbounded thread pool causing high contention in the system. |
It sounds like there's probably a leak in
@ansgarstrother or @keiranmraine, can you post an example of how you're creating and managing your session objects and transactions? |
So I've done some investigation and the problem isn't that they need to be closed (as that causes problems with the object being attached to a closed session) but that the session needs to rollback or commit when the session is returned. I've not tested within this library but the following may work for a session:
Our non-cockroach approach is to create a context manager:
and use as:
Inspiration taken from here: http://docs.sqlalchemy.org/en/latest/orm/session_basics.html#when-do-i-construct-a-session-when-do-i-commit-it-and-when-do-i-close-it Not closing is intentional as otherwise lazy loading will fail on objects if used generically for read and write. |
Interesting. So if there's a lazy-loaded relationship, the objects created in the transaction are holding onto the Session for later lazy-loading? That seems like it might be an issue (but again, I never use the ORM half of SQLAlchemy so I might be misinterpreting). Sessions created by If this is the issue, then we can fix the leak by switching back to standard (non-autocommit) sessions and wrap them in an adapter object to give them the same interface as connections. However, any use of lazy loading runs the risk of getting a retryable error. I would recommend disabling lazy loading for any application using CockroachDB and always do your loading inside a transaction with |
I forgot to mention that the approach we are using is via flasks The DB state (in PostGres) ended up looking like this, note the inactive transaction connections:
|
We just switched over from using db.sessions.commit() to using run_transaction as per the diff:
cockroachdb/examples-python@4fecdf1
After making this change in our test environment after a little bit the api continues to get slower and eventually become completely bound up.
The following error eventually prints out: sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 20 reached
If there some type of clean up that needs to occur? Or is this a sign of something else?
The text was updated successfully, but these errors were encountered: