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: