diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2022-05-16 08:25:29 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-16 08:25:29 +0300 |
commit | 70af994fee7c0850ae859727d9468a5f29375a38 (patch) | |
tree | f2a55c4a3a3348f6c3d2d3da1ca74f7c469c9f58 /Lib/threading.py | |
parent | 953ab0795243900ccccaaca069d932730a86fc20 (diff) | |
download | cpython-70af994fee7c0850ae859727d9468a5f29375a38.tar.gz cpython-70af994fee7c0850ae859727d9468a5f29375a38.zip |
gh-92530: Fix an issue that occurred after interrupting threading.Condition.notify (GH-92534)
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.
Diffstat (limited to 'Lib/threading.py')
-rw-r--r-- | Lib/threading.py | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/Lib/threading.py b/Lib/threading.py index 40edcde1153..a3df587f106 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -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 |