From 8262390bf09bcb03872557e8d36bbae3bf957b36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Casaj=C3=BAs?= Date: Wed, 24 Jul 2024 16:49:55 +0200 Subject: [PATCH] Close sessions between loops to make sure we leave no lock (#2162) * Close sessions between loops to make sure we leave no lock * Close at the end * Close before sleeps * Use python generic empty list in case the events is an iterator --- events/event_source.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/events/event_source.py b/events/event_source.py index 4e812ec1..89a8a4a2 100644 --- a/events/event_source.py +++ b/events/event_source.py @@ -4,6 +4,8 @@ import psycopg2 import select from abc import ABC, abstractmethod + +from app.db import Session from app.log import LOG from app.models import SyncEvent from app.events.event_dispatcher import NOTIFICATION_CHANNEL @@ -66,6 +68,7 @@ class PostgresEventSource(EventSource): LOG.info(f"Could not find event with id={notify.payload}") except Exception as e: LOG.warn(f"Error getting event: {e}") + Session.close() # Ensure we get a new connection and we don't leave a dangling tx def __connect(self): self.__connection = psycopg2.connect(self.__connection_string) @@ -97,7 +100,8 @@ class DeadLetterEventSource(EventSource): ) for event in events: on_event(event) - else: + Session.close() # Ensure that we have a new connection and we don't have a dangling tx with a lock + if not events: LOG.debug("No dead letter events") sleep(_DEAD_LETTER_INTERVAL_SECONDS) except Exception as e: