Skip to content

gh-132561: Fix the public multiprocessing.SemLock.locked method #132586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/multiprocessing/synchronize.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def _make_methods(self):
self.release = self._semlock.release

def locked(self):
return self._semlock._count() != 0
return self._semlock._is_zero()

def __enter__(self):
return self._semlock.__enter__()
Expand Down
37 changes: 37 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,27 @@ def test_lock(self):
self.assertFalse(lock.locked())
self.assertRaises((ValueError, threading.ThreadError), lock.release)

@classmethod
def _test_lock_locked_2processes(cls, lock, event, res):
lock.acquire()
res.value = lock.locked()
event.set()

def test_lock_locked_2processes(self):
if self.TYPE != 'processes':
self.skipTest('test not appropriate for {}'.format(self.TYPE))

lock = self.Lock()
event = self.Event()
res = self.Value('b', 0)
p = self.Process(target=self._test_lock_locked_2processes,
args=(lock, event, res))
p.start()
event.wait()
self.assertTrue(lock.locked())
self.assertTrue(res.value)
p.join()

@staticmethod
def _acquire_release(lock, timeout, l=None, n=1):
for _ in range(n):
Expand Down Expand Up @@ -1561,6 +1582,22 @@ def test_rlock(self):
self.assertFalse(lock.locked())
self.assertRaises((AssertionError, RuntimeError), lock.release)

def test_rlock_locked_2processes(self):
if self.TYPE != 'processes':
self.skipTest('test not appropriate for {}'.format(self.TYPE))

rlock = self.RLock()
event = self.Event()
res = Value('b', 0)
# target is the same as for the test_lock_locked_2processes test.
p = self.Process(target=self._test_lock_locked_2processes,
args=(rlock, event, res))
p.start()
event.wait()
self.assertTrue(rlock.locked())
self.assertTrue(res.value)
p.join()

def test_lock_context(self):
with self.Lock() as locked:
self.assertTrue(locked)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix the public ``locked`` method of ``multiprocessing.SemLock`` class.
Also adding 2 tests for the derivated :class:`multiprocessing.Lock` and :class:`multiprocessing.RLock` classes.
Loading