]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-142763: Fix race in ZoneInfo cache eviction (gh-144978)
authorSam Gross <colesbury@gmail.com>
Tue, 10 Mar 2026 18:47:58 +0000 (14:47 -0400)
committerGitHub <noreply@github.com>
Tue, 10 Mar 2026 18:47:58 +0000 (14:47 -0400)
The cache may be cleared between the evaluation of the if statement and the
call to popitem.

Lib/zoneinfo/_zoneinfo.py
Misc/NEWS.d/next/Library/2025-12-18-00-00-00.gh-issue-142763.AJpZPVG5.rst [new file with mode: 0644]

index 3ffdb4c837192b86fc5e61e0846cd041b5cb8a96..bd3fefc6c9d95998c389f0e207a3c7432feb763a 100644 (file)
@@ -47,7 +47,11 @@ class ZoneInfo(tzinfo):
         cls._strong_cache[key] = cls._strong_cache.pop(key, instance)
 
         if len(cls._strong_cache) > cls._strong_cache_size:
-            cls._strong_cache.popitem(last=False)
+            try:
+                cls._strong_cache.popitem(last=False)
+            except KeyError:
+                # another thread may have already emptied the cache
+                pass
 
         return instance
 
diff --git a/Misc/NEWS.d/next/Library/2025-12-18-00-00-00.gh-issue-142763.AJpZPVG5.rst b/Misc/NEWS.d/next/Library/2025-12-18-00-00-00.gh-issue-142763.AJpZPVG5.rst
new file mode 100644 (file)
index 0000000..a533036
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a race condition between :class:`zoneinfo.ZoneInfo` creation and
+:func:`zoneinfo.ZoneInfo.clear_cache` that could raise :exc:`KeyError`.