Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

thread-safety issue in pure-Pythonzoneinfo cache on 3.13t #142763

Closed
Labels
3.13bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-free-threadingtype-bugAn unexpected behavior, bug, or error
@ariebovenberg

Description

@ariebovenberg

Bug report

Bug description:

Summary

There seems to be a race condition in the cache underlying the pure PythonZoneInfo implementation, which causes errors in the latest 3.13t builds. It appears to have been fixed indirectly in 3.14t. Since 3.13t is still in bugfix mode, it might be worth patching.

I’m running on 3.13.11t specifically, under MacOS 26.2

Details

The zoneinfo cache uses an OrderedDict. Unlikedict, AFAIKOrderedDict is not thread-safe (at least, its methods defined in Python are not):

cls._strong_cache.popitem(last=False)

Thepopitem() method is defined in Python, and runs into problems when hit concurrently.

A minimal-ish demo. When run, it results in lots ofKeyError and it often doesn’t terminate. In rare cases it even segfaults.

  File "/Users/arie/code/sandbox313-nogil/zoneinfo_crash.py", line 52, in touch_timezones    zdt = ZoneInfo(tz)  File "/Users/arie/.pyenv/versions/3.13.11t/lib/python3.13t/zoneinfo/_zoneinfo.py", line 50, in __new__    cls._strong_cache.popitem(last=False)    ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^KeyError: 'Africa/Brazzaville'
fromthreadingimportThreadfromzoneinfo._zoneinfoimportZoneInfo# the pure Python versionNUM_THREADS=16NUM_ITERATIONS=100TIMEZONE_SAMPLE= ["UTC","America/Guyana","Etc/GMT-11","Europe/Vienna","America/Rainy_River","Asia/Ulaanbaatar","US/Alaska","America/Rankin_Inlet","Arctic/Longyearbyen","Pacific/Bougainville","Africa/Monrovia","Europe/Copenhagen","America/Hermosillo","Africa/Brazzaville","Asia/Tashkent","Pacific/Saipan","Europe/Tallinn","Europe/Uzhgorod","Africa/Nairobi","America/Argentina/Ushuaia","Brazil/Acre",]TZS=TIMEZONE_SAMPLE* (NUM_THREADS*NUM_ITERATIONS)deftouch_timezones(tzs):"""A minimal function that triggers a timezone lookup"""fortzintzs:zdt=ZoneInfo(tz)delzdtdefmain(func):threads= []forninrange(NUM_THREADS):thread=Thread(target=func,args=(TZS[n::NUM_THREADS],))threads.append(thread)thread.start()forthreadinthreads:thread.join()if__name__=="__main__":main(touch_timezones)

Potential fixes

A sensible solution seems to synchronize all access to_strong_cache using aLock. Side note: zoneinfo has another cache, but it usesWeakValueDictionary, whichis thread-safe.

While I reliably get issues on 3.13.11t, it appears to be fixed in 3.14. Looking at the diff, both the zoneinfo and OrderedDict code remains mostly the same between these two versions. It might have been fixed due to changes in dictobject.c

CPython versions tested on:

3.13

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-free-threadingtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2026 Movatter.jp