Skip to content

gh-145446: Add critical section in functools module for PyDict_Next#145487

Open
bkap123 wants to merge 5 commits intopython:mainfrom
bkap123:critical
Open

gh-145446: Add critical section in functools module for PyDict_Next#145487
bkap123 wants to merge 5 commits intopython:mainfrom
bkap123:critical

Conversation

@bkap123
Copy link
Contributor

@bkap123 bkap123 commented Mar 3, 2026

Added a critical section whenever PyDict_Next is called in Module/_functoolsmodule.c in 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.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@colesbury colesbury left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@bedevere-app
Copy link

bedevere-app bot commented Mar 10, 2026

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. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@bkap123
Copy link
Contributor Author

bkap123 commented Mar 10, 2026

I have made the requested changes; please review again

@bedevere-app
Copy link

bedevere-app bot commented Mar 10, 2026

Thanks for making the requested changes!

@vstinner, @colesbury: please review the changes made to this pull request.

@bedevere-app bedevere-app bot requested review from colesbury and vstinner March 10, 2026 22:18
@colesbury
Copy link
Contributor

I don't think the remaining critical sections are needed either.

@bkap123
Copy link
Contributor Author

bkap123 commented Mar 10, 2026

Okay, after thinking about it I see what you mean. I can close the PR if the critical sections are not needed.

@bkap123
Copy link
Contributor Author

bkap123 commented Mar 11, 2026

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants