From 3ee5c3bcc0a3fe28b26496664b8ad657829494b3 Mon Sep 17 00:00:00 2001 From: Inex Code Date: Mon, 11 Nov 2024 20:04:00 +0300 Subject: [PATCH] fix: free unused journal.Reader instances --- selfprivacy_api/graphql/subscriptions/logs.py | 1 + selfprivacy_api/utils/systemd_journal.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/selfprivacy_api/graphql/subscriptions/logs.py b/selfprivacy_api/graphql/subscriptions/logs.py index 1e47dba..e3715bb 100644 --- a/selfprivacy_api/graphql/subscriptions/logs.py +++ b/selfprivacy_api/graphql/subscriptions/logs.py @@ -27,5 +27,6 @@ async def log_stream() -> AsyncGenerator[LogEntry, None]: yield LogEntry(entry) except Exception: asyncio.get_event_loop().remove_reader(j) + j.close() return queue.task_done() diff --git a/selfprivacy_api/utils/systemd_journal.py b/selfprivacy_api/utils/systemd_journal.py index cbb953f..9049712 100644 --- a/selfprivacy_api/utils/systemd_journal.py +++ b/selfprivacy_api/utils/systemd_journal.py @@ -42,6 +42,8 @@ def get_paginated_logs( events = get_events_from_journal(j, limit, lambda j: j.get_previous()) events.reverse() + j.close() + return events elif up_cursor is None and down_cursor is not None: j.seek_cursor(down_cursor) @@ -50,6 +52,8 @@ def get_paginated_logs( events = get_events_from_journal(j, limit, lambda j: j.get_previous()) events.reverse() + j.close() + return events elif up_cursor is not None and down_cursor is None: j.seek_cursor(up_cursor) @@ -57,8 +61,12 @@ def get_paginated_logs( events = get_events_from_journal(j, limit, lambda j: j.get_next()) + j.close() + return events else: + j.close() + raise NotImplementedError( "Pagination by both up_cursor and down_cursor is not implemented" )