Skip to content

Commit a214abd

Browse files
authored
Merge pull request #3203 from Zac-HD/lru-keyerror
Fix LR-reused cache bug
2 parents f91adfe + 5673d8a commit a214abd

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

hypothesis-python/RELEASE.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch fixes :issue:`3169`, an extremely rare bug which would
4+
trigger if an internal least-recently-reused cache dropped a newly
5+
added entry immediately after it was added.

hypothesis-python/src/hypothesis/internal/cache.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,16 @@ def on_access(self, key, value, score):
262262
score[0] = 2
263263
score[1] = self.tick()
264264
return score
265+
266+
def pin(self, key):
267+
try:
268+
super().pin(key)
269+
except KeyError:
270+
# The whole point of an LRU cache is that it might drop things for you
271+
assert key not in self.keys_to_indices
272+
273+
def unpin(self, key):
274+
try:
275+
super().unpin(key)
276+
except KeyError:
277+
assert key not in self.keys_to_indices

hypothesis-python/tests/cover/test_cache_implementation.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,19 @@ def target():
316316
worker.join()
317317

318318
assert not errors
319+
320+
321+
def test_pin_and_unpin_are_noops_if_dropped():
322+
# See https://github.com/HypothesisWorks/hypothesis/issues/3169
323+
cache = LRUReusedCache(max_size=10)
324+
cache[30] = True
325+
assert 30 in cache
326+
327+
for i in range(20):
328+
cache[i] = False
329+
330+
assert 30 not in cache
331+
cache.pin(30)
332+
assert 30 not in cache
333+
cache.unpin(30)
334+
assert 30 not in cache

0 commit comments

Comments
 (0)