Skip to content

gh-135557: Use atomic writes on heapq operations #135601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 21, 2025
29 changes: 28 additions & 1 deletion Lib/test/test_free_threading/test_heapq.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import heapq

from enum import Enum
from threading import Thread, Barrier
from threading import Thread, Barrier, Lock
from random import shuffle, randint

from test.support import threading_helper
Expand Down Expand Up @@ -178,6 +178,33 @@ def heapreplace_max_func(max_heap, replace_items):
self.assertEqual(len(max_heap), OBJECT_COUNT)
self.test_heapq.check_max_invariant(max_heap)

def test_lock_free_list_read(self):
n, n_threads = 1_000, 10
l = []
barrier = Barrier(n_threads * 2)

count = 0
lock = Lock()

def worker():
with lock:
nonlocal count
x = count
count += 1

barrier.wait()
for i in range(n):
if x % 2:
heapq.heappush(l, 1)
heapq.heappop(l)
else:
try:
l[0]
except IndexError:
pass

self.run_concurrently(worker, (), n_threads * 2)
Copy link
Contributor

@YvesDup YvesDup Jun 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test is successful here because the worker function has no arguments.
But shouldn't the second argument args be a tuple?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't it an empty tuple here?


@staticmethod
def is_sorted_ascending(lst):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix races on :mod:`heapq` updates and :class:`list` reads on the :term:`free threaded <free threading>`
build.
37 changes: 21 additions & 16 deletions Modules/_heapqmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ annotated by François Pinard, and converted to C by Raymond Hettinger.

#include "Python.h"
#include "pycore_list.h" // _PyList_ITEMS(), _PyList_AppendTakeRef()
#include "pycore_pyatomic_ft_wrappers.h"

#include "clinic/_heapqmodule.c.h"

Expand Down Expand Up @@ -59,8 +60,8 @@ siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
arr = _PyList_ITEMS(heap);
parent = arr[parentpos];
newitem = arr[pos];
arr[parentpos] = newitem;
arr[pos] = parent;
FT_ATOMIC_STORE_PTR_RELAXED(arr[parentpos], newitem);
FT_ATOMIC_STORE_PTR_RELAXED(arr[pos], parent);
pos = parentpos;
}
return 0;
Expand Down Expand Up @@ -108,8 +109,8 @@ siftup(PyListObject *heap, Py_ssize_t pos)
/* Move the smaller child up. */
tmp1 = arr[childpos];
tmp2 = arr[pos];
arr[childpos] = tmp2;
arr[pos] = tmp1;
FT_ATOMIC_STORE_PTR_RELAXED(arr[childpos], tmp2);
FT_ATOMIC_STORE_PTR_RELAXED(arr[pos], tmp1);
pos = childpos;
}
/* Bubble it up to its final resting place (by sifting its parents down). */
Expand Down Expand Up @@ -172,8 +173,9 @@ heappop_internal(PyObject *heap, int siftup_func(PyListObject *, Py_ssize_t))
if (!n)
return lastelt;
returnitem = PyList_GET_ITEM(heap, 0);
PyList_SET_ITEM(heap, 0, lastelt);
if (siftup_func((PyListObject *)heap, 0)) {
PyListObject *list = _PyList_CAST(heap);
FT_ATOMIC_STORE_PTR_RELAXED(list->ob_item[0], lastelt);
if (siftup_func(list, 0)) {
Py_DECREF(returnitem);
return NULL;
}
Expand Down Expand Up @@ -208,8 +210,9 @@ heapreplace_internal(PyObject *heap, PyObject *item, int siftup_func(PyListObjec
}

returnitem = PyList_GET_ITEM(heap, 0);
PyList_SET_ITEM(heap, 0, Py_NewRef(item));
if (siftup_func((PyListObject *)heap, 0)) {
PyListObject *list = _PyList_CAST(heap);
FT_ATOMIC_STORE_PTR_RELAXED(list->ob_item[0], Py_NewRef(item));
if (siftup_func(list, 0)) {
Py_DECREF(returnitem);
return NULL;
}
Expand Down Expand Up @@ -284,8 +287,9 @@ _heapq_heappushpop_impl(PyObject *module, PyObject *heap, PyObject *item)
}

returnitem = PyList_GET_ITEM(heap, 0);
PyList_SET_ITEM(heap, 0, Py_NewRef(item));
if (siftup((PyListObject *)heap, 0)) {
PyListObject *list = _PyList_CAST(heap);
FT_ATOMIC_STORE_PTR_RELAXED(list->ob_item[0], Py_NewRef(item));
if (siftup(list, 0)) {
Py_DECREF(returnitem);
return NULL;
}
Expand Down Expand Up @@ -437,8 +441,8 @@ siftdown_max(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
arr = _PyList_ITEMS(heap);
parent = arr[parentpos];
newitem = arr[pos];
arr[parentpos] = newitem;
arr[pos] = parent;
FT_ATOMIC_STORE_PTR_RELAXED(arr[parentpos], newitem);
FT_ATOMIC_STORE_PTR_RELAXED(arr[pos], parent);
pos = parentpos;
}
return 0;
Expand Down Expand Up @@ -486,8 +490,8 @@ siftup_max(PyListObject *heap, Py_ssize_t pos)
/* Move the smaller child up. */
tmp1 = arr[childpos];
tmp2 = arr[pos];
arr[childpos] = tmp2;
arr[pos] = tmp1;
FT_ATOMIC_STORE_PTR_RELAXED(arr[childpos], tmp2);
FT_ATOMIC_STORE_PTR_RELAXED(arr[pos], tmp1);
pos = childpos;
}
/* Bubble it up to its final resting place (by sifting its parents down). */
Expand Down Expand Up @@ -621,8 +625,9 @@ _heapq_heappushpop_max_impl(PyObject *module, PyObject *heap, PyObject *item)
}

returnitem = PyList_GET_ITEM(heap, 0);
PyList_SET_ITEM(heap, 0, Py_NewRef(item));
if (siftup_max((PyListObject *)heap, 0) < 0) {
PyListObject *list = _PyList_CAST(heap);
FT_ATOMIC_STORE_PTR_RELAXED(list->ob_item[0], Py_NewRef(item));
if (siftup_max(list, 0) < 0) {
Py_DECREF(returnitem);
return NULL;
}
Expand Down
Loading