Skip to content
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

Reduce SQLAlchemy session scope to avoid stale data #2475

Merged
merged 2 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions utils/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ def process(

log.removeHandler(per_analysis_handler)

# Remove the SQLAlchemy session to ensure the next task pulls objects from
# the database, instead of relying on a potentially outdated object cache.
# Stale data can prevent SQLAlchemy from querying the database or issuing
# statements, resulting in unexpected errors and inconsistencies.
db.session.remove()


def init_worker():
signal.signal(signal.SIGINT, signal.SIG_IGN)
Expand Down
7 changes: 5 additions & 2 deletions web/web/middleware/db_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
with Database().session.begin():
return self.get_response(request)
db = Database()
with db.session.begin():
resp = self.get_response(request)
db.session.remove()
return resp