Skip to content

Let a subclass of LRUEngineCache that skips its constructor read and write - #303

Merged
jwrosewell merged 1 commit into
rcfrom
fix/lru-cache-lock-without-constructor
Dec 1, 2026
Merged

jwrosewell merged 1 commit into
rcfrom
fix/lru-cache-lock-without-constructor

Conversation

@jwrosewell

Copy link
Copy Markdown
Contributor

Problem

#301 gave LRUEngineCache a lock, made in its constructor, so that requests on several threads can share one cache. A subclass whose own constructor sets self.cache without calling the constructor of LRUEngineCache had no lock, so its first read or write raised AttributeError. The same subclass worked before #301.

Change

pipeline/fiftyone_pipeline_engines/src/fiftyone_pipeline_engines/lru_cache.py

  • _get_lock gives the cache's lock. When the constructor did not make one, it makes it on first use, under a lock the class holds only while making it, so every thread of one cache shares the same lock.
  • get_cache_value and set_cache_value hold the lock _get_lock gives.
  • A cache made through the constructor behaves exactly as before.

Tests

pipeline/fiftyone_pipeline_engines/tests/test_lru_cache.py adds a subclass that sets its cache itself and skips the constructor.

  • test_a_subclass_that_skips_the_constructor_reads_and_writes writes and reads one value.
  • test_its_threads_share_one_lock_made_on_first_use runs eight threads on one such cache, with a cache that records how many threads are inside it at once, and expects one.

Both fail without the change, with AttributeError. On Windows with Python 3.12, the fiftyone_pipeline_engines tests give 175 passed, which is the 173 before this change and the 2 new ones.

No version change and no +semver line.

…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.
@jwrosewell
jwrosewell merged commit 8a005b1 into rc Dec 1, 2026
6 checks passed
@jwrosewell
jwrosewell deleted the fix/lru-cache-lock-without-constructor branch December 1, 2026 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant