[Import-sig] Bug Fix for Installer

Anthony Tuininga anthony@computronix.com
15 Jul 2002 10:25:11 -0600


Well, after a little more sleuthing I discovered the source of the
problem with the locking within the import hooks in Installer.

The _release function needs to have one line added:

    def _acquire(self):
        if self.rlock.locked():
            if self.locker == self._get_ident():
                self.lockcount = self.lockcount + 1
##                print "_acquire incrementing lockcount to",
self.lockcount
                return
        self.rlock.acquire()
        self.locker = self._get_ident()
        self.lockcount = 0
##        print "_acquire first time!"


    def _release(self):
        if self.lockcount:
            self.lockcount = self.lockcount - 1
##            print "_release decrementing lockcount to", self.lockcount
        else:
#---------------------------------------------------------------------------
# THIS LINE WAS ADDED (BELOW)
            self.locker = None
# THIS LINE WAS ADDED (ABOVE)
#---------------------------------------------------------------------------
            self.rlock.release()
##            print "_release releasing lock!"

Without that line, the following scenario can occur.....

1) Thread 1 releases the lock
2) Thread 2 acquires the lock but does __NOT__ set who the waiter is (it
loses control before it can set the locker)
3) Thread 1 sees that the lock is acquired and thinks that it still owns
the lock so simply increments the lock count
4) Thread 2 sets the locker and the lockcount to 0
5) Thread 1 releases the lock -- OOPS!

If this doesn't make any sense to you, fire away! This (in my opinion)
is a really good reason for why the import lock needs to be held in core
Python in order to avoid this sort of problem. Multi-threaded
applications are a pain to deal with!

On another note, a slight improvement in performance could likely be
seen by checking sys.modules __before__ acquiring the lock and simply
returning. Note that sys.modules will have to be checked again after the
acquisition of the lock since some other thread may have imported it
while this thread was waiting for the lock to be acquired. Any comments?

Gordon, any thoughts on when this could be integrated into Installer?

-- 
Anthony Tuininga
anthony@computronix.com
 
Computronix
Distinctive Software. Real People.
Suite 200, 10216 - 124 Street NW
Edmonton, AB, Canada  T5N 4A3
Phone:	(780) 454-3700
Fax:	(780) 454-3838
http://www.computronix.com