Skip to content

Commit af1ad53

Browse files
[3.13] gh-142763: Fix race in ZoneInfo cache eviction (gh-144978) (#145782)
The cache may be cleared between the evaluation of the if statement and the call to popitem. (cherry picked from commit 665c1db) Co-authored-by: Sam Gross <colesbury@gmail.com>
1 parent 37e9d84 commit af1ad53

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)