-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-116738: Make _suggestions module thread-safe #140321
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
yoney
wants to merge
2
commits into
python:main
Choose a base branch
from
yoney:ft_suggestions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+33
−3
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import unittest | ||
|
||
from test.support import import_helper, threading_helper | ||
from test.support.threading_helper import run_concurrently | ||
|
||
suggestions = import_helper.import_module("_suggestions") | ||
|
||
NTHREADS = 10 | ||
|
||
|
||
@threading_helper.requires_working_threading() | ||
class SuggestionsTests(unittest.TestCase): | ||
def test_generate_suggestions(self): | ||
candidates = [str(i) for i in range(100)] | ||
|
||
def worker(): | ||
_ = suggestions._generate_suggestions(candidates, "42") | ||
candidates.clear() | ||
|
||
run_concurrently(worker_func=worker, nthreads=NTHREADS) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Core_and_Builtins/2025-10-18-19-52-20.gh-issue-116738.NLJW0L.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Make _suggestions module thread-safe on the :term:`free threaded <free | ||
threading>` build. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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_section
and 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 thecandidates
list 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_section
makes the code a bit easier to read and reason about, since thecandidates
list 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
?