Repository navigation
gh-159185: Fix marshalling of a set modified while its elements are marshalled - #159195
Merged
Merged
Conversation
… are marshalled
Marshalling a set element can run Python code (__buffer__, an audit
hook) which changes the set. The number of elements was read before
the loop, so a set which grew was written past the end of the list and
one which shrank left NULL items in the list which was then sorted.
Raise RuntimeError("set changed size during iteration") instead.
|
Thanks @gpshead for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15. |
|
Sorry, @gpshead, I could not cleanly backport this to Please backport manually with cherry_picker, see the devguide for more information. |
|
Sorry, @gpshead, I could not cleanly backport this to Please backport manually with cherry_picker, see the devguide for more information. |
|
GH-159199 is a backport of this pull request to the 3.15 branch. |
|
GH-159200 is a backport of this pull request to the 3.14 branch. |
gpshead
added a commit
that referenced
this pull request
Oct 11, 2026
…s are marshalled (GH-159195) (#159200) Marshalling a set element can run Python code (__buffer__, an audit hook) which changes the set. The number of elements was read before the loop, so a set which grew was written past the end of the list and one which shrank left NULL items in the list which was then sorted. Raise RuntimeError("set changed size during iteration") instead. (cherry picked from commit cd71799)
gpshead
added a commit
that referenced
this pull request
Oct 11, 2026
…s are marshalled (GH-159195) (#159199) Marshalling a set element can run Python code (__buffer__, an audit hook) which changes the set. The number of elements was read before the loop, so a set which grew was written past the end of the list and one which shrank left NULL items in the list which was then sorted. Raise RuntimeError("set changed size during iteration") instead. (cherry picked from commit cd71799)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
To order the elements of a set,
marshalmarshals each element on its own. That can run Python code (an element's__buffer__(), an audit hook), and that code can change the set. The number of elements was read once, before the loop.PyList_SET_ITEM: Assertion 'index < list->allocated' failedw_complex_object: Assertion `i == n' failedWith this PR both raise
RuntimeError: set changed size during iteration, as iterating the set would.Change
flowchart TB subgraph before["main"] direction TB b1["n = size of the set"] --> b2["list of n slots"] b2 --> b3["for each element:<br>marshal it (may run Python code)<br>store in slot i++"] b3 -- "set grew" --> b4["writes past slot n"] b3 -- "set shrank" --> b5["slots left NULL,<br>then sorted"] end subgraph after["this PR"] direction TB a1["n = size of the set"] --> a2["list of n slots"] a2 --> a3["for each element:<br>stop if i == n<br>marshal it, store in slot i++"] a3 --> a4{"stopped early,<br>or i != n?"} a4 -- yes --> a5["RuntimeError"] a4 -- no --> a6["sort and write"] endThe change is two checks around the existing loop in
w_complex_object(). On the free-threaded build the same checks cover another thread changing the set between the size being read and the critical section being entered. A set that is changed without changing its size is not detected; that is memory safe, and the output is whatever was iterated.This touches the same block as the PR for gh-159184; whichever is merged second needs a one-line conflict resolved.
Tests
test_set_changed_size: grow and shrink, all marshal versions.marshal.dumps()crashes when a set is modified while it is being marshalled #159185