]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-127879: Fix data race in `_PyFreeList_Push` (#127880)
authorSam Gross <colesbury@gmail.com>
Thu, 12 Dec 2024 17:59:13 +0000 (17:59 +0000)
committerGitHub <noreply@github.com>
Thu, 12 Dec 2024 17:59:13 +0000 (12:59 -0500)
Writes to the `ob_tid` field need to use atomics because it may be
concurrently read by a non-locking dictionary, list, or structmember
read.

Include/internal/pycore_freelist.h

index da2d7bf6ae13939b23c8d512f2017c6e530ed4a6..84a5ab30f3eeeab294378127ab2a878d42962255 100644 (file)
@@ -51,7 +51,7 @@ static inline int
 _PyFreeList_Push(struct _Py_freelist *fl, void *obj, Py_ssize_t maxsize)
 {
     if (fl->size < maxsize && fl->size >= 0) {
-        *(void **)obj = fl->freelist;
+        FT_ATOMIC_STORE_PTR_RELAXED(*(void **)obj, fl->freelist);
         fl->freelist = obj;
         fl->size++;
         OBJECT_STAT_INC(to_freelist);