Skip to content
Open
Show file tree
Hide file tree
Changes from 12 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
5 changes: 3 additions & 2 deletions Lib/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,8 @@ 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"):
Expand All @@ -579,7 +580,7 @@ def recv_fds(sock, bufsize, maxfds, flags=0):
# Array of ints
fds = array.array("i")
msg, ancdata, flags, addr = sock.recvmsg(bufsize,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename the result flags as msg_flags to be consistent with the signature (don't forget to update return ...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

_socket.CMSG_LEN(maxfds * fds.itemsize))
_socket.CMSG_LEN(maxfds * fds.itemsize), flags)
for cmsg_level, cmsg_type, cmsg_data in ancdata:
if (cmsg_level == _socket.SOL_SOCKET and cmsg_type == _socket.SCM_RIGHTS):
fds.frombytes(cmsg_data[:
Expand Down
127 changes: 112 additions & 15 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -3516,7 +3516,7 @@ def _testSendmsgTimeout(self):
# Linux supports MSG_DONTWAIT when sending, but in general, it
# only works when receiving. Could add other platforms if they
# support it too.
@skipWithClientIf(sys.platform not in {"linux", "android"},
@skipWithClientIf(sys.platform not in {"linux", "android", "freebsd"},
"MSG_DONTWAIT not known to work on this platform when "
"sending")
def testSendmsgDontWait(self):
Expand Down Expand Up @@ -7327,19 +7327,22 @@ def test_dual_stack_client_v6(self):
@requireAttrs(socket, "recv_fds")
@requireAttrs(socket, "AF_UNIX")
class SendRecvFdsTests(unittest.TestCase):
def testSendAndRecvFds(self):
def close_pipes(pipes):
for fd1, fd2 in pipes:
os.close(fd1)
os.close(fd2)

def _cleanup_fds(self, fds):
def close_fds(fds):
for fd in fds:
os.close(fd)
self.addCleanup(close_fds, fds)

def _test_pipe(self, rfd, wfd, msg):
assert len(msg) < 512
os.write(wfd, msg)
data = os.read(rfd, 512)
self.assertEqual(data, msg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert len(msg) < 512
os.write(wfd, msg)
data = os.read(rfd, 512)
self.assertEqual(data, msg)
os.write(wfd, msg)
data = os.read(rfd, len(msg))
self.assertEqual(data, msg)

I think we can do it like this right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

man 7 pipe says:

POSIX.1 says that writes of less than PIPE_BUF bytes must be atomic: the output data is written to the pipe as a contiguous sequence. [...] POSIX.1 requires PIPE_BUF to be at least 512 bytes.

I hardcoded 512 here, which may look confusing. I've updated it to make it more clear.


def testSendAndRecvFds(self):
# send 10 file descriptors
pipes = [os.pipe() for _ in range(10)]
self.addCleanup(close_pipes, pipes)
self._cleanup_fds(fd for pair in pipes for fd in pair)
fds = [rfd for rfd, wfd in pipes]

# use a UNIX socket pair to exchange file descriptors locally
Expand All @@ -7348,21 +7351,115 @@ def close_fds(fds):
socket.send_fds(sock1, [MSG], fds)
# request more data and file descriptors than expected
msg, fds2, flags, addr = socket.recv_fds(sock2, len(MSG) * 2, len(fds) * 2)
self.addCleanup(close_fds, fds2)
self._cleanup_fds(fds2)

self.assertEqual(msg, MSG)
self.assertEqual(len(fds2), len(fds))
self.assertEqual(flags, 0)
# don't test addr

# test that file descriptors are connected
for index, fds in enumerate(pipes):
rfd, wfd = fds
os.write(wfd, str(index).encode())
for index, ((_, wfd), rfd) in enumerate(zip(pipes, fds2)):
self._test_pipe(rfd, wfd, str(index).encode())

def test_send_recv_fds_with_addrs(self):
sock1 = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
sock2 = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
rfd, wfd = os.pipe()
self.addCleanup(os.close, rfd)
self.addCleanup(os.close, wfd)

with tempfile.TemporaryDirectory() as tmpdir, sock1, sock2:
sock1_addr = os.path.join(tmpdir, "sock1")
sock2_addr = os.path.join(tmpdir, "sock2")
sock1.bind(sock1_addr)
sock2.bind(sock2_addr)
sock2.setblocking(False)

socket.send_fds(sock1, [MSG], [rfd], address=sock2_addr)
if sys.platform.startswith("freebsd"):
# FreeBSD requires at least CMSG_LEN(2 * sizeof(int)), otherwise
# the cmsg will be truncated
recv_fds_len = 2
else:
recv_fds_len = 1
msg, fds, flags, addr = socket.recv_fds(sock2, len(MSG), recv_fds_len)
self._cleanup_fds(fds)

self.assertEqual(msg, MSG)
if hasattr(socket, "MSG_CTRUNC"):
self.assertEqual(flags & socket.MSG_CTRUNC, 0)
self.assertEqual(len(fds), 1)
self.assertEqual(addr, sock1_addr)

self._test_pipe(fds[0], wfd, MSG)

for index, rfd in enumerate(fds2):
data = os.read(rfd, 100)
self.assertEqual(data, str(index).encode())
@requireAttrs(socket, "MSG_PEEK")
@unittest.skipUnless(sys.platform in ("linux", "android"), "works on Linux")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm back here, but does it work on freebsd?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately no. On FreeBSD recvmsg with MSG_PEEK returns random bytes in the ancillary data, which looks like some kind of uninitialized memory.

def test_recv_fds_peek(self):
rfd, wfd = os.pipe()
self.addCleanup(os.close, rfd)
self.addCleanup(os.close, wfd)

sock1, sock2 = socket.socketpair(socket.AF_UNIX, socket.SOCK_DGRAM)
with sock1, sock2:
socket.send_fds(sock1, [MSG], [rfd])
sock2.setblocking(False)

# peek message on sock2
peek_len = len(MSG) // 2
msg, fds, flags, addr = socket.recv_fds(sock2, peek_len, 1,
flags=socket.MSG_PEEK)
self._cleanup_fds(fds)

self.assertEqual(len(msg), peek_len)
self.assertEqual(msg, MSG[:peek_len])
self.assertEqual(flags & socket.MSG_TRUNC, socket.MSG_TRUNC)
if hasattr(socket, "MSG_CTRUNC"):
self.assertEqual(flags & socket.MSG_CTRUNC, 0)
self.assertEqual(len(fds), 1)
self._test_pipe(fds[0], wfd, MSG)

# will raise BlockingIOError if MSG_PEEK didn't work
msg, fds, flags, addr = socket.recv_fds(sock2, len(MSG), 1)
self._cleanup_fds(fds)

self.assertEqual(msg, MSG)
if hasattr(socket, "MSG_CTRUNC"):
self.assertEqual(flags & socket.MSG_CTRUNC, 0)
self.assertEqual(len(fds), 1)
self._test_pipe(fds[0], wfd, MSG)

@requireAttrs(socket, "MSG_DONTWAIT")
@unittest.skipIf(sys.platform in ("darwin",),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above, the test for sendmsg only enabled it for @skipWithClientIf(sys.platform not in {"linux", "android", "freebsd"}. I think it's better if we check this here as well because there might be more platforms than {"linux", "android", "freebsd", "darwin"} in the future.

Or: we change the test above with @skipWithClientIf(sys.platform not in ("darwin",) instead and add @requireAttrs(socket, "MSG_DONTWAIT")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to PEP 11, it should be safe to replace @skipWithClientIf(sys.platform not in {"linux", "android", "freebsd"}) with @requireAttrs(socket, "MSG_DONTWAIT") and @skipWithClientIf(sys.platform not in ("darwin",)) on all supported platforms. If this test fails on any other platform, we can update it as needed.

"MSG_DONTWAIT not known to work on this platform when "
"sending")
def test_send_fds_dontwait(self):
rfd, wfd = os.pipe()
self.addCleanup(os.close, rfd)
self.addCleanup(os.close, wfd)

# use SOCK_STREAM instead of SOCK_DGRAM to support *BSD platforms
# ref: https://docs.python.org/3/library/asyncio-protocol.html#datagram-protocols
sock1, sock2 = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM)
with sock1, sock2:
sock1.setblocking(True)
with self.assertRaises(BlockingIOError):
for _ in range(64 * 1024):
socket.send_fds(sock1, [MSG], [rfd], socket.MSG_DONTWAIT)

if sys.platform.startswith("freebsd"):
# FreeBSD requires at least CMSG_LEN(2 * sizeof(int)), otherwise
# the cmsg will be truncated
recv_fds_len = 2
else:
recv_fds_len = 1
msg, fds, flags, addr = socket.recv_fds(sock2, len(MSG), recv_fds_len)
self._cleanup_fds(fds)

self.assertEqual(msg, MSG)
self.assertEqual(len(fds), 1)
self._test_pipe(fds[0], wfd, MSG)


class FreeThreadingTests(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix ``flags`` and ``address`` parameters which were ignored in
:func:`socket.send_fds` and :func:`socket.recv_fds`.
Loading