diff --git a/Lib/threading.py b/Lib/threading.py index a3cb245ab9606c742c53040d48e0518c236b5a32..565f868e928d2be6234e1765d7a85c48847e0dc5 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -360,14 +360,21 @@ def notify(self, n=1): """ 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 index 0000000000000000000000000000000000000000..8bb8ca0488c962554bf76c326f0775d0d7f101e4 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-05-09-09-28-02.gh-issue-92530.M4Q1RS.rst @@ -0,0 +1,2 @@ +Fix an issue that occurred after interrupting +:func:`threading.Condition.notify`.