gh-145142: Make str.maketrans safe under free-threading#145157
Open
VanshAgarwal24036 wants to merge 2 commits intopython:mainfrom
Open
gh-145142: Make str.maketrans safe under free-threading#145157VanshAgarwal24036 wants to merge 2 commits intopython:mainfrom
VanshAgarwal24036 wants to merge 2 commits intopython:mainfrom
Conversation
colesbury
reviewed
Feb 23, 2026
Comment on lines
+8
to
+9
| @threading_helper.reap_threads | ||
| def test_maketrans_dict_concurrent_modification(self): |
Contributor
There was a problem hiding this comment.
Move this to test_free_threading/test_str.py and remove the @threading_helper.reap_threads -- it's not needed
| } | ||
| PyObject *items = PyDict_Items(x); | ||
| if(items == NULL) goto err; | ||
| Py_ssize_t n = PyList_GET_SIZE(items); |
Contributor
There was a problem hiding this comment.
Use a critical section here instead of copying the items to a list. You'll need to fix up the goto err. Something like:
{
...
Py_BEGIN_CRITICAL_SECTION(x);
while (PyDict_Next(x, &i, &key, &value)) {
...
}
goto done;
err:
Py_CLEAR(new);
done:
Py_END_CRITICAL_SECTION();
return new;
}| @@ -0,0 +1,3 @@ | |||
| Fix a crash in free-threaded builds when str.maketrans() iterates over a | |||
| dictionary that is concurrently modified. Wrap PyDict_Next iteration in a | |||
Contributor
There was a problem hiding this comment.
Fix a crash in the free-threaded build when the dictionary argument to
:meth:`str.maketrans()` is concurrently modified.
We don't need to include implementation details about the critical section in the NEWS blurb.
| def test_maketrans_dict_concurrent_modification(self): | ||
| number_of_iterations = 5 | ||
| number_of_attempts = 100 | ||
| for _ in range(number_of_iterations): |
Contributor
There was a problem hiding this comment.
Just for _ in range(5):. Same with the loop below. Don't bother to assign the iteration count to local variables.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Replace unsafe PyDict_Next iteration with snapshot iteration using PyDict_Items to avoid crashes when the input dict is mutated concurrently under free-threading.
Adds a regression test.