Skip to content

gh-116738: Make _heapq module thread-safe #135036

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

Conversation

yoney
Copy link
Contributor

@yoney yoney commented Jun 2, 2025

This uses critical sections to make heapq methods that update the heap thread-safe when the GIL is disabled. This is accomplished by using the @critical_section clinic directive.

cc: @mpage @colesbury

@python-cla-bot
Copy link

python-cla-bot bot commented Jun 2, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

Copy link
Contributor

@StanFromIreland StanFromIreland left a comment

Choose a reason for hiding this comment

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

There is a lot of duplication in tests because of min/max heaps, why not organize with subtests?

Also for testing the heap, it is probably best to reuse the existing methods from test_heapq

def check_invariant(self, heap):
# Check the heap invariant.
for pos, item in enumerate(heap):
if pos: # pos 0 has no parent
parentpos = (pos-1) >> 1
self.assertTrue(heap[parentpos] <= item)
def check_max_invariant(self, heap):
for pos, item in enumerate(heap[1:], start=1):
parentpos = (pos - 1) >> 1
self.assertGreaterEqual(heap[parentpos], item)

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

I think it would also be a good idea to get rid of the borrowed reference usage here.

@yoney yoney marked this pull request as ready for review June 2, 2025 17:38
@yoney yoney requested a review from rhettinger as a code owner June 2, 2025 17:38
@colesbury colesbury added the needs backport to 3.14 bugs and security fixes label Jun 2, 2025
@colesbury
Copy link
Contributor

The Modules/_heapqmodule.c change looks good to me. I haven't looked through the tests yet. I don't think should change the implementation to avoid borrowed references: I'd rather keep the change small and limited to the thread safety fix rather than try to "clean" things up, and I'm not really convinced that avoiding borrowed references here would make things better.

@yoney - would you please add add a NEWS entry via blurb. You can use uvx blurb, if you have the uv tools, or pip install it, or blurb-it.

@ZeroIntensity
Copy link
Member

I'd rather keep the change small and limited to the thread safety fix rather than try to "clean" things up, and I'm not really convinced that avoiding borrowed references here would make things better.

Ok, but we should definitely do this in a follow-up (possibly only for 3.15). There are definitely some things here that aren't safe. For example:

lastelt = PyList_GET_ITEM(heap, n-1) ;
Py_INCREF(lastelt);
if (PyList_SetSlice(heap, n-1, n, NULL)) {
    Py_DECREF(lastelt);
    return NULL;
}
n--;

if (!n)
    return lastelt;
returnitem = PyList_GET_ITEM(heap, 0);

A finalizer could either release the critical section or explicitly clear the list, which could cause that PyList_GET_ITEM call to return NULL. I guess that's not related to borrowed references, though--more of a problem with PyList_GET_ITEM not doing validation.

There's also some incredibly horrible things going on, like this:

returnitem = PyList_GET_ITEM(heap, 0);
PyList_SET_ITEM(heap, 0, lastelt);

returnitem starts out as a borrowed reference, but then has the ownership implicitly handed off through PyList_SET_ITEM, which doesn't decref it.

@yoney
Copy link
Contributor Author

yoney commented Jun 2, 2025

Ok, but we should definitely do this in a follow-up (possibly only for 3.15). There are definitely some things here that aren't safe.

@ZeroIntensity I agree that there are things we should follow up on. I initially tried to address some of them as part of the free-threading change, but it introduces complexity to the review and makes the free-threading change harder to review, so I decided to follow up on those issues separately.

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

Nitpicks

@rhettinger rhettinger removed their request for review June 3, 2025 05:20
@yoney
Copy link
Contributor Author

yoney commented Jun 3, 2025

There is a lot of duplication in tests because of min/max heaps, why not organize with subtests?

@StanFromIreland Thank you so much for your review! I've already refactored the code and moved some repeated parts into separate functions while addressing the other comments. I'm not sure if subtests will provide more code reuse here. What do you think?

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

LGTM with two tiny complaints

if (PyList_Append(heap, item))
// In a free-threaded build, the heap is locked at this point.
// Therefore, calling _PyList_AppendTakeRef() is safe and no overhead.
if (_PyList_AppendTakeRef((PyListObject *)heap, Py_XNewRef(item)))
Copy link
Member

Choose a reason for hiding this comment

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

Wait, why is this now XNewRef? item can never be NULL.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure about "item can never be NULL" and in the previous version, PyList_Append() had a NULL check. That's why I thought this would be safe. However, _PyList_AppendTakeRef() expects a non-NULL value for item so it doesn't really make it safer.

Can I assume that "item can never be NULL" because _heapq_heappush_impl() is called from clinic-generated code?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it might be possible for item to be NULL if you vectorcall this from a C extension? If we want to mirror the previous implementation we should add a check that item is not NULL:

if (newitem == NULL) {
    PyErr_BadInternalCall();
    return NULL
}

Copy link
Member

Choose a reason for hiding this comment

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

I think it might be possible for item to be NULL if you vectorcall this from a C extension?

Well, that would definitely be the user's fault--you can't pass NULL to a vectorcall arg. I think adding the NULL check here is additional work that isn't necessary. We don't do this for anything else that uses AC, right?

Copy link
Contributor

@mpage mpage Jun 5, 2025

Choose a reason for hiding this comment

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

I think adding the NULL check here is additional work that isn't necessary. We don't do this for anything else that uses AC, right?

The previous implementation that used PyList_Append performed the NULL check (in PyList_Append). If we want to match the existing behavior we should include the NULL check here as well. I would err on the side of being conservative and include it unless we are guaranteed to never see a NULL item (for example if something higher up in the call stack already checks for it).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mpage @ZeroIntensity
Thank you!
I've added the NULL check, which aligns better with the previous version. However, if we decide that we can assume the value cannot be NULL, I can remove it.

if (PyList_Append(heap, item))
// In a free-threaded build, the heap is locked at this point.
// Therefore, calling _PyList_AppendTakeRef() is safe and no overhead.
if (_PyList_AppendTakeRef((PyListObject *)heap, Py_XNewRef(item)))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it might be possible for item to be NULL if you vectorcall this from a C extension? If we want to mirror the previous implementation we should add a check that item is not NULL:

if (newitem == NULL) {
    PyErr_BadInternalCall();
    return NULL
}

Copy link
Contributor

@mpage mpage left a comment

Choose a reason for hiding this comment

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

Looks great! Will merge on Monday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants