Skip to content

gh-117657: Using critical section to make _socket.socket.close thread safe #120490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -3328,8 +3328,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 @@ -3354,11 +3365,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 @@ -5115,8 +5121,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 @@ -57,7 +57,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
Loading