]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-153932: protect read of en_index during enumerate.reduce (GH-154118)
authorL. Le <lelynn11@gmail.com>
Sun, 19 Jul 2026 13:20:56 +0000 (15:20 +0200)
committerGitHub <noreply@github.com>
Sun, 19 Jul 2026 13:20:56 +0000 (15:20 +0200)
Lib/test/libregrtest/tsan.py
Lib/test/test_enumerate.py
Misc/NEWS.d/next/Core_and_Builtins/2026-07-19-12-33-27.gh-issue-153932.lKw2bo.rst [new file with mode: 0644]
Objects/enumobject.c

index bacfe5e21ba0b7dd612160fed3383d7ba391e0c6..c7ec63763bc9d859182825e61e53c1e6ab8b6d2b 100644 (file)
@@ -8,6 +8,7 @@ TSAN_TESTS = [
     'test_ctypes',
     'test_concurrent_futures',
     'test_enum',
+    'test_enumerate',
     'test_functools',
     'test_httpservers',
     'test_imaplib',
index 5cb54cff9b76fdd588e08116f35a7777bbfa3385..c8b85fe869217818b9135f4fe6e48d7ad0de1c0d 100644 (file)
@@ -3,8 +3,11 @@ import operator
 import sys
 import pickle
 import gc
+import threading
+
 
 from test import support
+from test.support import threading_helper
 
 class G:
     'Sequence using __getitem__'
@@ -292,5 +295,28 @@ class TestLongStart(EnumerateStartTestCase):
                        (sys.maxsize+3,'c')]
 
 
+@threading_helper.requires_working_threading()
+class TestThreadSafety(EnumerateStartTestCase):
+    def test_thread_safety_while_iterating(self):
+        # gh-153932: calling reduce while iterating should pass with TSAN
+
+        en = enumerate(range(10_000))
+        stop = threading.Event()
+
+        def advance():
+            for _ in en:
+                pass
+            stop.set()
+
+        def read():
+            while not stop.is_set():
+                en.__reduce__()
+
+        threads = [threading.Thread(target=advance), threading.Thread(target=read)]
+
+        with threading_helper.start_threads(threads):
+            pass
+
+
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-19-12-33-27.gh-issue-153932.lKw2bo.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-19-12-33-27.gh-issue-153932.lKw2bo.rst
new file mode 100644 (file)
index 0000000..56b9630
--- /dev/null
@@ -0,0 +1,2 @@
+Fix thread safety issue in the ``__reduce__`` method of
+:py:class:`enumerate`.
index fc53f1bfee8dde47b3fa6463b9cad783bac7cadc..68aa594c5540ceea5e3f5ec9364a8494c1bb6f99 100644 (file)
@@ -277,10 +277,13 @@ enum_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
     enumobject *en = _enumobject_CAST(op);
     PyObject *result;
     Py_BEGIN_CRITICAL_SECTION(en);
-    if (en->en_longindex != NULL)
+    if (en->en_longindex != NULL) {
         result = Py_BuildValue("O(OO)", Py_TYPE(en), en->en_sit, en->en_longindex);
-    else
-        result = Py_BuildValue("O(On)", Py_TYPE(en), en->en_sit, en->en_index);
+    }
+    else {
+        Py_ssize_t en_index = FT_ATOMIC_LOAD_SSIZE_RELAXED(en->en_index);
+        result = Py_BuildValue("O(On)", Py_TYPE(en), en->en_sit, en_index);
+    }
     Py_END_CRITICAL_SECTION();
     return result;
 }