Skip to content

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented Nov 20, 2024

grpmodule.c is no longer built with the limited C API, since PyMutex is excluded from the limited C API.

grpmodule.c is no longer built with the limited C API, since PyMutex
is excluded from the limited C API.
@vstinner
Copy link
Member Author

@ZeroIntensity @colesbury: Would you mind to review my change?

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

Hmm, this is an interesting way to do it. Local static variables are notorious for being non-thread-safe, so I'm not sure this kind of approach will work. If it does, I don't think we're fixing any issues with re-entrancy.

I would just go with @critical_section on each function, and then switch the module's Py_mod_multiple_interpreters to Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED instead of Py_MOD_PER_INTERPRETER_GIL_SUPPORTED to denote shared-GIL for subinterpreters. That should fix races for both cases.

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.

This approach looks good to me. I think you are right that we don't have to worry about deadlock here because the calls inside the lock won't call arbitrary Python code.

There's a failing CI related to generated files. I think the pycore_lock.h include in pycore_modsupport.h can be removed now. Also, maybe the arg clinic files need to be regenerated?

Local static variables are notorious for being non-thread-safe

A static PyMutex is fine because PyMutex is thread-safe. (It doesn't matter if it's a local or global static variable.)

The general problems with static variables (including static local variables) is that the data structures are not thread-safe or that they are lazily initialized pointers, which is not the case here.

@ZeroIntensity
Copy link
Member

Thanks for the insight, Sam. How scalable is this approach (as in, will we have a static PyMutex everywhere)?

@colesbury
Copy link
Contributor

I don't think we'll have static PyMutex everywhere, but this is a good pattern for certain libc functions that are not thread-safe and do not have thread-safe alternatives.

@vstinner
Copy link
Member Author

I think the pycore_lock.h include in pycore_modsupport.h can be removed now.

Correct: I wrote #127093 to remove it.

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

LGTM

@vstinner vstinner merged commit 3c2bd66 into python:main Nov 21, 2024
39 checks passed
@vstinner vstinner deleted the getgrall_mutex branch November 21, 2024 14:47
@vstinner
Copy link
Member Author

Merged, thanks for reviews @colesbury and @ZeroIntensity.

@bedevere-app
Copy link

bedevere-app bot commented Nov 21, 2024

GH-127104 is a backport of this pull request to the 3.13 branch.

vstinner added a commit to vstinner/cpython that referenced this pull request Nov 21, 2024
…#127055)

grpmodule.c is no longer built with the limited C API, since PyMutex
is excluded from the limited C API.

(cherry picked from commit 3c2bd66)
vstinner added a commit that referenced this pull request Nov 26, 2024
…) (#127104)

* gh-126316: Make grp.getgrall() thread-safe: add a mutex (#127055)

grpmodule.c is no longer built with the limited C API, since PyMutex
is excluded from the limited C API.

(cherry picked from commit 3c2bd66)

* Revert ABI changes

Don't use Argument Clinic for grp.getgrgid() to avoid changing the
ABI (change PyInterpreterState structure by adding an "id"
identifier).
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
…#127055)

grpmodule.c is no longer built with the limited C API, since PyMutex
is excluded from the limited C API.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants