Condition.wait(0.5) doesn't respect it's timeout

Piet van Oostrum piet at cs.uu.nl
Sat Apr 18 15:29:46 EDT 2009


>>>>> stephane.bisinger at gmail.com (SB) wrote:

>SB> On Apr 18, 10:24 am, Piet van Oostrum <p... at cs.uu.nl> wrote:
>>> 
>>> I haven't run it (too much hassle to setup) but I noticed one strange
>>> thing in your code:
>>> 
>>> ,----
>>> | def groupUpdated(self, gView):
>>> |         # Acquire the lock to do modifications
>>> |         self._mod_lock.acquire()
>>> |  
>>> |         if not self._groups.has_key(gView.uid):
>>> |             return
>>> `----
>>> 
>>> In case the return is taken, the lock will not be released thereby
>>> blocking the rest. It could be that the timeout is taken, but before the
>>> wait can continue it has to acquire the lock again.

>SB> That is absolutely correct, but I guess with an RLock there shouldn't
>SB> be a deadlock... Anyway I'm fixing that!

I suppose the groupUpdated() method is called from a different thread
than the self._mod_lock.wait(timeout=1) because it contains a notify().
Otherwise your code would have been flawed. From the code you can see
that this is the case as the thread with wait in it is created here (in
aMSNContactListWidget.__init__) and it contains no calls to
groupUpdated. Therefore the RLock thing isn't applicable. It was,
however, applicable in the superfluous acquire in the loop, so it saved
you there.

Maybe you can put print statements at the beginning and end of each
"with" block to see if there is an unreleased lock that blocks the wait.

Some more remarks:

1. __repaint acquires the lock, but it is called from __thread_run which
has already acquired it. Because the lock is recursive it doesn't harm
but it is not necessary.

2. Importing in a thread is discouraged. I think it is cleaner to put
   the import sys in the top of the module.
3. I think I have found a bug in the Condition implementation, but I
   can't imagine you being hit by it. It would not be a reproducible one.
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list