Skip to content

Commit 0f4f427

Browse files
committed
gh-125243: Fix ZoneInfo data race in free threading build
Lock `ZoneInfoType` to protect accesses to `ZONEINFO_STRONG_CACHE`. Refactor the `tp_new` handler to use Argument Clinic so that we can just use `@critical_section` annotations on the relevant functions. Also use `PyDict_SetDefaultRef` instead of `PyDict_SetDefault` when inserting into the `TIMEDELTA_CACHE`.
1 parent dd0ee20 commit 0f4f427

File tree

3 files changed

+84
-13
lines changed

3 files changed

+84
-13
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix data race when creating :class:`zoneinfo.ZoneInfo` objects in the free
2+
threading build.

Modules/_zoneinfo.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#endif
44

55
#include "Python.h"
6+
#include "pycore_critical_section.h" // _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED()
67
#include "pycore_long.h" // _PyLong_GetOne()
78
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
89

@@ -298,15 +299,20 @@ get_weak_cache(zoneinfo_state *state, PyTypeObject *type)
298299
}
299300
}
300301

302+
/*[clinic input]
303+
@critical_section
304+
@classmethod
305+
zoneinfo.ZoneInfo.__new__
306+
307+
key: object
308+
309+
Create a new ZoneInfo instance.
310+
[clinic start generated code]*/
311+
301312
static PyObject *
302-
zoneinfo_new(PyTypeObject *type, PyObject *args, PyObject *kw)
313+
zoneinfo_ZoneInfo_impl(PyTypeObject *type, PyObject *key)
314+
/*[clinic end generated code: output=95e61dab86bb95c3 input=4619eb0305327e83]*/
303315
{
304-
PyObject *key = NULL;
305-
static char *kwlist[] = {"key", NULL};
306-
if (PyArg_ParseTupleAndKeywords(args, kw, "O", kwlist, &key) == 0) {
307-
return NULL;
308-
}
309-
310316
zoneinfo_state *state = zoneinfo_get_state_by_self(type);
311317
PyObject *instance = zone_from_strong_cache(state, type, key);
312318
if (instance != NULL || PyErr_Occurred()) {
@@ -467,6 +473,7 @@ zoneinfo_ZoneInfo_no_cache_impl(PyTypeObject *type, PyTypeObject *cls,
467473
}
468474

469475
/*[clinic input]
476+
@critical_section
470477
@classmethod
471478
zoneinfo.ZoneInfo.clear_cache
472479
@@ -481,7 +488,7 @@ Clear the ZoneInfo cache.
481488
static PyObject *
482489
zoneinfo_ZoneInfo_clear_cache_impl(PyTypeObject *type, PyTypeObject *cls,
483490
PyObject *only_keys)
484-
/*[clinic end generated code: output=114d9b7c8a22e660 input=e32ca3bb396788ba]*/
491+
/*[clinic end generated code: output=114d9b7c8a22e660 input=35944715df26d24e]*/
485492
{
486493
zoneinfo_state *state = zoneinfo_get_state_by_cls(cls);
487494
PyObject *weak_cache = get_weak_cache(state, type);
@@ -821,7 +828,7 @@ zoneinfo_ZoneInfo__unpickle_impl(PyTypeObject *type, PyTypeObject *cls,
821828
return NULL;
822829
}
823830

824-
PyObject *rv = zoneinfo_new(type, val_args, NULL);
831+
PyObject *rv = zoneinfo_ZoneInfo(type, val_args, NULL);
825832

826833
Py_DECREF(val_args);
827834
return rv;
@@ -858,8 +865,7 @@ load_timedelta(zoneinfo_state *state, long seconds)
858865
0, seconds, 0, 1, PyDateTimeAPI->DeltaType);
859866

860867
if (tmp != NULL) {
861-
rv = PyDict_SetDefault(state->TIMEDELTA_CACHE, pyoffset, tmp);
862-
Py_XINCREF(rv);
868+
PyDict_SetDefaultRef(state->TIMEDELTA_CACHE, pyoffset, tmp, &rv);
863869
Py_DECREF(tmp);
864870
}
865871
}
@@ -2368,6 +2374,7 @@ strong_cache_free(StrongCacheNode *root)
23682374
static void
23692375
remove_from_strong_cache(zoneinfo_state *state, StrongCacheNode *node)
23702376
{
2377+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(state->ZoneInfoType);
23712378
if (state->ZONEINFO_STRONG_CACHE == node) {
23722379
state->ZONEINFO_STRONG_CACHE = node->next;
23732380
}
@@ -2422,6 +2429,7 @@ eject_from_strong_cache(zoneinfo_state *state, const PyTypeObject *const type,
24222429
return 0;
24232430
}
24242431

2432+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(state->ZoneInfoType);
24252433
StrongCacheNode *cache = state->ZONEINFO_STRONG_CACHE;
24262434
StrongCacheNode *node = find_in_strong_cache(cache, key);
24272435
if (node != NULL) {
@@ -2478,6 +2486,7 @@ zone_from_strong_cache(zoneinfo_state *state, const PyTypeObject *const type,
24782486
return NULL; // Strong cache currently only implemented for base class
24792487
}
24802488

2489+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(state->ZoneInfoType);
24812490
StrongCacheNode *cache = state->ZONEINFO_STRONG_CACHE;
24822491
StrongCacheNode *node = find_in_strong_cache(cache, key);
24832492

@@ -2504,6 +2513,7 @@ update_strong_cache(zoneinfo_state *state, const PyTypeObject *const type,
25042513
return;
25052514
}
25062515

2516+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(state->ZoneInfoType);
25072517
StrongCacheNode *new_node = strong_cache_node_new(key, zone);
25082518
if (new_node == NULL) {
25092519
return;
@@ -2631,7 +2641,7 @@ static PyType_Slot zoneinfo_slots[] = {
26312641
{Py_tp_getattro, PyObject_GenericGetAttr},
26322642
{Py_tp_methods, zoneinfo_methods},
26332643
{Py_tp_members, zoneinfo_members},
2634-
{Py_tp_new, zoneinfo_new},
2644+
{Py_tp_new, zoneinfo_ZoneInfo},
26352645
{Py_tp_dealloc, zoneinfo_dealloc},
26362646
{Py_tp_traverse, zoneinfo_traverse},
26372647
{Py_tp_clear, zoneinfo_clear},

Modules/clinic/_zoneinfo.c.h

Lines changed: 60 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)