classification
Title: Occasional RuntimeError from Condition.notify
Type: crash Stage: resolved
Components: Library (Lib) Versions: Python 3.5, Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: dougz, haypo, pitrou, python-dev, rhettinger, tim.peters
Priority: normal Keywords: patch

Created on 2014-08-11 23:47 by dougz, last changed 2014-08-29 21:28 by pitrou. This issue is now closed.

Files
File name Uploaded Description Edit
fix.diff dougz, 2014-08-11 23:47 move self._waiters modification inside lock review
Messages (5)
msg225208 - (view) Author: Doug Zongker (dougz) * Date: 2014-08-11 23:47
Condition.wait() modifies self._waiters without holding the lock (when a wait with timeout times out without the condition being notified).

If this happens to occur in between construction of the _islice and _deque objects in Condition.notify():

    def notify(self, n=1):
        [...]
        all_waiters = self._waiters
        waiters_to_notify = _deque(_islice(all_waiters, n))

then the result is a RuntimeError exception:

    File "/usr/lib/python3.4/threading.py", line 358, in notify_all
      self.notify(len(self._waiters))
    File "/usr/lib/python3.4/threading.py", line 341, in notify
      waiters_to_notify = _deque(_islice(all_waiters, n))
  RuntimeError: deque mutated during iteration

(I have a server which makes extensive use of conditions on which this happens about once a day.)

This patch fixes this bug by moving wait()'s modification of self._waiters to be inside the lock, as suggested by Antoine Pitrou here: http://bugs.python.org/issue17385#msg183875
msg225213 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2014-08-12 00:18
+1.  I agree it's a bug, that the diagnosis is correct, and that the patch will fix it :-)
msg226068 - (view) Author: Doug Zongker (dougz) * Date: 2014-08-29 17:18
So, what happens now?  What do I need to do to make progress on this?
msg226088 - (view) Author: Roundup Robot (python-dev) Date: 2014-08-29 21:27
New changeset 4cce39cfe46c by Antoine Pitrou in branch '3.4':
Issue #22185: Fix an occasional RuntimeError in threading.Condition.wait() caused by mutation of the waiters queue without holding the lock.
http://hg.python.org/cpython/rev/4cce39cfe46c

New changeset 78a38f8bd5d9 by Antoine Pitrou in branch 'default':
Issue #22185: Fix an occasional RuntimeError in threading.Condition.wait() caused by mutation of the waiters queue without holding the lock.
http://hg.python.org/cpython/rev/78a38f8bd5d9
msg226089 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-08-29 21:28
It only needed someone to push your patch, which I just did.
Thank you very much Doug, your contribution is appreciated!
History
Date User Action Args
2014-08-29 21:28:26pitrousetstatus: open -> closed
resolution: fixed
messages: + msg226089

stage: resolved
2014-08-29 21:27:42python-devsetnosy: + python-dev
messages: + msg226088
2014-08-29 17:18:44dougzsetmessages: + msg226068
2014-08-16 12:43:17hayposetnosy: + haypo
2014-08-12 00:18:58tim.peterssetmessages: + msg225213
2014-08-11 23:51:44pitrousetnosy: + tim.peters, rhettinger
2014-08-11 23:47:11dougzcreate