Skip to content

Commit e5130ab

Browse files
corona10diegorusso
authored andcommitted
pythongh-112087: Make list.sort to be thread-safe for PEP 703. (pythongh-116553)
1 parent 6429975 commit e5130ab

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

Objects/clinic/listobject.c.h

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/listobject.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -2577,6 +2577,7 @@ unsafe_tuple_compare(PyObject *v, PyObject *w, MergeState *ms)
25772577
* duplicated).
25782578
*/
25792579
/*[clinic input]
2580+
@critical_section
25802581
list.sort
25812582
25822583
*
@@ -2596,7 +2597,7 @@ The reverse flag can be set to sort in descending order.
25962597

25972598
static PyObject *
25982599
list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
2599-
/*[clinic end generated code: output=57b9f9c5e23fbe42 input=a74c4cd3ec6b5c08]*/
2600+
/*[clinic end generated code: output=57b9f9c5e23fbe42 input=667bf25d0e3a3676]*/
26002601
{
26012602
MergeState ms;
26022603
Py_ssize_t nremaining;
@@ -2623,7 +2624,7 @@ list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
26232624
saved_ob_item = self->ob_item;
26242625
saved_allocated = self->allocated;
26252626
Py_SET_SIZE(self, 0);
2626-
self->ob_item = NULL;
2627+
FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item, NULL);
26272628
self->allocated = -1; /* any operation will reset it to >= 0 */
26282629

26292630
if (keyfunc == NULL) {
@@ -2843,8 +2844,8 @@ list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
28432844
final_ob_item = self->ob_item;
28442845
i = Py_SIZE(self);
28452846
Py_SET_SIZE(self, saved_ob_size);
2846-
self->ob_item = saved_ob_item;
2847-
self->allocated = saved_allocated;
2847+
FT_ATOMIC_STORE_PTR_RELEASE(self->ob_item, saved_ob_item);
2848+
FT_ATOMIC_STORE_SSIZE_RELAXED(self->allocated, saved_allocated);
28482849
if (final_ob_item != NULL) {
28492850
/* we cannot use list_clear() for this because it does not
28502851
guarantee that the list is really empty when it returns */
@@ -2870,7 +2871,9 @@ PyList_Sort(PyObject *v)
28702871
PyErr_BadInternalCall();
28712872
return -1;
28722873
}
2874+
Py_BEGIN_CRITICAL_SECTION(v);
28732875
v = list_sort_impl((PyListObject *)v, NULL, 0);
2876+
Py_END_CRITICAL_SECTION();
28742877
if (v == NULL)
28752878
return -1;
28762879
Py_DECREF(v);

0 commit comments

Comments
 (0)