gh-145446: Add critical section in functools module for PyDict_Next#145487
gh-145446: Add critical section in functools module for PyDict_Next#145487bkap123 wants to merge 5 commits intopython:mainfrom
PyDict_Next#145487Conversation
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.
|
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 |
|
I have made the requested changes; please review again |
|
Thanks for making the requested changes! @vstinner, @colesbury: please review the changes made to this pull request. |
|
I don't think the remaining critical sections are needed either. |
|
Okay, after thinking about it I see what you mean. I can close the PR if the critical sections are not needed. |
|
@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. |
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