https://github.com/python/cpython/commit/bb9d955e16c5578bdbc72750fbbffc8313559109
commit: bb9d955e16c5578bdbc72750fbbffc8313559109
branch: main
author: Kumar Aditya <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2025-01-01T18:00:47+05:30
summary:
gh-128277: remove unnecessary critical section from `socket.close` (#128305)
Remove unnecessary critical section from `socket.close` as it now uses relaxed
atomics for `sock_fd`.
files:
M Lib/test/test_socket.py
M Modules/clinic/socketmodule.c.h
M Modules/socketmodule.c
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index aac213e36aecf0..faf326d9164e1b 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -7075,6 +7075,26 @@ def close_fds(fds):
self.assertEqual(data, str(index).encode())
+class FreeThreadingTests(unittest.TestCase):
+
+ def test_close_detach_race(self):
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+
+ def close():
+ for _ in range(1000):
+ s.close()
+
+ def detach():
+ for _ in range(1000):
+ s.detach()
+
+ t1 = threading.Thread(target=close)
+ t2 = threading.Thread(target=detach)
+
+ with threading_helper.start_threads([t1, t2]):
+ pass
+
+
def setUpModule():
thread_info = threading_helper.threading_setup()
unittest.addModuleCleanup(threading_helper.threading_cleanup, *thread_info)
diff --git a/Modules/clinic/socketmodule.c.h b/Modules/clinic/socketmodule.c.h
index db1a28b86c8773..2152f288a9722f 100644
--- a/Modules/clinic/socketmodule.c.h
+++ b/Modules/clinic/socketmodule.c.h
@@ -6,7 +6,6 @@ preserve
# include "pycore_gc.h" // PyGC_Head
# include "pycore_runtime.h" // _Py_ID()
#endif
-#include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
#include "pycore_modsupport.h" // _PyArg_UnpackKeywords()
PyDoc_STRVAR(_socket_socket_close__doc__,
@@ -26,13 +25,7 @@ _socket_socket_close_impl(PySocketSockObject *s);
static PyObject *
_socket_socket_close(PySocketSockObject *s, PyObject *Py_UNUSED(ignored))
{
- PyObject *return_value = NULL;
-
- Py_BEGIN_CRITICAL_SECTION(s);
- return_value = _socket_socket_close_impl(s);
- Py_END_CRITICAL_SECTION();
-
- return return_value;
+ return _socket_socket_close_impl(s);
}
static int
@@ -287,4 +280,4 @@ _socket_socket_if_nametoindex(PySocketSockObject *self,
PyObject *arg)
#ifndef _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF
#define _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF
#endif /* !defined(_SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF) */
-/*[clinic end generated code: output=59c36bb31b05de68 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=3e612e8df1c322dd input=a9049054013a1b77]*/
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index efc8be3ab592ba..e70aa304f2f3a3 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -3386,7 +3386,6 @@ sockets the address is a tuple (ifname, proto [,pkttype
[,hatype [,addr]]])");
will surely fail. */
/*[clinic input]
-@critical_section
_socket.socket.close
self as s: self(type="PySocketSockObject *")
@@ -3397,7 +3396,7 @@ Close the socket. It cannot be used after this call.
static PyObject *
_socket_socket_close_impl(PySocketSockObject *s)
-/*[clinic end generated code: output=038b2418e07f6f6c input=9839a261e05bcb97]*/
+/*[clinic end generated code: output=038b2418e07f6f6c input=dc487e470e55a83c]*/
{
SOCKET_T fd;
int res;
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]