Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about using PyList_GetItemRef with this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@corona10, Thank you so much for your comment!
Just to make sure I understand your suggestion correctly, did you mean:
PyList_GetItemRef()instead ofPyList_GET_ITEM()within the currentcritical_section?critical_sectionand usePyList_GetItemRef()in both loops I mentioned in the description?Regarding 1: I replaced
PyList_GetItem()withPyList_GET_ITEM()because, in the with-GIL build, it’s already assumed thatPyList_GetItem()cannot fail, so the returned value isn't checked. Within thecritical_section, the assumption is that thecandidateslist cannot be mutated, andPyList_GET_ITEM()avoids extra atomic operations.Regarding 2: I think this could be implemented with
PyList_GetItemRef()and nocritical_section, as long as errors returned fromPyList_GetItemRef()are properly handled. However, in my opinion, using acritical_sectionmakes the code a bit easier to read and reason about, since thecandidateslist cannot be mutated in this context. This means we don’t need to consider update cases between blocks or iterations.What do you think, would it be better to use
PyList_GetItemRef()here?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yoney
Hmm, even if you use PyList_GET_ITEM, it won’t check whether the returned value is NULL, so it won’t actually fix the UB you mentioned. What do you think?
cpython/Include/cpython/listobject.h
Line 40 in f11ec6e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@corona10
Sorry, my previous comment wasn't clear before. I was referring to cases where
PyList_GetItem()could fail due toPyList_Check()orvalid_index(), particularly in the free-threading build. I was trying to explain why we need acritical_section.cpython/Objects/listobject.c
Lines 384 to 397 in f11ec6e
These properties are checked in
_suggestions__generate_suggestions_impl()earlier, before callingPyList_GetItem(), and the GIL or thecritical_section"protects" them. That's why I replacedPyList_GetItem()withPyList_GET_ITEM(). However, the actual element of the list could still beNULL. I had assumed this wasn’t possible, but should we check if the element isNULL?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I get it, looks good to me. Thank you for explain