Skip to content

Commit

Permalink
Merge pull request #2475 from josh-feather/cape-web-session-bug-fix
Browse files Browse the repository at this point in the history
Reduce SQLAlchemy session scope to avoid stale data
  • Loading branch information
doomedraven authored Jan 30, 2025
2 parents 510f70c + f96022f commit 0bc6dfd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
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

0 comments on commit 0bc6dfd

Please sign in to comment.