Skip to content

Commit

Permalink
pythongh-117657: Use critical section to make _socket.socket.close th…
Browse files Browse the repository at this point in the history
…read safe (pythonGH-120490)
  • Loading branch information
aisk authored and estyxx committed Jul 17, 2024
1 parent cc0fe65 commit 8bbdc8c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
29 changes: 28 additions & 1 deletion Modules/clinic/socketmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 13 additions & 8 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3331,8 +3331,19 @@ sockets the address is a tuple (ifname, proto [,pkttype [,hatype [,addr]]])");
Set the file descriptor to -1 so operations tried subsequently
will surely fail. */

/*[clinic input]
@critical_section
_socket.socket.close
self as s: self(type="PySocketSockObject *")
close()
Close the socket. It cannot be used after this call.
[clinic start generated code]*/

static PyObject *
sock_close(PySocketSockObject *s, PyObject *Py_UNUSED(ignored))
_socket_socket_close_impl(PySocketSockObject *s)
/*[clinic end generated code: output=038b2418e07f6f6c input=9839a261e05bcb97]*/
{
SOCKET_T fd;
int res;
Expand All @@ -3357,11 +3368,6 @@ sock_close(PySocketSockObject *s, PyObject *Py_UNUSED(ignored))
Py_RETURN_NONE;
}

PyDoc_STRVAR(sock_close_doc,
"close()\n\
\n\
Close the socket. It cannot be used after this call.");

static PyObject *
sock_detach(PySocketSockObject *s, PyObject *Py_UNUSED(ignored))
{
Expand Down Expand Up @@ -5118,8 +5124,7 @@ static PyMethodDef sock_methods[] = {
{"bind", (PyCFunction)sock_bind, METH_O,
bind_doc},
#endif
{"close", (PyCFunction)sock_close, METH_NOARGS,
sock_close_doc},
_SOCKET_SOCKET_CLOSE_METHODDEF
#ifdef HAVE_CONNECT
{"connect", (PyCFunction)sock_connect, METH_O,
connect_doc},
Expand Down
1 change: 0 additions & 1 deletion Tools/tsan/suppressions_free_threading.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ race_top:PyInterpreterState_ThreadHead
race_top:_PyObject_TryGetInstanceAttribute
race_top:PyThreadState_Next
race_top:PyUnstable_InterpreterFrame_GetLine
race_top:sock_close
race_top:tstate_delete_common
race_top:tstate_is_freed
race_top:type_modified_unlocked
Expand Down

0 comments on commit 8bbdc8c

Please sign in to comment.