Skip to content

Commit 8a51767

Browse files
authored
gh-117657: Use critical section to make _socket.socket.close thread safe (GH-120490)
1 parent c7991cc commit 8a51767

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

Modules/clinic/socketmodule.c.h

+28-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/socketmodule.c

+13-8
Original file line numberDiff line numberDiff line change
@@ -3331,8 +3331,19 @@ sockets the address is a tuple (ifname, proto [,pkttype [,hatype [,addr]]])");
33313331
Set the file descriptor to -1 so operations tried subsequently
33323332
will surely fail. */
33333333

3334+
/*[clinic input]
3335+
@critical_section
3336+
_socket.socket.close
3337+
self as s: self(type="PySocketSockObject *")
3338+
3339+
close()
3340+
3341+
Close the socket. It cannot be used after this call.
3342+
[clinic start generated code]*/
3343+
33343344
static PyObject *
3335-
sock_close(PySocketSockObject *s, PyObject *Py_UNUSED(ignored))
3345+
_socket_socket_close_impl(PySocketSockObject *s)
3346+
/*[clinic end generated code: output=038b2418e07f6f6c input=9839a261e05bcb97]*/
33363347
{
33373348
SOCKET_T fd;
33383349
int res;
@@ -3357,11 +3368,6 @@ sock_close(PySocketSockObject *s, PyObject *Py_UNUSED(ignored))
33573368
Py_RETURN_NONE;
33583369
}
33593370

3360-
PyDoc_STRVAR(sock_close_doc,
3361-
"close()\n\
3362-
\n\
3363-
Close the socket. It cannot be used after this call.");
3364-
33653371
static PyObject *
33663372
sock_detach(PySocketSockObject *s, PyObject *Py_UNUSED(ignored))
33673373
{
@@ -5118,8 +5124,7 @@ static PyMethodDef sock_methods[] = {
51185124
{"bind", (PyCFunction)sock_bind, METH_O,
51195125
bind_doc},
51205126
#endif
5121-
{"close", (PyCFunction)sock_close, METH_NOARGS,
5122-
sock_close_doc},
5127+
_SOCKET_SOCKET_CLOSE_METHODDEF
51235128
#ifdef HAVE_CONNECT
51245129
{"connect", (PyCFunction)sock_connect, METH_O,
51255130
connect_doc},

Tools/tsan/suppressions_free_threading.txt

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ race_top:PyInterpreterState_ThreadHead
5454
race_top:_PyObject_TryGetInstanceAttribute
5555
race_top:PyThreadState_Next
5656
race_top:PyUnstable_InterpreterFrame_GetLine
57-
race_top:sock_close
5857
race_top:tstate_delete_common
5958
race_top:tstate_is_freed
6059
race_top:type_modified_unlocked

0 commit comments

Comments
 (0)