Skip to content

Commit 7f70ba3

Browse files
bpo-45310: Fix parrallel shared memory tests (GH-28661) (GH-28979)
Add a PID to names of POSIX shared memory objects to allow running multiprocessing tests (test_multiprocessing_fork, test_multiprocessing_spawn, etc) in parallel. (cherry picked from commit eb4495e) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 1d8cb01 commit 7f70ba3

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

Lib/test/_test_multiprocessing.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3749,12 +3749,19 @@ def _attach_existing_shmem_then_write(shmem_name_or_obj, binary_data):
37493749
local_sms.buf[:len(binary_data)] = binary_data
37503750
local_sms.close()
37513751

3752+
def _new_shm_name(self, prefix):
3753+
# Add a PID to the name of a POSIX shared memory object to allow
3754+
# running multiprocessing tests (test_multiprocessing_fork,
3755+
# test_multiprocessing_spawn, etc) in parallel.
3756+
return prefix + str(os.getpid())
3757+
37523758
def test_shared_memory_basics(self):
3753-
sms = shared_memory.SharedMemory('test01_tsmb', create=True, size=512)
3759+
name_tsmb = self._new_shm_name('test01_tsmb')
3760+
sms = shared_memory.SharedMemory(name_tsmb, create=True, size=512)
37543761
self.addCleanup(sms.unlink)
37553762

37563763
# Verify attributes are readable.
3757-
self.assertEqual(sms.name, 'test01_tsmb')
3764+
self.assertEqual(sms.name, name_tsmb)
37583765
self.assertGreaterEqual(sms.size, 512)
37593766
self.assertGreaterEqual(len(sms.buf), sms.size)
37603767

@@ -3763,12 +3770,12 @@ def test_shared_memory_basics(self):
37633770
self.assertEqual(sms.buf[0], 42)
37643771

37653772
# Attach to existing shared memory segment.
3766-
also_sms = shared_memory.SharedMemory('test01_tsmb')
3773+
also_sms = shared_memory.SharedMemory(name_tsmb)
37673774
self.assertEqual(also_sms.buf[0], 42)
37683775
also_sms.close()
37693776

37703777
# Attach to existing shared memory segment but specify a new size.
3771-
same_sms = shared_memory.SharedMemory('test01_tsmb', size=20*sms.size)
3778+
same_sms = shared_memory.SharedMemory(name_tsmb, size=20*sms.size)
37723779
self.assertLess(same_sms.size, 20*sms.size) # Size was ignored.
37733780
same_sms.close()
37743781

@@ -3779,17 +3786,17 @@ def test_shared_memory_basics(self):
37793786
# manages unlinking on its own and unlink() does nothing).
37803787
# True release of shared memory segment does not necessarily
37813788
# happen until process exits, depending on the OS platform.
3789+
name_dblunlink = self._new_shm_name('test01_dblunlink')
3790+
sms_uno = shared_memory.SharedMemory(
3791+
name_dblunlink,
3792+
create=True,
3793+
size=5000
3794+
)
37823795
with self.assertRaises(FileNotFoundError):
3783-
sms_uno = shared_memory.SharedMemory(
3784-
'test01_dblunlink',
3785-
create=True,
3786-
size=5000
3787-
)
3788-
37893796
try:
37903797
self.assertGreaterEqual(sms_uno.size, 5000)
37913798

3792-
sms_duo = shared_memory.SharedMemory('test01_dblunlink')
3799+
sms_duo = shared_memory.SharedMemory(name_dblunlink)
37933800
sms_duo.unlink() # First shm_unlink() call.
37943801
sms_duo.close()
37953802
sms_uno.close()
@@ -3801,7 +3808,7 @@ def test_shared_memory_basics(self):
38013808
# Attempting to create a new shared memory segment with a
38023809
# name that is already in use triggers an exception.
38033810
there_can_only_be_one_sms = shared_memory.SharedMemory(
3804-
'test01_tsmb',
3811+
name_tsmb,
38053812
create=True,
38063813
size=512
38073814
)
@@ -3815,7 +3822,7 @@ def test_shared_memory_basics(self):
38153822
# case of MacOS/darwin, requesting a smaller size is disallowed.
38163823
class OptionalAttachSharedMemory(shared_memory.SharedMemory):
38173824
_flags = os.O_CREAT | os.O_RDWR
3818-
ok_if_exists_sms = OptionalAttachSharedMemory('test01_tsmb')
3825+
ok_if_exists_sms = OptionalAttachSharedMemory(name_tsmb)
38193826
self.assertEqual(ok_if_exists_sms.size, sms.size)
38203827
ok_if_exists_sms.close()
38213828

@@ -4003,10 +4010,11 @@ def test_shared_memory_ShareableList_basics(self):
40034010
self.assertEqual(sl.count(b'adios'), 0)
40044011

40054012
# Exercise creating a duplicate.
4006-
sl_copy = shared_memory.ShareableList(sl, name='test03_duplicate')
4013+
name_duplicate = self._new_shm_name('test03_duplicate')
4014+
sl_copy = shared_memory.ShareableList(sl, name=name_duplicate)
40074015
try:
40084016
self.assertNotEqual(sl.shm.name, sl_copy.shm.name)
4009-
self.assertEqual('test03_duplicate', sl_copy.shm.name)
4017+
self.assertEqual(name_duplicate, sl_copy.shm.name)
40104018
self.assertEqual(list(sl), list(sl_copy))
40114019
self.assertEqual(sl.format, sl_copy.format)
40124020
sl_copy[-1] = 77

0 commit comments

Comments
 (0)