From 36a4c392785ba6670e6e58541631cf76279bfecf Mon Sep 17 00:00:00 2001 From: Thomas Kowalski Date: Tue, 26 May 2026 08:59:18 +0200 Subject: [PATCH 1/2] refactor: use PyMutex in socket module --- Modules/socketmodule.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 566989d648d2f7..129185be3839e7 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -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; #endif @@ -6217,7 +6217,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 @@ -6233,7 +6233,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); @@ -6324,7 +6324,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 @@ -6334,7 +6334,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); @@ -9290,11 +9290,6 @@ socket_exec(PyObject *m) #endif #endif /* _MSTCPIP_ */ - /* Initialize gethostbyname lock */ -#if defined(USE_GETHOSTBYNAME_LOCK) - netdb_lock = PyThread_allocate_lock(); -#endif - #ifdef MS_WINDOWS /* remove some flags on older version Windows during run-time */ if (remove_unusable_flags(m) < 0) { From 66fd90a1973be01cec464d66fba9c78ea979a63f Mon Sep 17 00:00:00 2001 From: Thomas Kowalski Date: Tue, 26 May 2026 09:01:35 +0200 Subject: [PATCH 2/2] misc: add news entry --- .../next/Library/2026-05-26-08-57-31.gh-issue-150406.jF3g63.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-05-26-08-57-31.gh-issue-150406.jF3g63.rst diff --git a/Misc/NEWS.d/next/Library/2026-05-26-08-57-31.gh-issue-150406.jF3g63.rst b/Misc/NEWS.d/next/Library/2026-05-26-08-57-31.gh-issue-150406.jF3g63.rst new file mode 100644 index 00000000000000..c902a65cd48b04 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-05-26-08-57-31.gh-issue-150406.jF3g63.rst @@ -0,0 +1 @@ +Migrate Lock usage in the :mod:`socket` module to :c:type:`PyMutex`.