Skip to content
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

gh-128277: remove unnecessary critical section from socket.close #128305

Merged
merged 5 commits into from
Jan 1, 2025
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
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
Loading