]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-92530: Fix an issue that occurred after interrupting threading.Condition.notify...
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 16 May 2022 05:25:29 +0000 (08:25 +0300)
committerGitHub <noreply@github.com>
Mon, 16 May 2022 05:25:29 +0000 (08:25 +0300)
If Condition.notify() was interrupted just after it released the waiter lock,
but before removing it from the queue, the following calls of notify() failed
with RuntimeError: cannot release un-acquired lock.

Lib/threading.py
Misc/NEWS.d/next/Library/2022-05-09-09-28-02.gh-issue-92530.M4Q1RS.rst [new file with mode: 0644]

index 40edcde11539d65521550f323cd2e81f1d68e382..a3df587f10666cbb52497fcb4de6b20f5da5a9eb 100644 (file)
@@ -368,14 +368,21 @@ class Condition:
         """
         if not self._is_owned():
             raise RuntimeError("cannot notify on un-acquired lock")
-        all_waiters = self._waiters
-        waiters_to_notify = _deque(_islice(all_waiters, n))
-        if not waiters_to_notify:
-            return
-        for waiter in waiters_to_notify:
-            waiter.release()
+        waiters = self._waiters
+        while waiters and n > 0:
+            waiter = waiters[0]
+            try:
+                waiter.release()
+            except RuntimeError:
+                # gh-92530: The previous call of notify() released the lock,
+                # but was interrupted before removing it from the queue.
+                # It can happen if a signal handler raises an exception,
+                # like CTRL+C which raises KeyboardInterrupt.
+                pass
+            else:
+                n -= 1
             try:
-                all_waiters.remove(waiter)
+                waiters.remove(waiter)
             except ValueError:
                 pass
 
diff --git a/Misc/NEWS.d/next/Library/2022-05-09-09-28-02.gh-issue-92530.M4Q1RS.rst b/Misc/NEWS.d/next/Library/2022-05-09-09-28-02.gh-issue-92530.M4Q1RS.rst
new file mode 100644 (file)
index 0000000..8bb8ca0
--- /dev/null
@@ -0,0 +1,2 @@
+Fix an issue that occurred after interrupting
+:func:`threading.Condition.notify`.