Skip to content

gh-69121: Fix socket recv() and recvfrom() with MSG_TRUNC - #160677

Open
ViderStar wants to merge 8 commits into
python:mainfrom
ViderStar:gh-160622-recv-msg-trunc
Open

ViderStar wants to merge 8 commits into
python:mainfrom
ViderStar:gh-160622-recv-msg-trunc

Conversation

@ViderStar

@ViderStar ViderStar commented Oct 25, 2026 •

Copy link
Copy Markdown

With MSG_TRUNC, Linux recv(2)/recvfrom(2) return the real length of the datagram, which can be larger than the buffer. Since gh-157226, PyBytesWriter_FinishWithSize() rejects a size larger than the allocated one, so sock.recv(n, MSG_TRUNC) and sock.recvfrom(n, MSG_TRUNC) raise ValueError after the datagram has already been consumed.

This clamps the returned length to the requested size, so only the bytes actually received are returned (as suggested in gh-69121). recv_into() and recvfrom_into() are unchanged: they still return the value from the system call as is, which is the way to get the full datagram length.

Tests: added testRecvTrunc and testRecvFromTrunc to BasicUDPTest. On a Linux debug build they fail on main with ValueError: size larger than allocated size and pass with this change; full test_socket passes on Linux and macOS.

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.
@python-cla-bot

python-cla-bot Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@ViderStar

Copy link
Copy Markdown
Author

@vstinner could you take a look? This is a regression from gh-157226: with MSG_TRUNC Linux returns the full datagram length, and PyBytesWriter_FinishWithSize() now raises ValueError after the datagram has already been consumed. The fix clamps the length to the buffer size; tests included.

@vstinner vstinner changed the title gh-160622: Fix socket recv() and recvfrom() with MSG_TRUNC gh-69121: Fix socket recv() and recvfrom() with MSG_TRUNC Nov 26, 2026
@vstinner

Copy link
Copy Markdown
Member

It seems like only recv() and recvfrom() should be fixed: recvmsg() ignores MSG_TRUNC flag, but it can set MSG_TRUNC flag in the returned flags.

@read-the-docs-community

read-the-docs-community Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #36197342 | 📁 Comparing 18dc11d against main (2bcfe11)

  🔍 Preview build  

2 files changed
± library/socket.html
± whatsnew/changelog.html

@vstinner vstinner added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Nov 26, 2026
@vstinner

Copy link
Copy Markdown
Member

@ViderStar: Next time, please ask the issue reporter (@kolyshkin) if they want to work on a pull request, rather than directly propose your PR.

@ViderStar: I pushed directly to your branch since I wanted to change multiple times. Can you please review the updated PR and tells me if you look good to you?

@kolyshkin: Can you also please review and test the change?

I modified the recv() and recvfrom() documentation to document MSG_TRUNC flag. In practice with this change, the flag is basically ignored. But instead, I tried to describe the behavior, since the flag is not technically ignored.

@vstinner

Copy link
Copy Markdown
Member

Ah, and I also added a test on MSG_TRUNC | MSG_PEEK flags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants