gh-127085: fix some data races in memoryview in free-threading (#127412)

This commit is contained in:
Edward Xu 2024-12-17 03:12:19 +08:00 committed by GitHub
parent 1d276ec6f8
commit 4937ba54c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 52 additions and 8 deletions

View file

@ -16,7 +16,8 @@ import pickle
import struct
from itertools import product
from test.support import import_helper
from test import support
from test.support import import_helper, threading_helper
class MyObject:
@ -733,5 +734,28 @@ class OtherTest(unittest.TestCase):
self.assertIsNone(wr())
@threading_helper.requires_working_threading()
@support.requires_resource("cpu")
class RacingTest(unittest.TestCase):
def test_racing_getbuf_and_releasebuf(self):
"""Repeatly access the memoryview for racing."""
from multiprocessing.managers import SharedMemoryManager
from threading import Thread
n = 100
with SharedMemoryManager() as smm:
obj = smm.ShareableList(range(100))
threads = []
for _ in range(n):
# Issue gh-127085, the `ShareableList.count` is just a convenient way to mess the `exports`
# counter of `memoryview`, this issue has no direct relation with `ShareableList`.
threads.append(Thread(target=obj.count, args=(1,)))
with threading_helper.start_threads(threads):
pass
del obj
if __name__ == "__main__":
unittest.main()