https://github.com/python/cpython/commit/15c75d7a8b86d9f76982f70a00b69b403458694f
commit: 15c75d7a8b86d9f76982f70a00b69b403458694f
branch: main
author: Duprat <ydup...@gmail.com>
committer: vstinner <vstin...@python.org>
date: 2025-04-17T11:41:30+02:00
summary:

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

Co-authored-by: Peter Bierma <zintensity...@gmail.com>

files:
A Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst
M Lib/multiprocessing/synchronize.py
M Lib/test/_test_multiprocessing.py

diff --git a/Lib/multiprocessing/synchronize.py 
b/Lib/multiprocessing/synchronize.py
index 771f1db8813852..30425047e9801a 100644
--- a/Lib/multiprocessing/synchronize.py
+++ b/Lib/multiprocessing/synchronize.py
@@ -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__()
diff --git a/Lib/test/_test_multiprocessing.py 
b/Lib/test/_test_multiprocessing.py
index 1cd5704905f95c..be6efc49e9489e 100644
--- a/Lib/test/_test_multiprocessing.py
+++ b/Lib/test/_test_multiprocessing.py
@@ -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):
@@ -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)
diff --git 
a/Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst 
b/Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst
new file mode 100644
index 00000000000000..862db02b819ee3
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-04-16-11-44-56.gh-issue-132561.ekkDPE.rst
@@ -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.

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to