[Python-bugs-list] Bug with Threaded exceptions? (PR#151)

Tim Peters tim_one@email.msn.com
Tue, 7 Dec 1999 00:47:03 -0500


Python defines neither the lifetime of objects nor makes any guarantee about
which threads will run finalizers.  Just facts of life!  Acquiring/releasing
a lock in an object ctor/dtor is a C++ idiom that doesn't carry over (C++
doesn't say anything about threads either, but does define the lifetime of
local vrbls).  It *usually* works OK in CPython, but usually doesn't in
JPython.

In your particular case, the local vrbl "a" is still alive after function
exit because it's still reachable via the traceback object created by the
exception.  Among other things, the traceback object contains the chain of
stack frames (which in turn contain the local vrbls) active at the time the
exception was triggered.  Binding sys.exc_traceback to, e.g., None will
usually make it unreachable again (and so trigger CPython's refcount-based
garbage collection of "a").

I don't know of any way around this except using by try/finally in one way
or another.  In any case, it's not a bug -- it's functioning as designed.