Title: `marshal.dumps()` crashes when a set is modified while it is being marshalled · Issue #159185 · python/cpython · GitHub
Open Graph Title: `marshal.dumps()` crashes when a set is modified while it is being marshalled · Issue #159185 · python/cpython
X Title: `marshal.dumps()` crashes when a set is modified while it is being marshalled · Issue #159185 · python/cpython
Description: Crash report To sort the elements of a set, w_complex_object() reads the size of the set, allocates a list of that size, then marshals each element on its own in a loop. Marshalling an element can run Python code (an element's __buffer__...
Open Graph Description: Crash report To sort the elements of a set, w_complex_object() reads the size of the set, allocates a list of that size, then marshals each element on its own in a loop. Marshalling an element can ...
X Description: Crash report To sort the elements of a set, w_complex_object() reads the size of the set, allocates a list of that size, then marshals each element on its own in a loop. Marshalling an element can ...
Opengraph URL: https://github.com/python/cpython/issues/159185
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`marshal.dumps()` crashes when a set is modified while it is being marshalled","articleBody":"### Crash report\n\nTo sort the elements of a set, `w_complex_object()` reads the size of the\nset, allocates a list of that size, then marshals each element on its own\nin a loop. Marshalling an element can run Python code (an element's\n`__buffer__`, an audit hook), and that code can change the set. If the set\ngrows, the loop writes past the end of the list. If it shrinks, the list\nis left with NULL items and is then sorted.\n\n```python\nimport marshal, sys\n\nclass Item:\n def __hash__(self):\n return 0\n def __buffer__(self, flags):\n if sys.argv[1] == 'grow':\n s.update(range(1000, 1400))\n else:\n s.difference_update(range(1, 60))\n return memoryview(b'abc')\n\ns = {Item()}\ns.update(range(1, 60))\nmarshal.dumps(s)\n```\n\nOn main:\n\n- `shrink`, release build: segmentation fault.\n- `grow`, release build: no error; writes past the list allocation.\n- `grow`, debug build:\n `PyList_SET_ITEM: Assertion 'index \u003c list->allocated' failed.`\n\nOn the free-threaded build another thread changing the set between the size\nread and the critical section has the same effect.\n\n### Proposed fix\n\nStop the loop at the size that was read, and raise\n`RuntimeError(\"set changed size during iteration\")` if the number of\nelements seen differs from it, as iterating a set does.\n\n---\n\nDiscovered using Claude while doing a deep dive on pyc compilation determinism.\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\n_No response_\n\n### Output from running 'python -VV' on the command line:\n\n_No response_\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-159195\n* gh-159199\n* gh-159200\n* gh-159201\n* gh-159202\n* gh-159203\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/gpshead","@type":"Person","name":"gpshead"},"datePublished":"2026-10-11T17:37:37Z","url":"https://github.com/python/cpython/issues/159185"}
| 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:f9388def-89b1-7294-aac6-4f54b0d28b09 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DD3E:17FB7:313BF7F:3E76A25:6B02013E |
| html-safe-nonce | 5feface213ae36a741f11bdc1fca20cdf5fb1ec0246cb8b9516c7126aef8ab9a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERDNFOjE3RkI3OjMxM0JGN0Y6M0U3NkEyNTo2QjAyMDEzRSIsInZpc2l0b3JfaWQiOiI2ODQ3MTkwODI2NzcxMTUzMjE0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 2e342b4175aabb2fdc6deff85bfbb02a3e671b1b2fc0accb881a2bd2424ca1cf |
| hovercard-subject-tag | issue:5803499548 |
| 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/159185/issue_layout |
| twitter:image | https://opengraph.githubassets.com/390d90c60c79afe45bc88e31a23246151368e8e818e8bdcb26d0c10ab255da75/python/cpython/issues/159185 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/390d90c60c79afe45bc88e31a23246151368e8e818e8bdcb26d0c10ab255da75/python/cpython/issues/159185 |
| og:image:alt | Crash report To sort the elements of a set, w_complex_object() reads the size of the set, allocates a list of that size, then marshals each element on its own in a loop. Marshalling an element can ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gpshead |
| 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