]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-150452: use PyMutex in socket module (#150453)
authorThomas Kowalski <thom.kowa@gmail.com>
Wed, 15 Jul 2026 10:39:28 +0000 (12:39 +0200)
committerGitHub <noreply@github.com>
Wed, 15 Jul 2026 10:39:28 +0000 (10:39 +0000)
Modules/socketmodule.c

index ada4fda6571049d4e62b81ac40bedb7fccef3ceb..084c2dbcff066e5ee377d1d3edafa5b9f308a933 100644 (file)
@@ -1170,7 +1170,7 @@ new_sockobject(socket_state *state, SOCKET_T fd, int family, int type,
 /* Lock to allow python interpreter to continue, but only allow one
    thread to be in gethostbyname or getaddrinfo */
 #if defined(USE_GETHOSTBYNAME_LOCK)
-static PyThread_type_lock netdb_lock;
+static PyMutex netdb_lock = {0};
 #endif
 
 
@@ -6219,7 +6219,7 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
 #endif
 #else /* not HAVE_GETHOSTBYNAME_R */
 #ifdef USE_GETHOSTBYNAME_LOCK
-    PyThread_acquire_lock(netdb_lock, 1);
+    PyMutex_Lock(&netdb_lock);
 #endif
     _Py_COMP_DIAG_PUSH
     _Py_COMP_DIAG_IGNORE_DEPR_DECLS
@@ -6235,7 +6235,7 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
     ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr),
                          sa->sa_family);
 #ifdef USE_GETHOSTBYNAME_LOCK
-    PyThread_release_lock(netdb_lock);
+    PyMutex_Unlock(&netdb_lock);
 #endif
 finally:
     PyMem_Free(name);
@@ -6326,7 +6326,7 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
 #endif
 #else /* not HAVE_GETHOSTBYNAME_R */
 #ifdef USE_GETHOSTBYNAME_LOCK
-    PyThread_acquire_lock(netdb_lock, 1);
+    PyMutex_Lock(&netdb_lock);
 #endif
     _Py_COMP_DIAG_PUSH
     _Py_COMP_DIAG_IGNORE_DEPR_DECLS
@@ -6336,7 +6336,7 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
     Py_END_ALLOW_THREADS
     ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr), af);
 #ifdef USE_GETHOSTBYNAME_LOCK
-    PyThread_release_lock(netdb_lock);
+    PyMutex_Unlock(&netdb_lock);
 #endif
 finally:
     PyMem_Free(ip_num);
@@ -9292,15 +9292,6 @@ socket_exec(PyObject *m)
 #endif
 #endif /* _MSTCPIP_ */
 
-    /* Initialize gethostbyname lock */
-#if defined(USE_GETHOSTBYNAME_LOCK)
-    netdb_lock = PyThread_allocate_lock();
-    if (netdb_lock == NULL) {
-        PyErr_NoMemory();
-        goto error;
-    }
-#endif
-
 #ifdef MS_WINDOWS
     /* remove some flags on older version Windows during run-time */
     if (remove_unusable_flags(m) < 0) {