gh-145446: Add critical section in functools module for PyDict_Next#145487
gh-145446: Add critical section in functools module for PyDict_Next#145487encukou merged 5 commits into
PyDict_Next#145487Conversation
vstinner
left a comment
There was a problem hiding this comment.
LGTM
Sorry, something went wrong.
colesbury
left a comment
There was a problem hiding this comment.
You don't need to lock keyword arguments dictionaries. They're not exposed to other threads and cannot be modified concurrently.
This is true even if you splat a dict: foo(**mydict). The called function gets a copy of mydict.
Sorry, something went wrong.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Sorry, something went wrong.
|
I have made the requested changes; please review again |
Sorry, something went wrong.
|
Thanks for making the requested changes! @vstinner, @colesbury: please review the changes made to this pull request. |
Sorry, something went wrong.
|
I don't think the remaining critical sections are needed either. |
Sorry, something went wrong.
|
Okay, after thinking about it I see what you mean. I can close the PR if the critical sections are not needed. |
Sorry, something went wrong.
|
@colesbury After further reflection, I actually think that you need the critical section in the remaining cases. For example, without the critical section, this code results in a data race: from functools import partial
from threading import Thread
p = partial(lambda: None)
def f():
for _ in range(100):
repr(p)
def g():
for i in range(100):
p.keywords[f"{i}"] = i
t1 = Thread(target=f)
t2 = Thread(target=g)
t1.start()
t2.start()
t1.join()
t2.join()I attached the full TSan report here although it is quite long and I don't think it is that illuminating. However, the only remaining cases are for the Let me know if you think it is worth continuing this PR or if we should close it. |
Sorry, something went wrong.
colesbury
left a comment
There was a problem hiding this comment.
Makes sense. Thanks for the example
Sorry, something went wrong.
17eb035
into
python:main
Mar 12, 2026
|
Thanks @bkap123 for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14. |
Sorry, something went wrong.
|
Sorry, @bkap123 and @encukou, I could not cleanly backport this to |
Sorry, something went wrong.
|
Thank you! |
Sorry, something went wrong.
…`PyDict_Next` (pythonGH-145487) (cherry picked from commit 17eb035) Co-authored-by: bkap123 <97006829+bkap123@users.noreply.github.com>
Added a critical section whenever
PyDict_Nextis called inModule/_functoolsmodule.cin the free-threaded build.Additionally, I had to manually remove the lock created by a critical section in case of an early return due to an error.
PyDict_Nextcalls in_functoolsmodule.c#145446