Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion Lib/test/test_free_threading/test_collections.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from collections import deque
from collections import OrderedDict, deque
from copy import copy
from test.support import threading_helper

Expand Down Expand Up @@ -49,5 +49,30 @@ def mutate():
)


class TestOrderedDict(unittest.TestCase):
def test_iterator_update_clear_race(self):
# gh-151627: OrderedDict iterator construction must not race with
# concurrent clear()/update() operations that mutate the linked list.
od = OrderedDict((i, i) for i in range(100))

def mutate():
for i in range(5000):
od.clear()
od.update(((i, i), (i + 1, i + 1), (i + 2, i + 2)))

def iterate():
for _ in range(5000):
try:
for _ in od:
pass
list(reversed(od))
except RuntimeError:
pass

threading_helper.run_concurrently(
[mutate, *[iterate for _ in range(8)]],
)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash in :class:`collections.OrderedDict` iterators in free-threaded
builds when the dictionary is concurrently cleared or updated.
2 changes: 2 additions & 0 deletions Objects/odictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1942,11 +1942,13 @@ odictiter_new(PyODictObject *od, int kind)
}

di->kind = kind;
Py_BEGIN_CRITICAL_SECTION(od);
node = reversed ? _odict_LAST(od) : _odict_FIRST(od);
di->di_current = node ? Py_NewRef(_odictnode_KEY(node)) : NULL;
di->di_size = PyODict_SIZE(od);
di->di_state = od->od_state;
di->di_odict = (PyODictObject*)Py_NewRef(od);
Py_END_CRITICAL_SECTION();

_PyObject_GC_TRACK(di);
return (PyObject *)di;
Expand Down
Loading