gh-145446: Add critical section in functools module for `PyDict_Next` by brijkapadia · Pull Request #145487 · python/cpython
@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 kw entry of the partial object. As such, a better solution might be to transition this kw dict to a frozendict (see issue #145478).
Let me know if you think it is worth continuing this PR or if we should close it.