From 8cf4b5900536068637f879aa7bd2bab1e54329e8 Mon Sep 17 00:00:00 2001 From: Marcin Bachry Date: Wed, 11 Dec 2024 19:58:46 +0100 Subject: [PATCH 1/4] socket: pass flags and address from send_fds --- Lib/socket.py | 2 +- Lib/test/test_socket.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Lib/socket.py b/Lib/socket.py index be37c24d6174a2..3441d0bbd309ad 100644 --- a/Lib/socket.py +++ b/Lib/socket.py @@ -563,7 +563,7 @@ def send_fds(sock, buffers, fds, flags=0, address=None): import array return sock.sendmsg(buffers, [(_socket.SOL_SOCKET, - _socket.SCM_RIGHTS, array.array("i", fds))]) + _socket.SCM_RIGHTS, array.array("i", fds))], flags, address) __all__.append("send_fds") if hasattr(_socket.socket, "recvmsg"): diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 307d6e886c617f..e69d646fb38291 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -7071,6 +7071,45 @@ def close_fds(fds): data = os.read(rfd, 100) self.assertEqual(data, str(index).encode()) + def testSendAndRecvFdsByAddress(self): + def close_pipes(pipes): + for fd1, fd2 in pipes: + os.close(fd1) + os.close(fd2) + + def close_fds(fds): + for fd in fds: + os.close(fd) + + # send 10 file descriptors + pipes = [os.pipe() for _ in range(10)] + self.addCleanup(close_pipes, pipes) + fds = [rfd for rfd, wfd in pipes] + + sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) + address = socket_helper.create_unix_domain_name() + self.addCleanup(os_helper.unlink, address) + socket_helper.bind_unix_socket(sock, address) + + socket.send_fds(sock, [MSG], fds, 0, address) + + # request more data and file descriptors than expected + msg, fds2, flags, addr = socket.recv_fds(sock, len(MSG) * 2, len(fds) * 2) + self.addCleanup(close_fds, fds2) + + self.assertEqual(msg, MSG) + self.assertEqual(len(fds2), len(fds)) + self.assertEqual(flags, 0) + + # test that file descriptors are connected + for index, fds in enumerate(pipes): + rfd, wfd = fds + os.write(wfd, str(index).encode()) + + for index, rfd in enumerate(fds2): + data = os.read(rfd, 100) + self.assertEqual(data, str(index).encode()) + def setUpModule(): thread_info = threading_helper.threading_setup() From 9cb500959c6bf243d28bd5b27f6a94069629f664 Mon Sep 17 00:00:00 2001 From: Marcin Bachry Date: Wed, 11 Dec 2024 20:15:23 +0100 Subject: [PATCH 2/4] Add NEWS entry --- .../next/Library/2024-12-11-20-15-00.gh-issue-127840.pt8fiQ.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-12-11-20-15-00.gh-issue-127840.pt8fiQ.rst diff --git a/Misc/NEWS.d/next/Library/2024-12-11-20-15-00.gh-issue-127840.pt8fiQ.rst b/Misc/NEWS.d/next/Library/2024-12-11-20-15-00.gh-issue-127840.pt8fiQ.rst new file mode 100644 index 00000000000000..6bc55181cdd113 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-12-11-20-15-00.gh-issue-127840.pt8fiQ.rst @@ -0,0 +1 @@ +Fix ``socket.send_fds`` ignoring flags and address parameters From 01f92d31c8a262fb1513d807b28c4a2efcf6a192 Mon Sep 17 00:00:00 2001 From: Marcin Bachry Date: Thu, 12 Dec 2024 19:03:28 +0100 Subject: [PATCH 3/4] Update Misc/NEWS.d/next/Library/2024-12-11-20-15-00.gh-issue-127840.pt8fiQ.rst Co-authored-by: Peter Bierma --- .../next/Library/2024-12-11-20-15-00.gh-issue-127840.pt8fiQ.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-12-11-20-15-00.gh-issue-127840.pt8fiQ.rst b/Misc/NEWS.d/next/Library/2024-12-11-20-15-00.gh-issue-127840.pt8fiQ.rst index 6bc55181cdd113..51eaaa96ebaa07 100644 --- a/Misc/NEWS.d/next/Library/2024-12-11-20-15-00.gh-issue-127840.pt8fiQ.rst +++ b/Misc/NEWS.d/next/Library/2024-12-11-20-15-00.gh-issue-127840.pt8fiQ.rst @@ -1 +1 @@ -Fix ``socket.send_fds`` ignoring flags and address parameters +Fix :func:`socket.send_fds` ignoring flags and address parameters. From 8f7c0582a79cfaa1aaaeceaf644a8bfe4cf73cba Mon Sep 17 00:00:00 2001 From: Marcin Bachry Date: Thu, 12 Dec 2024 18:29:13 +0100 Subject: [PATCH 4/4] Simplify test --- Lib/test/test_socket.py | 37 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index e69d646fb38291..bc0be351096ad8 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -7072,43 +7072,28 @@ def close_fds(fds): self.assertEqual(data, str(index).encode()) def testSendAndRecvFdsByAddress(self): - def close_pipes(pipes): - for fd1, fd2 in pipes: - os.close(fd1) - os.close(fd2) - - def close_fds(fds): - for fd in fds: - os.close(fd) - - # send 10 file descriptors - pipes = [os.pipe() for _ in range(10)] - self.addCleanup(close_pipes, pipes) - fds = [rfd for rfd, wfd in pipes] + rfd, wfd = os.pipe() + self.addCleanup(os.close, rfd) + self.addCleanup(os.close, wfd) sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) address = socket_helper.create_unix_domain_name() self.addCleanup(os_helper.unlink, address) socket_helper.bind_unix_socket(sock, address) - socket.send_fds(sock, [MSG], fds, 0, address) + socket.send_fds(sock, [MSG], [rfd], 0, address) # request more data and file descriptors than expected - msg, fds2, flags, addr = socket.recv_fds(sock, len(MSG) * 2, len(fds) * 2) - self.addCleanup(close_fds, fds2) - + msg, (rfd2,), flags, addr = socket.recv_fds(sock, len(MSG) * 2, 2) + self.addCleanup(os.close, rfd2) self.assertEqual(msg, MSG) - self.assertEqual(len(fds2), len(fds)) self.assertEqual(flags, 0) + self.assertEqual(addr, address) - # test that file descriptors are connected - for index, fds in enumerate(pipes): - rfd, wfd = fds - os.write(wfd, str(index).encode()) - - for index, rfd in enumerate(fds2): - data = os.read(rfd, 100) - self.assertEqual(data, str(index).encode()) + # test that the file descriptor is connected + os.write(wfd, b'data') + data = os.read(rfd2, 100) + self.assertEqual(data, b'data') def setUpModule():