Skip to content

Commit 4f3be1b

Browse files
gh-150452: use PyMutex in socket module (#150453)
1 parent 1c44402 commit 4f3be1b

1 file changed

Lines changed: 5 additions & 14 deletions

File tree

Modules/socketmodule.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ new_sockobject(socket_state *state, SOCKET_T fd, int family, int type,
11701170
/* Lock to allow python interpreter to continue, but only allow one
11711171
thread to be in gethostbyname or getaddrinfo */
11721172
#if defined(USE_GETHOSTBYNAME_LOCK)
1173-
static PyThread_type_lock netdb_lock;
1173+
static PyMutex netdb_lock = {0};
11741174
#endif
11751175

11761176

@@ -6219,7 +6219,7 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
62196219
#endif
62206220
#else /* not HAVE_GETHOSTBYNAME_R */
62216221
#ifdef USE_GETHOSTBYNAME_LOCK
6222-
PyThread_acquire_lock(netdb_lock, 1);
6222+
PyMutex_Lock(&netdb_lock);
62236223
#endif
62246224
_Py_COMP_DIAG_PUSH
62256225
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
@@ -6235,7 +6235,7 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
62356235
ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr),
62366236
sa->sa_family);
62376237
#ifdef USE_GETHOSTBYNAME_LOCK
6238-
PyThread_release_lock(netdb_lock);
6238+
PyMutex_Unlock(&netdb_lock);
62396239
#endif
62406240
finally:
62416241
PyMem_Free(name);
@@ -6326,7 +6326,7 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
63266326
#endif
63276327
#else /* not HAVE_GETHOSTBYNAME_R */
63286328
#ifdef USE_GETHOSTBYNAME_LOCK
6329-
PyThread_acquire_lock(netdb_lock, 1);
6329+
PyMutex_Lock(&netdb_lock);
63306330
#endif
63316331
_Py_COMP_DIAG_PUSH
63326332
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
@@ -6336,7 +6336,7 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
63366336
Py_END_ALLOW_THREADS
63376337
ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr), af);
63386338
#ifdef USE_GETHOSTBYNAME_LOCK
6339-
PyThread_release_lock(netdb_lock);
6339+
PyMutex_Unlock(&netdb_lock);
63406340
#endif
63416341
finally:
63426342
PyMem_Free(ip_num);
@@ -9292,15 +9292,6 @@ socket_exec(PyObject *m)
92929292
#endif
92939293
#endif /* _MSTCPIP_ */
92949294

9295-
/* Initialize gethostbyname lock */
9296-
#if defined(USE_GETHOSTBYNAME_LOCK)
9297-
netdb_lock = PyThread_allocate_lock();
9298-
if (netdb_lock == NULL) {
9299-
PyErr_NoMemory();
9300-
goto error;
9301-
}
9302-
#endif
9303-
93049295
#ifdef MS_WINDOWS
93059296
/* remove some flags on older version Windows during run-time */
93069297
if (remove_unusable_flags(m) < 0) {

0 commit comments

Comments
 (0)