Skip to content

gh-127081: lock non-re-entrant *pwent calls #132748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

duaneg
Copy link

@duaneg duaneg commented Apr 20, 2025

The libc setpwent, getpwent, and endpwent functions are not thread-safe. Protect them with mutexs in free-threading builds.

The libc setpwent, getpwent, and endpwent functions are not thread-safe.
Protect them with mutexs in free-threading builds.
@picnixz picnixz changed the title gh-127081: lock non-re-entrant *pwent calls gh-127081: lock non-re-entrant *pwent calls Apr 20, 2025
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.

We need a critical section here. Py_CLEAR and Py_DECREF are re-entrant (as in, they can invoke the eval loop), so a mutex is prone to lock-ordering or re-entrancy deadlocks.

@ZeroIntensity
Copy link
Member

Actually, sorry, we need to fix subinterpreter thread-safety here too. We can't do that with a critical section. Could you adjust the mutex to go over solely the libc calls?

@duaneg
Copy link
Author

duaneg commented Apr 20, 2025

Py_CLEAR and Py_DECREF are re-entrant...Could you adjust the mutex to go over solely the libc calls?

Hmm, yeah, good point. I guess we can just defer the decrefs until exit.

Comment on lines 330 to 334
Py_ssize_t n = PyList_Size(d);
for (Py_ssize_t i = 0; i < n; ++i) {
PyObject *entry = PyList_GetItem(d, i);
Py_DECREF(entry);
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Py_ssize_t n = PyList_Size(d);
for (Py_ssize_t i = 0; i < n; ++i) {
PyObject *entry = PyList_GetItem(d, i);
Py_DECREF(entry);
}
Py_ssize_t n = PyList_GET_SIZE(d);
while (--n >= 0) {
Py_DECREF(PyList_GET_ITEM(d, n));
}

Copy link
Author

Choose a reason for hiding this comment

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

This change broke GIL builds, as they use the limited API only, which does not include those functions. Unfortunately I was using a free-threading build, so didn't notice initially. I'll switch back to using the ones available in the limited API, but keep the better loop construct.

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