Skip to content

Commit 665c1db

Browse files
authored
gh-142763: Fix race in ZoneInfo cache eviction (gh-144978)
The cache may be cleared between the evaluation of the if statement and the call to popitem.
1 parent 4d0dce0 commit 665c1db

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

Lib/zoneinfo/_zoneinfo.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ def __new__(cls, key):
4747
cls._strong_cache[key] = cls._strong_cache.pop(key, instance)
4848

4949
if len(cls._strong_cache) > cls._strong_cache_size:
50-
cls._strong_cache.popitem(last=False)
50+
try:
51+
cls._strong_cache.popitem(last=False)
52+
except KeyError:
53+
# another thread may have already emptied the cache
54+
pass
5155

5256
return instance
5357

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a race condition between :class:`zoneinfo.ZoneInfo` creation and
2+
:func:`zoneinfo.ZoneInfo.clear_cache` that could raise :exc:`KeyError`.

0 commit comments

Comments
 (0)