question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

500 errors after a few days, giving PendingRollback error

See original GitHub issue

I have a deployment of flask-admin running on a fairly large DB and the interface is used by multiple users.

Every few days, all pages lock up with a 500 error, perhaps because of some race condition where multiple users edit the same thing;

 "Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/local/lib/python3.9/site-packages/flask_admin/base.py", line 69, in inner
    return self._run_view(f, *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/flask_admin/base.py", line 368, in _run_view
    return fn(self, *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/flask_admin/model/base.py", line 1969, in index_view
    count, data = self.get_list(view_args.page, sort_column, view_args.sort_desc,
  File "/usr/local/lib/python3.9/site-packages/flask_admin/contrib/sqla/view.py", line 1065, in get_list
    count = count_query.scalar() if count_query else None
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/query.py", line 2718, in scalar
    ret = self.one()
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/query.py", line 2695, in one
    return self._iter().one()
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/query.py", line 2733, in _iter
    result = self.session.execute(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 1530, in execute
    result = conn._execute_20(statement, params or {}, execution_options)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1443, in _execute_20
    return meth(self, args_10style, kwargs_10style, execution_options)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/elements.py", line 310, in _execute_on_connection
    return connection._execute_clauseelement(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1312, in _execute_clauseelement
    ret = self._execute_context(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1529, in _execute_context
    conn = self._revalidate_connection()
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 508, in _revalidate_connection
    self._invalid_transaction()
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 481, in _invalid_transaction
    raise exc.PendingRollbackError(
sqlalchemy.exc.PendingRollbackError: Can't reconnect until invalid transaction is rolled back. (Background on this error at: http://sqlalche.me/e/14/8s2b)
"  

It seems that sometimes transactions fail and the session gets stuck in an bad state forever.

I imagine this can be resolved by using a new session for every request and tearing down the sessions at the end, but that might be wasteful and leaky. I also can’t find a good way to set that up, as the flask-admin init already wants the session.

Does anyone have any good ideas on how to fix this? I can’t imagine I’m the only one running into this issue.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gboorcommented, May 10, 2021

Hi @mrjoes,

I was not aware of flask-sqlalchemy doing the whole scoped thing. I switched to flask-sqlalchemy instead of using raw sessions and now everything seems to work fine. Thanks for pointing that one out!

0reactions
mrjoescommented, May 10, 2021

Yeah, you were starving your connection pool by not releasing sessions (which, I think, does an explicit rollback on a used connection).

Read more comments on GitHub >

github_iconTop Results From Across the Web

500 errors after a few days, giving PendingRollback error issue
I have a deployment of flask-admin running on a fairly large DB and the interface is used by multiple users. Every few days,...
Read more >
How to Fix a 500 Internal Server Error on Your WordPress Site
The 500 Internal Server Error status code occurs when the server encounters an error that prevents it from fulfilling the request.
Read more >
Why do I get SQLAlchemy nested rollback error?
The problem was solved. The point, in this case, is that rollback is not executed until we call rollback explicitly, so when we...
Read more >
What Is a 500 Internal Server Error, and How Do I Fix It?
If you try to visit a website and see a “500 Internal Server Error” message, it means something has gone wrong with the...
Read more >
How to Fix 500 Internal Server Error - Sunny HQ
500 server errors are almost inevitable, so many websites customize their error messages to explain to visitors why the page isn't working.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found