Skip to content

Commit fd6bd15

Browse files
[3.15] gh-153932: protect read of en_index during enumerate.reduce (GH-154118) (GH-154128)
gh-153932: protect read of en_index during enumerate.reduce (GH-154118) (cherry picked from commit 1604043) Co-authored-by: L. Le <lelynn11@gmail.com>
1 parent 7d1b64b commit fd6bd15

4 files changed

Lines changed: 35 additions & 3 deletions

File tree

Lib/test/libregrtest/tsan.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
'test_ctypes',
99
'test_concurrent_futures',
1010
'test_enum',
11+
'test_enumerate',
1112
'test_functools',
1213
'test_httpservers',
1314
'test_imaplib',

Lib/test/test_enumerate.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import sys
44
import pickle
55
import gc
6+
import threading
7+
68

79
from test import support
10+
from test.support import threading_helper
811

912
class G:
1013
'Sequence using __getitem__'
@@ -292,5 +295,28 @@ def enum(self, iterable, start=sys.maxsize + 1):
292295
(sys.maxsize+3,'c')]
293296

294297

298+
@threading_helper.requires_working_threading()
299+
class TestThreadSafety(EnumerateStartTestCase):
300+
def test_thread_safety_while_iterating(self):
301+
# gh-153932: calling reduce while iterating should pass with TSAN
302+
303+
en = enumerate(range(10_000))
304+
stop = threading.Event()
305+
306+
def advance():
307+
for _ in en:
308+
pass
309+
stop.set()
310+
311+
def read():
312+
while not stop.is_set():
313+
en.__reduce__()
314+
315+
threads = [threading.Thread(target=advance), threading.Thread(target=read)]
316+
317+
with threading_helper.start_threads(threads):
318+
pass
319+
320+
295321
if __name__ == "__main__":
296322
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix thread safety issue in the ``__reduce__`` method of
2+
:py:class:`enumerate`.

Objects/enumobject.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,13 @@ enum_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
277277
enumobject *en = _enumobject_CAST(op);
278278
PyObject *result;
279279
Py_BEGIN_CRITICAL_SECTION(en);
280-
if (en->en_longindex != NULL)
280+
if (en->en_longindex != NULL) {
281281
result = Py_BuildValue("O(OO)", Py_TYPE(en), en->en_sit, en->en_longindex);
282-
else
283-
result = Py_BuildValue("O(On)", Py_TYPE(en), en->en_sit, en->en_index);
282+
}
283+
else {
284+
Py_ssize_t en_index = FT_ATOMIC_LOAD_SSIZE_RELAXED(en->en_index);
285+
result = Py_BuildValue("O(On)", Py_TYPE(en), en->en_sit, en_index);
286+
}
284287
Py_END_CRITICAL_SECTION();
285288
return result;
286289
}

0 commit comments

Comments
 (0)