Make the pwd module functions getpwuid(), getpwnam(), and getpwall() thread-safe. These changes apply to scenarios where the GIL is disabled or in subinterpreter use cases.
--- /dev/null
+import unittest
+
+from test.support import threading_helper
+from test.support.threading_helper import run_concurrently
+
+from test import test_pwd
+
+
+NTHREADS = 10
+
+
+@threading_helper.requires_working_threading()
+class TestPwd(unittest.TestCase):
+ def setUp(self):
+ self.test_pwd = test_pwd.PwdTest()
+
+ def test_racing_test_values(self):
+ # test_pwd.test_values() calls pwd.getpwall() and checks the entries
+ run_concurrently(
+ worker_func=self.test_pwd.test_values, nthreads=NTHREADS
+ )
+
+ def test_racing_test_values_extended(self):
+ # test_pwd.test_values_extended() calls pwd.getpwall(), pwd.getpwnam(),
+ # pwd.getpwduid() and checks the entries
+ run_concurrently(
+ worker_func=self.test_pwd.test_values_extended,
+ nthreads=NTHREADS,
+ )
+
+
+if __name__ == "__main__":
+ unittest.main()
--- /dev/null
+Make functions in :mod:`pwd` thread-safe on the :term:`free threaded <free threading>` build.
preserve
[clinic start generated code]*/
+#include "pycore_modsupport.h" // _PyArg_BadArgument()
+
PyDoc_STRVAR(pwd_getpwuid__doc__,
"getpwuid($module, uidobj, /)\n"
"--\n"
PyObject *name;
if (!PyUnicode_Check(arg)) {
- PyErr_Format(PyExc_TypeError, "getpwnam() argument must be str, not %T", arg);
+ _PyArg_BadArgument("getpwnam", "argument", "str", arg);
goto exit;
}
name = arg;
#ifndef PWD_GETPWALL_METHODDEF
#define PWD_GETPWALL_METHODDEF
#endif /* !defined(PWD_GETPWALL_METHODDEF) */
-/*[clinic end generated code: output=dac88d500f6d6f49 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=5a8fb12939ff4ea3 input=a9049054013a1b77]*/
/* UNIX password file access module */
-// Need limited C API version 3.13 for PyMem_RawRealloc()
-#include "pyconfig.h" // Py_GIL_DISABLED
-#ifndef Py_GIL_DISABLED
-# define Py_LIMITED_API 0x030d0000
-#endif
-
#include "Python.h"
#include "posixmodule.h"
static struct PyModuleDef pwdmodule;
+/* Mutex to protect calls to getpwuid(), getpwnam(), and getpwent().
+ * These functions return pointer to static data structure, which
+ * may be overwritten by any subsequent calls. */
+static PyMutex pwd_db_mutex = {0};
+
#define DEFAULT_BUFFER_SIZE 1024
static PyObject *
Py_END_ALLOW_THREADS
#else
+ PyMutex_Lock(&pwd_db_mutex);
+ // The getpwuid() function is not required to be thread-safe.
+ // https://pubs.opengroup.org/onlinepubs/009604499/functions/getpwuid.html
p = getpwuid(uid);
#endif
if (p == NULL) {
+#ifndef HAVE_GETPWUID_R
+ PyMutex_Unlock(&pwd_db_mutex);
+#endif
PyMem_RawFree(buf);
if (nomem == 1) {
return PyErr_NoMemory();
retval = mkpwent(module, p);
#ifdef HAVE_GETPWUID_R
PyMem_RawFree(buf);
+#else
+ PyMutex_Unlock(&pwd_db_mutex);
#endif
return retval;
}
Py_END_ALLOW_THREADS
#else
+ PyMutex_Lock(&pwd_db_mutex);
+ // The getpwnam() function is not required to be thread-safe.
+ // https://pubs.opengroup.org/onlinepubs/009604599/functions/getpwnam.html
p = getpwnam(name_chars);
#endif
if (p == NULL) {
+#ifndef HAVE_GETPWNAM_R
+ PyMutex_Unlock(&pwd_db_mutex);
+#endif
if (nomem == 1) {
PyErr_NoMemory();
}
goto out;
}
retval = mkpwent(module, p);
+#ifndef HAVE_GETPWNAM_R
+ PyMutex_Unlock(&pwd_db_mutex);
+#endif
out:
PyMem_RawFree(buf);
Py_DECREF(bytes);
if ((d = PyList_New(0)) == NULL)
return NULL;
-#ifdef Py_GIL_DISABLED
- static PyMutex getpwall_mutex = {0};
- PyMutex_Lock(&getpwall_mutex);
-#endif
+ PyMutex_Lock(&pwd_db_mutex);
int failure = 0;
PyObject *v = NULL;
+ // The setpwent(), getpwent() and endpwent() functions are not required to
+ // be thread-safe.
+ // https://pubs.opengroup.org/onlinepubs/009696799/functions/setpwent.html
setpwent();
while ((p = getpwent()) != NULL) {
v = mkpwent(module, p);
done:
endpwent();
-#ifdef Py_GIL_DISABLED
- PyMutex_Unlock(&getpwall_mutex);
-#endif
+ PyMutex_Unlock(&pwd_db_mutex);
if (failure) {
Py_XDECREF(v);
Py_CLEAR(d);
Modules/faulthandler.c faulthandler_dump_traceback reentrant -
Modules/faulthandler.c faulthandler_dump_c_stack reentrant -
Modules/grpmodule.c - group_db_mutex -
+Modules/pwdmodule.c - pwd_db_mutex -
Python/pylifecycle.c _Py_FatalErrorFormat reentrant -
Python/pylifecycle.c fatal_error reentrant -