summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorDino Viehland <dinoviehland@meta.com>2024-05-02 20:03:05 (GMT)
committerGitHub <noreply@github.com>2024-05-02 20:03:05 (GMT)
commit1e67b9207c31a92d76bfac09fc706c4dc703669e (patch)
tree5f6f4bee04d4168daf715f70ef307518ad956594 /Lib/test
parent8ed546679524140d8282175411fd141fe7df070d (diff)
downloadcpython-1e67b9207c31a92d76bfac09fc706c4dc703669e.zip
cpython-1e67b9207c31a92d76bfac09fc706c4dc703669e.tar.gz
cpython-1e67b9207c31a92d76bfac09fc706c4dc703669e.tar.bz2
gh-117657: Fix TSAN list set failure (#118260)
* Fix TSAN list set failure * Relaxed atomic is sufficient, add targetted test * More list * Remove atomic assign in list * Fixup white space
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_free_threading/test_list.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/Lib/test/test_free_threading/test_list.py b/Lib/test/test_free_threading/test_list.py
new file mode 100644
index 0000000..79cb0a9
--- /dev/null
+++ b/Lib/test/test_free_threading/test_list.py
@@ -0,0 +1,80 @@
+import unittest
+
+from threading import Thread
+from unittest import TestCase
+
+from test.support import is_wasi
+
+
+class C:
+ def __init__(self, v):
+ self.v = v
+
+
+@unittest.skipIf(is_wasi, "WASI has no threads.")
+class TestList(TestCase):
+ def test_racing_iter_append(self):
+
+ l = []
+ OBJECT_COUNT = 10000
+
+ def writer_func():
+ for i in range(OBJECT_COUNT):
+ l.append(C(i + OBJECT_COUNT))
+
+ def reader_func():
+ while True:
+ count = len(l)
+ for i, x in enumerate(l):
+ self.assertEqual(x.v, i + OBJECT_COUNT)
+ if count == OBJECT_COUNT:
+ break
+
+ writer = Thread(target=writer_func)
+ readers = []
+ for x in range(30):
+ reader = Thread(target=reader_func)
+ readers.append(reader)
+ reader.start()
+
+ writer.start()
+ writer.join()
+ for reader in readers:
+ reader.join()
+
+ def test_racing_iter_extend(self):
+ iters = [
+ lambda x: [x],
+ ]
+ for iter_case in iters:
+ with self.subTest(iter=iter_case):
+ l = []
+ OBJECT_COUNT = 10000
+
+ def writer_func():
+ for i in range(OBJECT_COUNT):
+ l.extend(iter_case(C(i + OBJECT_COUNT)))
+
+ def reader_func():
+ while True:
+ count = len(l)
+ for i, x in enumerate(l):
+ self.assertEqual(x.v, i + OBJECT_COUNT)
+ if count == OBJECT_COUNT:
+ break
+
+ writer = Thread(target=writer_func)
+ readers = []
+ for x in range(30):
+ reader = Thread(target=reader_func)
+ readers.append(reader)
+ reader.start()
+
+ writer.start()
+ writer.join()
+ for reader in readers:
+ reader.join()
+
+
+if __name__ == "__main__":
+ unittest.main()