an error in python lib?

Ulrich Eckhardt ulrich.eckhardt at dominolaser.com
Fri Oct 12 03:15:12 EDT 2012


Am 12.10.2012 00:06, schrieb Wenhua Zhao:
> On Wed, Oct 10, 2012 at 12:21 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>> Can you demonstrate an API bug that is caused by this?
>
> A simple demo of this error is:
[...]
>      print "in main cv._is_owned: ", cv._is_owned()

That is kind of cheating, because as far as I can tell that function is 
private and not even documented in any way. You can use wait() though, 
which should always raise a RuntimeError:

----------->8------------------------>8----------------------

import time
from threading import Condition, Lock, Thread

cv = Condition(Lock())

def do_acquire():
     cv.acquire()
     print "cv acquired in thread"
     time.sleep(5)
     cv.release()
     print "cv released in thread"

thread = Thread(target=do_acquire)
thread.start()

for i in range(10):
     try:
         cv.wait()
         print "in main cv.wait() succeeded"
     except RuntimeError:
         print "in main cv.wait() raised RuntimeError"
     time.sleep(1)

----------->8------------------------>8----------------------

This gives me the following output:

in main cv.wait() raised RuntimeErrorcv acquired in thread

Exception in thread Thread-1:
Traceback (most recent call last):
   File "C:\Python27\lib\threading.py", line 551, in __bootstrap_inner
     self.run()
   File "C:\Python27\lib\threading.py", line 504, in run
     self.__target(*self.__args, **self.__kwargs)
   File "ttest.py", line 10, in do_acquire
     cv.release()
error: release unlocked lock

Following that, the program hangs. It seems the wait() released the lock 
that it didn't own, causing the error in do_acquire(). It then hung in 
wait(), although it should have raised a RuntimeError instead.


Uli




More information about the Python-list mailing list