From 6c4f783e77b0d6ded32b77bda1c8436aeaf891ea Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Wed, 18 Feb 2026 17:13:15 -0500 Subject: [PATCH 1/2] gh-142763: Fix race in ZoneInfo cache eviction --- Lib/zoneinfo/_zoneinfo.py | 6 +++++- .../2025-12-18-00-00-00.gh-issue-142763.AJpZPVG5.rst | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-12-18-00-00-00.gh-issue-142763.AJpZPVG5.rst diff --git a/Lib/zoneinfo/_zoneinfo.py b/Lib/zoneinfo/_zoneinfo.py index 3ffdb4c837192b..bd3fefc6c9d959 100644 --- a/Lib/zoneinfo/_zoneinfo.py +++ b/Lib/zoneinfo/_zoneinfo.py @@ -47,7 +47,11 @@ def __new__(cls, key): 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 index 00000000000000..1df0b38e33bb5c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-12-18-00-00-00.gh-issue-142763.AJpZPVG5.rst @@ -0,0 +1,3 @@ +Fix a race condition in :class:`zoneinfo.ZoneInfo` cache eviction that could +raise :exc:`KeyError` when multiple threads create :class:`~zoneinfo.ZoneInfo` +instances concurrently. From 617739845de6a83d5ee930014dedc09eb79b043b Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Wed, 18 Feb 2026 17:23:28 -0500 Subject: [PATCH 2/2] Update blurb --- .../Library/2025-12-18-00-00-00.gh-issue-142763.AJpZPVG5.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 index 1df0b38e33bb5c..a5330365e3e42e 100644 --- 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 @@ -1,3 +1,2 @@ -Fix a race condition in :class:`zoneinfo.ZoneInfo` cache eviction that could -raise :exc:`KeyError` when multiple threads create :class:`~zoneinfo.ZoneInfo` -instances concurrently. +Fix a race condition between :class:`zoneinfo.ZoneInfo` creation and +:func:`zoneinfo.ZoneInfo.clear_cache` that could raise :exc:`KeyError`.