From 1be335d888c113b49cd6aee21bd0e14a8e528658 Mon Sep 17 00:00:00 2001 From: Artsem Lebiadzevich Date: Sun, 25 Oct 2026 22:05:44 +0300 Subject: [PATCH 1/7] gh-160622: Fix socket recv() and recvfrom() with MSG_TRUNC With MSG_TRUNC, recv() and recvfrom() can return the real length of the datagram, which may be larger than the requested size. Since PyBytesWriter_FinishWithSize() now rejects a size larger than the allocated one, this raised ValueError after the datagram had already been consumed. Return only the bytes actually received instead. --- Lib/test/test_socket.py | 20 +++++++++++++++++++ ...-10-25-19-04-57.gh-issue-160622.7n56jv.rst | 4 ++++ Modules/socketmodule.c | 10 ++++++++++ 3 files changed, 34 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-10-25-19-04-57.gh-issue-160622.7n56jv.rst diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index e4b3d848923f8c9..69b30eb100a89b4 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -3286,6 +3286,26 @@ def testRecvFromNegative(self): def _testRecvFromNegative(self): self.cli.sendto(MSG, 0, (HOST, self.port)) + # gh-160622: with MSG_TRUNC, recv() and recvfrom() must not return + # more bytes than requested, even if the datagram was longer. + @requireAttrs(socket, "MSG_TRUNC") + @unittest.skipIf(sys.platform == "win32", "Windows raises WSAEMSGSIZE") + def testRecvTrunc(self): + msg = self.serv.recv(2, socket.MSG_TRUNC) + self.assertEqual(msg, MSG[:2]) + + def _testRecvTrunc(self): + self.cli.sendto(MSG, 0, (HOST, self.port)) + + @requireAttrs(socket, "MSG_TRUNC") + @unittest.skipIf(sys.platform == "win32", "Windows raises WSAEMSGSIZE") + def testRecvFromTrunc(self): + msg, addr = self.serv.recvfrom(2, socket.MSG_TRUNC) + self.assertEqual(msg, MSG[:2]) + + def _testRecvFromTrunc(self): + self.cli.sendto(MSG, 0, (HOST, self.port)) + @unittest.skipUnless(HAVE_SOCKET_UDPLITE, 'UDPLITE sockets required for this test.') diff --git a/Misc/NEWS.d/next/Library/2026-10-25-19-04-57.gh-issue-160622.7n56jv.rst b/Misc/NEWS.d/next/Library/2026-10-25-19-04-57.gh-issue-160622.7n56jv.rst new file mode 100644 index 000000000000000..e1880ba65b2e687 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-25-19-04-57.gh-issue-160622.7n56jv.rst @@ -0,0 +1,4 @@ +:meth:`socket.socket.recv` and :meth:`socket.socket.recvfrom` no longer +raise :exc:`ValueError` and lose the datagram when called with +``MSG_TRUNC`` and the datagram is longer than *bufsize*. Only the bytes +actually received are returned now. diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 97146b1d752c3d8..832736db5fc3cd8 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -4066,6 +4066,12 @@ _socket_socket_recv_impl(PySocketSockObject *s, Py_ssize_t recvlen, PyBytesWriter_Discard(writer); return NULL; } + /* With MSG_TRUNC, recv() can return the real length of the datagram, + which may be larger than the buffer. Only recvlen bytes were + written. */ + if (outlen > recvlen) { + outlen = recvlen; + } return PyBytesWriter_FinishWithSize(writer, outlen); } @@ -4242,6 +4248,10 @@ _socket_socket_recvfrom_impl(PySocketSockObject *s, Py_ssize_t recvlen, PyBytesWriter_Discard(writer); goto error; } + /* See the comment in _socket_socket_recv_impl(). */ + if (outlen > recvlen) { + outlen = recvlen; + } PyObject *buf = PyBytesWriter_FinishWithSize(writer, outlen); if (buf == NULL) { From 1b61de4e4bdba5cf40702d6dd6fec1b31a77d831 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 26 Nov 2026 20:28:36 +0100 Subject: [PATCH 2/7] Test also recv() reading a whole datagram --- Lib/test/test_socket.py | 21 +++++++++++++++++---- Modules/socketmodule.c | 8 ++++++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 364a4734b008f9c..c323122d55899f5 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -51,6 +51,7 @@ HOST = socket_helper.HOST # test unicode string and carriage return MSG = 'Michael Gilfix was here\u1234\r\n'.encode('utf-8') +MSG2 = 'Monty Python test'.encode('utf-8') VSOCKPORT = 1234 AIX = platform.system() == "AIX" @@ -3308,20 +3309,32 @@ def _testRecvFromNegative(self): @requireAttrs(socket, "MSG_TRUNC") @unittest.skipIf(sys.platform == "win32", "Windows raises WSAEMSGSIZE") def testRecvTrunc(self): - msg = self.serv.recv(2, socket.MSG_TRUNC) - self.assertEqual(msg, MSG[:2]) + # Truncated read where recv() returns the real size of the datagram + msg = self.serv.recv(5, socket.MSG_TRUNC) + self.assertEqual(msg, MSG[:5]) + + # Read the whole second datagram + msg = self.serv.recv(len(MSG2) + 5, socket.MSG_TRUNC) + self.assertEqual(msg, MSG2) def _testRecvTrunc(self): self.cli.sendto(MSG, 0, (HOST, self.port)) + self.cli.sendto(MSG2, 0, (HOST, self.port)) @requireAttrs(socket, "MSG_TRUNC") @unittest.skipIf(sys.platform == "win32", "Windows raises WSAEMSGSIZE") def testRecvFromTrunc(self): - msg, addr = self.serv.recvfrom(2, socket.MSG_TRUNC) - self.assertEqual(msg, MSG[:2]) + # Truncated read where recvfrom() returns the real size of the datagram + msg, addr = self.serv.recvfrom(5, socket.MSG_TRUNC) + self.assertEqual(msg, MSG[:5]) + + # Read the whole second datagram + msg, addr = self.serv.recvfrom(len(MSG2) + 5, socket.MSG_TRUNC) + self.assertEqual(msg, MSG2) def _testRecvFromTrunc(self): self.cli.sendto(MSG, 0, (HOST, self.port)) + self.cli.sendto(MSG2, 0, (HOST, self.port)) @unittest.skipUnless(HAVE_SOCKET_UDPLITE, diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 832736db5fc3cd8..34905db0ef46a09 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -4066,7 +4066,8 @@ _socket_socket_recv_impl(PySocketSockObject *s, Py_ssize_t recvlen, PyBytesWriter_Discard(writer); return NULL; } - /* With MSG_TRUNC, recv() can return the real length of the datagram, + + /* With MSG_TRUNC, recv() returns the real size of the datagram, which may be larger than the buffer. Only recvlen bytes were written. */ if (outlen > recvlen) { @@ -4248,7 +4249,10 @@ _socket_socket_recvfrom_impl(PySocketSockObject *s, Py_ssize_t recvlen, PyBytesWriter_Discard(writer); goto error; } - /* See the comment in _socket_socket_recv_impl(). */ + + /* With MSG_TRUNC, recvfrom() returns the real size of the datagram, + which may be larger than the buffer. Only recvlen bytes were + written. */ if (outlen > recvlen) { outlen = recvlen; } From 72b2a99df1cdb97df24c722fe2f55e51f792e07a Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 26 Nov 2026 20:48:55 +0100 Subject: [PATCH 3/7] Document MSG_TRUNC behavior in recv/recvfrom() --- Doc/library/socket.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Doc/library/socket.rst b/Doc/library/socket.rst index 054ccc556740c20..a447782efbd7a7a 100644 --- a/Doc/library/socket.rst +++ b/Doc/library/socket.rst @@ -1786,6 +1786,10 @@ Socket Objects See the Unix manual page :manpage:`recv(2)` for the meaning of the optional argument *flags*; it defaults to zero. + When the :data:`!socket.MSG_TRUNC` flag is used, only receive up to + *bufsize* bytes even if the C function :c:func:`!recv` returns more + bytes (the real datagram size). + .. versionchanged:: 3.5 If the system call is interrupted and the signal handler does not raise an exception, the method now retries the system call instead of raising @@ -1801,6 +1805,10 @@ Socket Objects to zero. The format of *address* depends on the address family --- see :ref:`socket-addresses`. + When the :data:`!socket.MSG_TRUNC` flag is used, only receive up to + *bufsize* bytes even if the C function :c:func:`!recvfrom` returns more + bytes (the real datagram size). + .. versionchanged:: 3.5 If the system call is interrupted and the signal handler does not raise an exception, the method now retries the system call instead of raising From edaf4677adff69703dd208f74d7e94c73bad8f35 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 26 Nov 2026 21:02:23 +0100 Subject: [PATCH 4/7] Adjust test on truncate datagram --- Lib/test/test_socket.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index c323122d55899f5..1b2ae31303a22cc 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -3310,8 +3310,8 @@ def _testRecvFromNegative(self): @unittest.skipIf(sys.platform == "win32", "Windows raises WSAEMSGSIZE") def testRecvTrunc(self): # Truncated read where recv() returns the real size of the datagram - msg = self.serv.recv(5, socket.MSG_TRUNC) - self.assertEqual(msg, MSG[:5]) + msg = self.serv.recv(len(MSG) - 5, socket.MSG_TRUNC) + self.assertEqual(msg, MSG[:-5]) # Read the whole second datagram msg = self.serv.recv(len(MSG2) + 5, socket.MSG_TRUNC) @@ -3325,8 +3325,8 @@ def _testRecvTrunc(self): @unittest.skipIf(sys.platform == "win32", "Windows raises WSAEMSGSIZE") def testRecvFromTrunc(self): # Truncated read where recvfrom() returns the real size of the datagram - msg, addr = self.serv.recvfrom(5, socket.MSG_TRUNC) - self.assertEqual(msg, MSG[:5]) + msg, addr = self.serv.recvfrom(len(MSG) - 5, socket.MSG_TRUNC) + self.assertEqual(msg, MSG[:-5]) # Read the whole second datagram msg, addr = self.serv.recvfrom(len(MSG2) + 5, socket.MSG_TRUNC) From cb52deb89f4e5aff5bf9b6a5509ae56b4bc159c0 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 26 Nov 2026 21:05:07 +0100 Subject: [PATCH 5/7] Rephrase the Changelog entry --- .../2026-10-25-19-04-57.gh-issue-160622.7n56jv.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2026-10-25-19-04-57.gh-issue-160622.7n56jv.rst b/Misc/NEWS.d/next/Library/2026-10-25-19-04-57.gh-issue-160622.7n56jv.rst index e1880ba65b2e687..587d393d77c4665 100644 --- a/Misc/NEWS.d/next/Library/2026-10-25-19-04-57.gh-issue-160622.7n56jv.rst +++ b/Misc/NEWS.d/next/Library/2026-10-25-19-04-57.gh-issue-160622.7n56jv.rst @@ -1,4 +1,4 @@ -:meth:`socket.socket.recv` and :meth:`socket.socket.recvfrom` no longer -raise :exc:`ValueError` and lose the datagram when called with -``MSG_TRUNC`` and the datagram is longer than *bufsize*. Only the bytes -actually received are returned now. +Fix :meth:`socket.socket.recv` and :meth:`socket.socket.recvfrom` for the +:data:`!socket.MSG_TRUNC` flag: only receive up to *bufsize* bytes even if the +C function :c:func:`!recv` or :c:func:`!recvfrom` returns more bytes (the real +datagram size). From 11cb4cbf3205367cb397971baddb2bbdca3bb8ea Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 26 Nov 2026 21:07:33 +0100 Subject: [PATCH 6/7] Change the issue number to gh-69121 --- ...2.7n56jv.rst => 2026-10-25-19-04-57.gh-issue-69121.7n56jv.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/NEWS.d/next/Library/{2026-10-25-19-04-57.gh-issue-160622.7n56jv.rst => 2026-10-25-19-04-57.gh-issue-69121.7n56jv.rst} (100%) diff --git a/Misc/NEWS.d/next/Library/2026-10-25-19-04-57.gh-issue-160622.7n56jv.rst b/Misc/NEWS.d/next/Library/2026-10-25-19-04-57.gh-issue-69121.7n56jv.rst similarity index 100% rename from Misc/NEWS.d/next/Library/2026-10-25-19-04-57.gh-issue-160622.7n56jv.rst rename to Misc/NEWS.d/next/Library/2026-10-25-19-04-57.gh-issue-69121.7n56jv.rst From 18dc11d89e52b668c3c597e8d3babc97f8abc541 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 26 Nov 2026 21:14:47 +0100 Subject: [PATCH 7/7] Test also MSG_TRUNC | MSG_PEEK --- Lib/test/test_socket.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 1b2ae31303a22cc..73c575436e76cbd 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -3321,6 +3321,19 @@ def _testRecvTrunc(self): self.cli.sendto(MSG, 0, (HOST, self.port)) self.cli.sendto(MSG2, 0, (HOST, self.port)) + @requireAttrs(socket, "MSG_TRUNC") + @requireAttrs(socket, "MSG_PEEK") + @unittest.skipIf(sys.platform == "win32", "Windows raises WSAEMSGSIZE") + def testRecvTruncPeek(self): + msg = self.serv.recv(len(MSG) - 5, socket.MSG_TRUNC | socket.MSG_PEEK) + self.assertEqual(msg, MSG[:-5]) + + msg = self.serv.recv(len(MSG)) + self.assertEqual(msg, MSG) + + def _testRecvTruncPeek(self): + self.cli.sendto(MSG, 0, (HOST, self.port)) + @requireAttrs(socket, "MSG_TRUNC") @unittest.skipIf(sys.platform == "win32", "Windows raises WSAEMSGSIZE") def testRecvFromTrunc(self): @@ -3336,6 +3349,20 @@ def _testRecvFromTrunc(self): self.cli.sendto(MSG, 0, (HOST, self.port)) self.cli.sendto(MSG2, 0, (HOST, self.port)) + @requireAttrs(socket, "MSG_TRUNC") + @requireAttrs(socket, "MSG_PEEK") + @unittest.skipIf(sys.platform == "win32", "Windows raises WSAEMSGSIZE") + def testRecvFromTruncPeek(self): + msg, addr = self.serv.recvfrom(len(MSG) - 5, + socket.MSG_TRUNC | socket.MSG_PEEK) + self.assertEqual(msg, MSG[:-5]) + + msg, addr = self.serv.recvfrom(len(MSG)) + self.assertEqual(msg, MSG) + + def _testRecvFromTruncPeek(self): + self.cli.sendto(MSG, 0, (HOST, self.port)) + @unittest.skipUnless(HAVE_SOCKET_UDPLITE, 'UDPLITE sockets required for this test.')