Skip to content

Commit 15c75d7

Browse files
pythongh-132561: Fix the public multiprocessing.SemLock.locked method (python#132586)
Co-authored-by: Peter Bierma <[email protected]>
1 parent 0c356c8 commit 15c75d7

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

Lib/multiprocessing/synchronize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def _make_methods(self):
9191
self.release = self._semlock.release
9292

9393
def locked(self):
94-
return self._semlock._count() != 0
94+
return self._semlock._is_zero()
9595

9696
def __enter__(self):
9797
return self._semlock.__enter__()

Lib/test/_test_multiprocessing.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,27 @@ def test_lock(self):
14921492
self.assertFalse(lock.locked())
14931493
self.assertRaises((ValueError, threading.ThreadError), lock.release)
14941494

1495+
@classmethod
1496+
def _test_lock_locked_2processes(cls, lock, event, res):
1497+
lock.acquire()
1498+
res.value = lock.locked()
1499+
event.set()
1500+
1501+
def test_lock_locked_2processes(self):
1502+
if self.TYPE != 'processes':
1503+
self.skipTest('test not appropriate for {}'.format(self.TYPE))
1504+
1505+
lock = self.Lock()
1506+
event = self.Event()
1507+
res = self.Value('b', 0)
1508+
p = self.Process(target=self._test_lock_locked_2processes,
1509+
args=(lock, event, res))
1510+
p.start()
1511+
event.wait()
1512+
self.assertTrue(lock.locked())
1513+
self.assertTrue(res.value)
1514+
p.join()
1515+
14951516
@staticmethod
14961517
def _acquire_release(lock, timeout, l=None, n=1):
14971518
for _ in range(n):
@@ -1561,6 +1582,22 @@ def test_rlock(self):
15611582
self.assertFalse(lock.locked())
15621583
self.assertRaises((AssertionError, RuntimeError), lock.release)
15631584

1585+
def test_rlock_locked_2processes(self):
1586+
if self.TYPE != 'processes':
1587+
self.skipTest('test not appropriate for {}'.format(self.TYPE))
1588+
1589+
rlock = self.RLock()
1590+
event = self.Event()
1591+
res = Value('b', 0)
1592+
# target is the same as for the test_lock_locked_2processes test.
1593+
p = self.Process(target=self._test_lock_locked_2processes,
1594+
args=(rlock, event, res))
1595+
p.start()
1596+
event.wait()
1597+
self.assertTrue(rlock.locked())
1598+
self.assertTrue(res.value)
1599+
p.join()
1600+
15641601
def test_lock_context(self):
15651602
with self.Lock() as locked:
15661603
self.assertTrue(locked)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the public ``locked`` method of ``multiprocessing.SemLock`` class.
2+
Also adding 2 tests for the derivated :class:`multiprocessing.Lock` and :class:`multiprocessing.RLock` classes.

0 commit comments

Comments
 (0)