https://github.com/python/cpython/commit/458e33018a2f4f4b3d9a2c8f6e70dcce31f34005
commit: 458e33018a2f4f4b3d9a2c8f6e70dcce31f34005
branch: main
author: Duane Griffin <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2025-05-22T15:52:02+05:30
summary:

gh-127081: lock non-re-entrant `*pwent` calls in free-threading (#132748)

files:
A Misc/NEWS.d/next/Library/2025-04-21-00-58-04.gh-issue-127081.3DCl92.rst
M Modules/pwdmodule.c

diff --git 
a/Misc/NEWS.d/next/Library/2025-04-21-00-58-04.gh-issue-127081.3DCl92.rst 
b/Misc/NEWS.d/next/Library/2025-04-21-00-58-04.gh-issue-127081.3DCl92.rst
new file mode 100644
index 00000000000000..a99669a1bc021a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-04-21-00-58-04.gh-issue-127081.3DCl92.rst
@@ -0,0 +1,2 @@
+Fix libc thread safety issues with :mod:`pwd` by locking access to
+``getpwall``.
diff --git a/Modules/pwdmodule.c b/Modules/pwdmodule.c
index 2240e2078b2d98..c5a8cead19a773 100644
--- a/Modules/pwdmodule.c
+++ b/Modules/pwdmodule.c
@@ -301,18 +301,33 @@ pwd_getpwall_impl(PyObject *module)
     struct passwd *p;
     if ((d = PyList_New(0)) == NULL)
         return NULL;
+
+#ifdef Py_GIL_DISABLED
+    static PyMutex getpwall_mutex = {0};
+    PyMutex_Lock(&getpwall_mutex);
+#endif
+    int failure = 0;
+    PyObject *v = NULL;
     setpwent();
     while ((p = getpwent()) != NULL) {
-        PyObject *v = mkpwent(module, p);
+        v = mkpwent(module, p);
         if (v == NULL || PyList_Append(d, v) != 0) {
-            Py_XDECREF(v);
-            Py_DECREF(d);
-            endpwent();
-            return NULL;
+            /* NOTE: cannot dec-ref here, while holding the mutex. */
+            failure = 1;
+            goto done;
         }
         Py_DECREF(v);
     }
+
+done:
     endpwent();
+#ifdef Py_GIL_DISABLED
+    PyMutex_Unlock(&getpwall_mutex);
+#endif
+    if (failure) {
+        Py_XDECREF(v);
+        Py_CLEAR(d);
+    }
     return d;
 }
 #endif

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to