Title: socket.recv() and recvfrom() with MSG_TRUNC raise ValueError and lose the datagram since 3.15.1 · Issue #160622 · python/cpython · GitHub
Open Graph Title: socket.recv() and recvfrom() with MSG_TRUNC raise ValueError and lose the datagram since 3.15.1 · Issue #160622 · python/cpython
X Title: socket.recv() and recvfrom() with MSG_TRUNC raise ValueError and lose the datagram since 3.15.1 · Issue #160622 · python/cpython
Description: 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 ...
Open Graph Description: 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 ...
X Description: 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 ...
Opengraph URL: https://github.com/python/cpython/issues/160622
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"socket.recv() and recvfrom() with MSG_TRUNC raise ValueError and lose the datagram since 3.15.1","articleBody":"### Bug report\n\n### Bug description:\n\nSince 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:\n\n```python\nimport socket\na, b = socket.socketpair(socket.AF_UNIX, socket.SOCK_DGRAM)\na.send(b\"abcdefgh\")\na.send(b\"second\")\ntry:\n print(b.recv(2, socket.MSG_TRUNC))\nexcept ValueError as e:\n print(e)\ntry:\n print(b.recvfrom(2, socket.MSG_TRUNC))\nexcept ValueError as e:\n print(e)\nb.setblocking(False)\nb.recv(100) # BlockingIOError: both datagrams are gone\n```\n\n3.15.1:\n```\nsize larger than allocated size\nsize larger than allocated size\n```\n\n3.15.0 and 3.14 (the long-standing behavior, see gh-69121):\n```\nb'ab\\x00\\x00\\x00\\x00\\x00\\x00'\n(b'se\\x00\\x00\\x00\\x00', None)\n```\n\nWith `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.\n\nThe 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).\n\nRaising 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.\n\n### CPython versions tested on:\n\n3.14, 3.15\n\n### Operating systems tested on:\n\nLinux\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-160677\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/kolyshkin","@type":"Person","name":"kolyshkin"},"datePublished":"2026-10-24T20:42:46Z","url":"https://github.com/python/cpython/issues/160622"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0d0eb575-5ca1-3122-1ccb-a0048dfc6b36 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9E80:21BD98:2FBE968:3C93414:6B01FA0E |
| html-safe-nonce | fbd9fee0e13f809eed5f13e514a69661632742d6fef3a79acd418357f98da606 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RTgwOjIxQkQ5ODoyRkJFOTY4OjNDOTM0MTQ6NkIwMUZBMEUiLCJ2aXNpdG9yX2lkIjoiNTUxMDc2OTA3MzUzMDczMTAyMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 07585f5d02d1052ef59ede2c8eddef36f2fb99d47061cd1ffb53f1531aa15f20 |
| hovercard-subject-tag | issue:6017402638 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/160622/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d9afe9de21431920ee33d6bb5a9d976f6921a655985e726572e792a571c38fdd/python/cpython/issues/160622 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d9afe9de21431920ee33d6bb5a9d976f6921a655985e726572e792a571c38fdd/python/cpython/issues/160622 |
| og:image:alt | 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 ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kolyshkin |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width