From 71f274a8c9ccc619f2e20372d24e5c232aaee1a4 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Tue, 1 Dec 2026 20:00:03 +0000 Subject: [PATCH] Let a subclass of LRUEngineCache that skips its constructor read and write LRUEngineCache gained a lock in its constructor. A subclass whose own constructor sets the cache without calling that constructor had no lock, so its first read or write raised AttributeError. The lock is now made on first use when the constructor did not make it, once per cache however many threads ask at the same time. Two tests cover a subclass that skips the constructor. Both fail without the change. --- .../fiftyone_pipeline_engines/lru_cache.py | 22 ++++++++++++-- .../tests/test_lru_cache.py | 30 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/pipeline/fiftyone_pipeline_engines/src/fiftyone_pipeline_engines/lru_cache.py b/pipeline/fiftyone_pipeline_engines/src/fiftyone_pipeline_engines/lru_cache.py index 43208946..7e01b804 100644 --- a/pipeline/fiftyone_pipeline_engines/src/fiftyone_pipeline_engines/lru_cache.py +++ b/pipeline/fiftyone_pipeline_engines/src/fiftyone_pipeline_engines/lru_cache.py @@ -33,6 +33,11 @@ class LRUEngineCache(DataKeyedCache): one thread at a time reads or writes the cache, which lets requests processed on several threads share it. """ + + # Held only while a cache makes its own lock, for a subclass whose + # constructor sets the cache without calling this constructor. + _making_lock = threading.Lock() + def __init__(self, size = 1000): """! Constructor for LRUCache @@ -43,12 +48,25 @@ def __init__(self, size = 1000): self.cache = LRUCache(maxsize=size) self._lock = threading.Lock() + def _get_lock(self): + + # A subclass may not have called the constructor, so the lock is + # made on first use, once, however many threads ask at the same time. + lock = getattr(self, "_lock", None) + if lock is None: + with LRUEngineCache._making_lock: + lock = getattr(self, "_lock", None) + if lock is None: + lock = threading.Lock() + self._lock = lock + return lock + def get_cache_value (self, cache_key): - with self._lock: + with self._get_lock(): return self.cache.get(cache_key) def set_cache_value(self, key, value): - with self._lock: + with self._get_lock(): self.cache[key] = value diff --git a/pipeline/fiftyone_pipeline_engines/tests/test_lru_cache.py b/pipeline/fiftyone_pipeline_engines/tests/test_lru_cache.py index 4788c52b..a25f76d3 100644 --- a/pipeline/fiftyone_pipeline_engines/tests/test_lru_cache.py +++ b/pipeline/fiftyone_pipeline_engines/tests/test_lru_cache.py @@ -164,5 +164,35 @@ def work(number): self.assertIsNone(cache.get_cache_value(0)) +class OwnCacheEngineCache(LRUEngineCache): + """A subclass that sets its cache itself and does not call the + constructor of LRUEngineCache.""" + + def __init__(self, cache): + self.cache = cache + + +class SubclassWithoutConstructorTests(unittest.TestCase): + + def test_a_subclass_that_skips_the_constructor_reads_and_writes(self): + cache = OwnCacheEngineCache(LRUCache(maxsize=CACHE_SIZE)) + cache.set_cache_value(1, value_of(1)) + self.assertEqual(value_of(1), cache.get_cache_value(1)) + self.assertIsNone(cache.get_cache_value(2)) + + def test_its_threads_share_one_lock_made_on_first_use(self): + recording = OverlapRecordingCache(CACHE_SIZE) + cache = OwnCacheEngineCache(recording) + + def work(number): + for call in range(CALLS_PER_THREAD): + key = (number + call) % KEYS + cache.set_cache_value(key, value_of(key)) + cache.get_cache_value(key) + + self.assertEqual([], run_threads(work)) + self.assertEqual(1, recording.most_inside) + + if __name__ == "__main__": unittest.main()