Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

socket.recv() and recvfrom() with MSG_TRUNC raise ValueError and lose the datagram since 3.15.1

未關閉
#160,622 0 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
43/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
活躍
技術堆疊
python

研究方向

Start by tracing socket.recv() and recvfrom() through sock_recv() and sock_recvfrom() to PyBytesWriter_FinishWithSize(), then inspect the change from gh-157226 and the existing behavior discussed in gh-69121. Reproduce the reported case on Linux and check how recvmsg() handles truncated datagrams. Done means defining and verifying behavior that avoids raising after consuming the datagram and avoids returning invalid padding bytes.

由索引模型根據 Issue 內容生成。

描述

extension-modules topic-socket type-bug
Bug report
Bug description:

Since 3.15.1, socket.recv(bufsize, MSG_TRUNC) and socket.recvfrom(bufsize, MSG_TRUNC) raise ValueError: size larger than allocated size when the datagram is longer than bufsize. Without MSG_PEEK, the datagram is already consumed by the time the exception is raised, so the data is lost:

import socket
a, b = socket.socketpair(socket.AF_UNIX, socket.SOCK_DGRAM)
a.send(b"abcdefgh")
a.send(b"second")
try:
    print(b.recv(2, socket.MSG_TRUNC))
except ValueError as e:
    print(e)
try:
    print(b.recvfrom(2, socket.MSG_TRUNC))
except ValueError as e:
    print(e)
b.setblocking(False)
b.recv(100)  # BlockingIOError: both datagrams are gone

3.15.1:

size larger than allocated size
size larger than allocated size

3.15.0 and 3.14 (the long-standing behavior, see gh-69121):

b'ab\x00\x00\x00\x00\x00\x00'
(b'se\x00\x00\x00\x00', None)

With MSG_TRUNC, Linux recv(2) returns the real length of the datagram even if it was longer than the buffer. sock_recv() and sock_recvfrom() pass that length to PyBytesWriter_FinishWithSize(), which, after c38a347e2c3199d7d3f6a9eefbf46df52caf725d (backport of gh-157226 to 3.15), rejects a size larger than the allocated one. The same code is in main.

The old behavior was a bug too (gh-69121): the returned object was grown to the real datagram length, with only the first bufsize bytes being valid. Still, it is a behavior change in a bugfix release that breaks working code. For example, CRIU's pycriu used len(s.recv(1, socket.MSG_TRUNC | socket.MSG_PEEK)) to find out the message size (fixed in checkpoint-restore/criu#3268 by using recv_into(), which returns the value from recv(2) as is).

Raising after the data is consumed seems to be the worst option. Returning the min(outlen, bufsize) bytes actually received (as suggested in gh-69121, and similar to what recvmsg() does) would avoid both the data loss and the garbage bytes.

CPython versions tested on:

3.14, 3.15

Operating systems tested on:

Linux

Linked PRs
  • gh-160677
主要語言
Python
星號
77.7k
分支
38.3k
平均合併
14 小時 29 分鐘
30 天內合併 PR
699

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。