8000 gh-128277: remove unnecessary critical section from `socket.close` (… · srinivasreddy/cpython@bc07e75 · GitHub
[go: up one dir, main page]

Skip to content

Commit bc07e75

Browse files
kumaraditya303srinivasreddy
authored andcommitted
pythongh-128277: remove unnecessary critical section from socket.close (python#128305)
Remove unnecessary critical section from `socket.close` as it now uses relaxed atomics for `sock_fd`.
1 parent e2886de commit bc07e75

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

Lib/test/test_socket.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7075,6 +7075,26 @@ def close_fds(fds):
70757075
self.assertEqual(data, str(index).encode())
70767076

70777077

7078+
class FreeThreadingTests(unittest.TestCase):
7079+
7080+
def test_close_detach_race(self):
7081+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7082+
7083+
def close():
7084+
for _ in range(1000):
7085+
s.close()
7086+
7087+
def detach():
7088+
for _ in range(1000):
7089+
s.detach()
7090+
7091+
t1 = threading.Thread(target=close)
7092+
t2 = threading.Thread(target=detach)
7093+
7094+
with threading_helper.start_threads([t1, t2]):
7095+
pass
7096+
7097+
70787098
def setUpModule():
70797099
thread_info = threading_helper.threading_setup()
70807100
unittest.addModuleCleanup(threading_helper.threading_cleanup, *thread_info)

Modules/clinic/socketmodule.c.h

Lines changed: 2 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/socketmodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3386,7 +3386,6 @@ sockets the address is a tuple (ifname, proto [,pkttype [,hatype [,addr]]])");
33863386
will surely fail. */
33873387

33883388
/*[clinic input]
3389-
@critical_section
33903389
_socket.socket.close
33913390
self as s: self(type="PySocketSockObject *")
33923391
@@ -3397,7 +3396,7 @@ Close the socket. It cannot be used after this call.
33973396

33983397
static PyObject *
33993398
_socket_socket_close_impl(PySocketSockObject *s)
3400-
/*[clinic end generated code: output=038b2418e07f6f6c input=9839a261e05bcb97]*/
3399+
/*[clinic end generated code: output=038b2418e07f6f6c input=dc487e470e55a83c]*/
34013400
{
34023401
SOCKET_T fd;
34033402
int res;

0 commit comments

Comments
 (0)
0