Skip to content

[3.13] gh-127521: Mark list as "shared" before resizing if necessary (GH-127524) #127533

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 1 commit into from
Dec 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ free_list_items(PyObject** items, bool use_qsbr)
#endif
}

static void
ensure_shared_on_resize(PyListObject *self)
{
#ifdef Py_GIL_DISABLED
// Ensure that the list array is freed using QSBR if we are not the
// owning thread.
if (!_Py_IsOwnedByCurrentThread((PyObject *)self) &&
!_PyObject_GC_IS_SHARED(self))
{
_PyObject_GC_SET_SHARED(self);
}
#endif
}

/* Ensure ob_item has room for at least newsize elements, and set
* ob_size to newsize. If newsize > ob_size on entry, the content
* of the new slots at exit is undefined heap trash; it's the caller's
Expand Down Expand Up @@ -126,6 +140,8 @@ list_resize(PyListObject *self, Py_ssize_t newsize)
if (newsize == 0)
new_allocated = 0;

ensure_shared_on_resize(self);

#ifdef Py_GIL_DISABLED
_PyListArray *array = list_allocate_array(new_allocated);
if (array == NULL) {
Expand Down Expand Up @@ -842,6 +858,9 @@ list_clear_impl(PyListObject *a, bool is_resize)
Py_XDECREF(items[i]);
}
#ifdef Py_GIL_DISABLED
if (is_resize) {
ensure_shared_on_resize(a);
}
bool use_qsbr = is_resize && _PyObject_GC_IS_SHARED(a);
#else
bool use_qsbr = false;
Expand Down Expand Up @@ -3107,6 +3126,7 @@ list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
Py_XDECREF(final_ob_item[i]);
}
#ifdef Py_GIL_DISABLED
ensure_shared_on_resize(self);
bool use_qsbr = _PyObject_GC_IS_SHARED(self);
#else
bool use_qsbr = false;
Expand Down
Loading