Fix memory leak in `ExclusiveResourceProcessor.__exit__`

The ``_resource_timestamps`` class-level dict was never cleaned up
when ``__exit__`` was called on the primary processor, causing stale
entries to accumulate over the lifetime of the process. Add cleanup of
``_resource_timestamps`` in ``__exit__`` and corresponding unit tests.

Closes-Bug: #2146426
Signed-off-by: Rodolfo Alonso Hernandez <ralonsoh@redhat.com>
Change-Id: If345911483f7a15965c475dd4bbb6c3f5dc1df89
This commit is contained in:
Rodolfo Alonso Hernandez
2026-03-26 09:10:20 +01:00
parent b3bb0cc335
commit 1c3b4bb63e
3 changed files with 31 additions and 0 deletions
@@ -124,6 +124,7 @@ class ExclusiveResourceProcessor:
def __exit__(self, type, value, traceback):
if self._i_am_primary():
del self._primaries[self._id]
self._resource_timestamps.pop(self._id, None)
def _get_resource_data_timestamp(self):
return self._resource_timestamps.get(self._id,
@@ -76,6 +76,29 @@ class TestExclusiveResourceProcessor(base.BaseTestCase):
primary.__exit__(None, None, None)
self.assertNotIn(FAKE_ID, queue.ExclusiveResourceProcessor._primaries)
def test__exit__cleans_resource_timestamps(self):
with queue.ExclusiveResourceProcessor(FAKE_ID) as primary:
primary.fetched_and_processed(timeutils.utcnow())
self.assertIn(
FAKE_ID,
queue.ExclusiveResourceProcessor._resource_timestamps)
self.assertNotIn(
FAKE_ID,
queue.ExclusiveResourceProcessor._resource_timestamps)
def test__exit__non_primary_does_not_clean_resource_timestamps(self):
primary = queue.ExclusiveResourceProcessor(FAKE_ID)
primary.fetched_and_processed(timeutils.utcnow())
not_primary = queue.ExclusiveResourceProcessor(FAKE_ID)
not_primary.__exit__(None, None, None)
self.assertIn(
FAKE_ID,
queue.ExclusiveResourceProcessor._resource_timestamps)
primary.__exit__(None, None, None)
self.assertNotIn(
FAKE_ID,
queue.ExclusiveResourceProcessor._resource_timestamps)
def test_data_fetched_since(self):
primary = queue.ExclusiveResourceProcessor(FAKE_ID)
self.assertEqual(datetime.datetime.min,
@@ -0,0 +1,7 @@
---
fixes:
- |
Fixed a memory leak in ``ExclusiveResourceProcessor`` where the
``_resource_timestamps`` class-level dictionary was never cleaned up
when the primary processor exited. Over time, this caused stale
entries to accumulate for every resource ID that was ever processed.