Skip to content

Commit

Permalink
gh-128277: remove unnecessary critical section from socket.close (#…
Browse files Browse the repository at this point in the history
…128305)

Remove unnecessary critical section from `socket.close` as it now uses relaxed atomics for `sock_fd`.
  • Loading branch information
kumaraditya303 authored Jan 1, 2025
1 parent d903b17 commit bb9d955
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
20 changes: 20 additions & 0 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 2 additions & 9 deletions Modules/clinic/socketmodule.c.h

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

3 changes: 1 addition & 2 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *")
Expand All @@ -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;
Expand Down

0 comments on commit bb9d955

Please sign in to comment.