Skip to content

Commit 6c4f783

Browse files
committed
gh-142763: Fix race in ZoneInfo cache eviction
1 parent 0bbdb4e commit 6c4f783

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a race condition in :class:`zoneinfo.ZoneInfo` cache eviction that could
2+
raise :exc:`KeyError` when multiple threads create :class:`~zoneinfo.ZoneInfo`
3+
instances concurrently.

0 commit comments

Comments
 (0)