[Patches] Safe destruction of recursive objects

Christian Tismer tismer@tismer.com
Mon, 06 Mar 2000 22:52:39 +0100


Hi Guido,

> I was about to check this in, when I read the following:
> 
> > +   It would be nice to do this using the thread state.
> > +   Also, we could do an exact stack measure then.
> > +   Unfortunately, deallocations also take place when
> > +   the thread state is undefined.
> 
> Really?  Do you have a reproducible example?  One that only involves
> standard Python modules?

I had forgotton about this already, but yes it is true.
I had implemented this stuff with the tstate already
but had to learn that this is impossible.

> It makes a difference whether this can happen with the thread state
> undefined but the global interpreter lock still held, or whether you
> think that there can be deallocations without the lock held -- the
> latter would break many of my assumptions!

It happens on shutdown, for instance. There are many more cases
in PythonWin btw, otherwise I would have changed this.
Here the context when it happens in standard python:

tupledealloc() line 183 + 3 bytes
class_dealloc() line 142 + 28 bytes
finierrors() line 2830 + 18 bytes
Py_Finalize() line 250
Py_Main() line 306
PYTHON! 0040100f()
0012fb28()

tstate is null in this case. I checked it with this debug stuff
in tupleobject.c:

"""
/* Methods */

//debug begin
#include "compile.h"
#include "frameobject.h"
//debug end

static void
tupledealloc(op)
	register PyTupleObject *op;
{
	register int i;
//debug begin
	PyThreadState *tstate = PyThreadState_GET();
	if (tstate->frame && !PyFrame_Check(tstate->frame)) {
		int a = 42; /* breakpoint here */
	}
//debug end
	Py_TRASHCAN_SAFE_BEGIN(op)
	if (op->ob_size > 0) {
		i = op->ob_size;
		while (--i >= 0)
			Py_XDECREF(op->ob_item[i]);
#if MAXSAVESIZE > 0
"""

I was guessing that the memory would have been recycled, but
in fact the tstate was simply NULL.

cheers - chris

-- 
Christian Tismer             :^)   <mailto:tismer@appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaunstr. 26                  :    *Starship* http://starship.python.net
14163 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home