[issue21913] Possible deadlock in threading.Condition.wait() in Python 2.7.7

2014-07-03 Thread Sangeeth Saravanaraj

New submission from Sangeeth Saravanaraj:

Python version 2.7.7
Mac OS Darwin Kernel Version 13.2.0

I have the following code which when executed waits to be interrupted by 
SIGINT, SIGTERM or SIGQUIT. When an object is initialized, it creates a 
threading.Condition() and acquires() it! The program then registers the signal 
handlers where notify() and release() is called when the above mentioned 
signals are received. After registering the signal handlers, it calls wait() on 
the condition variable and block.

When I tried to stop the program with Ctrl-C, its did not respond. IOW, the 
_signal_handler() method did not get called.  

# start

from signal import signal, SIGINT, SIGTERM, SIGQUIT
from threading import Condition

class A:
def __init__(self):
self._termination_signal = Condition()
self._termination_signal.acquire(blocking=0)

def _signal_handler(self, signum, frame):
print "Received terminate request - signal = {0}".format(signum)
del frame
self._termination_signal.notify()
self._termination_signal.release()
return

def register_and_wait(self):
signal(SIGINT, self._signal_handler)
signal(SIGTERM, self._signal_handler)
signal(SIGQUIT, self._signal_handler)
print "Waiting to be interrupted!"
self._termination_signal.wait()  # control blocks here!
print "Notified!!"

def main():
a = A()
a.register_and_wait()

if __name__ == "__main__":
main()

# end


When the same code was tried in Python 3.4, it threw a "RuntimeError: cannot 
notify on un-acquired lock".

More information is available in this conversation in python-list mailer - 
https://mail.python.org/pipermail/python-list/2014-July/674350.html

--
components: Library (Lib)
messages: 50
nosy: pitrou, sangeeth
priority: normal
severity: normal
status: open
title: Possible deadlock in threading.Condition.wait() in Python 2.7.7
type: behavior
versions: Python 2.7

___
Python tracker 
<http://bugs.python.org/issue21913>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21913] Possible deadlock in threading.Condition.wait() in Python 2.7.7

2014-07-03 Thread Sangeeth Saravanaraj

Sangeeth Saravanaraj added the comment:

I am convinced that the code is wrong. It was written with wrong assumptions. 
But Python 2.7 behaves differently compare to Python 3.4. I am not expecting 
the same behavior in Python 2.7 as in Python 3.4 but I am expecting that some 
form of exception is raised justifying why Condition.notify() is illegal (as 
per #4) in this scenario and not block.

In 3.4, I could see that the signal handler is being called (when hitting 
Ctrl-c) and then Condition.notify() is raising a RuntimeError. But in 2.7, I am 
not able to prove that the signal handler is called at all. That makes me think 
why the signal is not caught when control is in Condition.wait().  

Your updated code snippet behaves the same as my original code. It blocks at 
Condition.wait() and does not respond to Ctrl-C and no signal handler is called 
and also not raising any exceptions.

Thanks for the beautiful explanation!

--

___
Python tracker 
<http://bugs.python.org/issue21913>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com