Skip to content

Commit b1e7361

Browse files
authored
bpo-38377: Add support.skip_if_broken_multiprocessing_synchronize() (GH-20944) (GH-20962)
On Linux, skip tests using multiprocessing if the current user cannot create a file in /dev/shm/ directory. Add the skip_if_broken_multiprocessing_synchronize() function to the test.support module. (cherry picked from commit ddbeb2f)
1 parent 610a60c commit b1e7361

File tree

8 files changed

+41
-12
lines changed

8 files changed

+41
-12
lines changed

Lib/test/_test_multiprocessing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
# Skip tests if _multiprocessing wasn't built.
3434
_multiprocessing = test.support.import_module('_multiprocessing')
3535
# Skip tests if sem_open implementation is broken.
36-
test.support.import_module('multiprocessing.synchronize')
36+
support.skip_if_broken_multiprocessing_synchronize()
3737
import threading
3838

3939
import multiprocessing.connection

Lib/test/support/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3168,3 +3168,26 @@ def save_restore_warnings_filters():
31683168
yield
31693169
finally:
31703170
warnings.filters[:] = old_filters
3171+
3172+
3173+
def skip_if_broken_multiprocessing_synchronize():
3174+
"""
3175+
Skip tests if the multiprocessing.synchronize module is missing, if there
3176+
is no available semaphore implementation, or if creating a lock raises an
3177+
OSError.
3178+
"""
3179+
3180+
# Skip tests if the _multiprocessing extension is missing.
3181+
import_module('_multiprocessing')
3182+
3183+
# Skip tests if there is no available semaphore implementation:
3184+
# multiprocessing.synchronize requires _multiprocessing.SemLock.
3185+
synchronize = import_module('multiprocessing.synchronize')
3186+
3187+
try:
3188+
# bpo-38377: On Linux, creating a semaphore is the current user
3189+
# does not have the permission to create a file in /dev/shm.
3190+
# Create a semaphore to check permissions.
3191+
synchronize.Lock(ctx=None)
3192+
except OSError as exc:
3193+
raise unittest.SkipTest(f"broken multiprocessing SemLock: {exc!r}")

Lib/test/test_asyncio/test_events.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,10 +2672,10 @@ def tearDown(self):
26722672
if sys.platform != 'win32':
26732673

26742674
def test_get_event_loop_new_process(self):
2675-
# Issue bpo-32126: The multiprocessing module used by
2675+
# bpo-32126: The multiprocessing module used by
26762676
# ProcessPoolExecutor is not functional when the
26772677
# multiprocessing.synchronize module cannot be imported.
2678-
support.import_module('multiprocessing.synchronize')
2678+
support.skip_if_broken_multiprocessing_synchronize()
26792679

26802680
async def main():
26812681
pool = concurrent.futures.ProcessPoolExecutor()

Lib/test/test_concurrent_futures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Skip tests if _multiprocessing wasn't built.
44
support.import_module('_multiprocessing')
55
# Skip tests if sem_open implementation is broken.
6-
support.import_module('multiprocessing.synchronize')
6+
support.skip_if_broken_multiprocessing_synchronize()
77

88
from test.support import hashlib_helper
99
from test.support.script_helper import assert_python_ok

Lib/test/test_logging.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3629,9 +3629,9 @@ def test_handle_called_with_queue_queue(self, mock_handle):
36293629

36303630
@patch.object(logging.handlers.QueueListener, 'handle')
36313631
def test_handle_called_with_mp_queue(self, mock_handle):
3632-
# Issue 28668: The multiprocessing (mp) module is not functional
3632+
# bpo-28668: The multiprocessing (mp) module is not functional
36333633
# when the mp.synchronize module cannot be imported.
3634-
support.import_module('multiprocessing.synchronize')
3634+
support.skip_if_broken_multiprocessing_synchronize()
36353635
for i in range(self.repeat):
36363636
log_queue = multiprocessing.Queue()
36373637
self.setup_and_log(log_queue, '%s_%s' % (self.id(), i))
@@ -3655,9 +3655,9 @@ def test_no_messages_in_queue_after_stop(self):
36553655
indicates that messages were not registered on the queue until
36563656
_after_ the QueueListener stopped.
36573657
"""
3658-
# Issue 28668: The multiprocessing (mp) module is not functional
3658+
# bpo-28668: The multiprocessing (mp) module is not functional
36593659
# when the mp.synchronize module cannot be imported.
3660-
support.import_module('multiprocessing.synchronize')
3660+
support.skip_if_broken_multiprocessing_synchronize()
36613661
for i in range(self.repeat):
36623662
queue = multiprocessing.Queue()
36633663
self.setup_and_log(queue, '%s_%s' %(self.id(), i))

Lib/test/test_multiprocessing_main_handling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
AVAILABLE_START_METHODS = set(multiprocessing.get_all_start_methods())
2424

2525
# Issue #22332: Skip tests if sem_open implementation is broken.
26-
support.import_module('multiprocessing.synchronize')
26+
support.skip_if_broken_multiprocessing_synchronize()
2727

2828
verbose = support.verbose
2929

Lib/test/test_venv.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
import tempfile
1717
from test.support import (captured_stdout, captured_stderr, requires_zlib,
1818
can_symlink, EnvironmentVarGuard, rmtree,
19-
import_module)
19+
import_module,
20+
skip_if_broken_multiprocessing_synchronize)
2021
import unittest
2122
import venv
2223
from unittest.mock import patch
@@ -357,10 +358,11 @@ def test_multiprocessing(self):
357358
"""
358359
Test that the multiprocessing is able to spawn.
359360
"""
360-
# Issue bpo-36342: Instantiation of a Pool object imports the
361+
# bpo-36342: Instantiation of a Pool object imports the
361362
# multiprocessing.synchronize module. Skip the test if this module
362363
# cannot be imported.
363-
import_module('multiprocessing.synchronize')
364+
skip_if_broken_multiprocessing_synchronize()
365+
364366
rmtree(self.env_dir)
365367
self.run_with_capture(venv.create, self.env_dir)
366368
envpy = os.path.join(os.path.realpath(self.env_dir),
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
On Linux, skip tests using multiprocessing if the current user cannot create
2+
a file in ``/dev/shm/`` directory. Add the
3+
:func:`~test.support.skip_if_broken_multiprocessing_synchronize` function to
4+
the :mod:`test.support` module.

0 commit comments

Comments
 (0)