socket.recv() and recvfrom() with MSG_TRUNC raise ValueError and lose the datagram since 3.15.1
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 43/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- python
- 领域
- backend, networking
调研方向
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 内容生成。
描述
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.8k
- 派生
- 37.7k
- 平均合并
- 11 小时 25 分钟
- 30 天内合并 PR
- 723
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/cpython 的其他 Issue
-
Null dereferencing of a pointer in initconfig.c可能已有人在做 @lazerg 于 1 天前认领。 未关闭interpreter-core topic-C-API type-bug
难度 2/5 1-3 小时 新手友好度 72/100
python/cpython#160962 · 4 条评论 ·
维护者通常 1 天内回复
-
tests topic-profiling type-bug
难度 2/5 1-3 小时 新手友好度 76/100
python/cpython#160881 · 1 条评论 ·
维护者通常 1 天内回复
-
3.16 build pending type-bug
难度 2/5 1-3 小时 新手友好度 62/100
python/cpython#160829 · 5 条评论 ·
维护者通常 1 天内回复
-
dependencies infra topic-ensurepip
难度 2/5 1-3 小时 新手友好度 66/100
维护者通常 1 天内回复
-
symtable: repr() of a Symbol raises TypeError under -X lazy_imports=all可能已有人在做 @anistark 于 2 天前认领。 未关闭stdlib topic-lazy-imports type-bug
难度 2/5 1-3 小时 新手友好度 72/100
python/cpython#160810 · 4 条评论 ·
维护者通常 1 天内回复
相似的 Issue
-
_chunk_scan_fwd launches its Triton kernel without a device guard: NaN when a model is split across GPUs可能已有人在做 @toddwbucy 今天认领。 未关闭
难度 1/5 1 小时以内 新手友好度 85/100
state-spaces/mamba#1072 ·
-
难度 1/5 1 小时以内 新手友好度 85/100
loopj/aiovantage#414 ·
维护者通常 1 天内回复
-
power_mismatch_deline docstring: the rear-to-total RMAD conversion has an extra bifaciality factor未关闭
难度 1/5 1 小时以内 新手友好度 85/100
pvlib/pvlib-python#2929 ·
维护者通常 1 天内回复
-
data good first issue launch-day unverified
难度 2/5 1-3 小时 新手友好度 78/100
BradGroux/wow-forever-mission-control#8 ·
维护者通常 1 天内回复
-
check bilibili falsely reports read/subtitle as broken: live_test fixture BV1GJ411x7h7 is gone (404)未关闭
难度 2/5 1-3 小时 新手友好度 78/100
Panniantong/Agent-Reach#889 ·
维护者通常 2 天内回复