From ab1b4919cbe2c5b1e8512b27629eecdc4ea410c5 Mon Sep 17 00:00:00 2001 From: Thomas Kowalski Date: Tue, 26 May 2026 08:59:18 +0200 Subject: [PATCH 1/4] refactor: use PyMutex in socket module --- Modules/socketmodule.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index ada4fda6571049d..39b221bd43fcde7 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 @@ -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) { From 5ca94e3495f3151ceeb6bbb408e281489de4a978 Mon Sep 17 00:00:00 2001 From: Thomas Kowalski Date: Tue, 26 May 2026 09:01:35 +0200 Subject: [PATCH 2/4] 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 000000000000000..c902a65cd48b043 --- /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`. From 30fb27f3337981a597e7a2fcf1a65b2f50202a8e Mon Sep 17 00:00:00 2001 From: Thomas Kowalski Date: Fri, 29 May 2026 17:48:34 +0200 Subject: [PATCH 3/4] review: update Modules/socketmodule.c Co-authored-by: Victor Stinner --- Modules/socketmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 39b221bd43fcde7..084c2dbcff066e5 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 PyMutex netdb_lock; +static PyMutex netdb_lock = {0}; #endif From 83dd4649dbff9f1f308ff22d1756943277e4dd12 Mon Sep 17 00:00:00 2001 From: Thomas Kowalski Date: Fri, 29 May 2026 17:47:57 +0200 Subject: [PATCH 4/4] misc: remove news entry --- .../next/Library/2026-05-26-08-57-31.gh-issue-150406.jF3g63.rst | 1 - 1 file changed, 1 deletion(-) delete 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 deleted file mode 100644 index c902a65cd48b043..000000000000000 --- a/Misc/NEWS.d/next/Library/2026-05-26-08-57-31.gh-issue-150406.jF3g63.rst +++ /dev/null @@ -1 +0,0 @@ -Migrate Lock usage in the :mod:`socket` module to :c:type:`PyMutex`.