[Python-bugs-list] [ python-Bugs-581210 ] Fatal Python Error when __del__ adds ref

noreply@sourceforge.net noreply@sourceforge.net
Sat, 13 Jul 2002 22:12:40 -0700


Bugs item #581210, was opened at 2002-07-14 00:47
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=581210&group_id=5470

>Category: Type/class unification
Group: Python 2.2.1
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: joshua kobrin (jkobrin)
>Assigned to: Tim Peters (tim_one)
Summary: Fatal Python Error when __del__ adds ref 

Initial Comment:
In python 2.2.1:
If a class derived from object or having a metaclass creates a new reference to itself in __del__, you will get this:

Fatal Python error: GC object already in linked list

Here is a short sample program that will produce the error:

gRef = None

class Foo(object):
  def __del__(self):
    global gRef
    gRef = self
    
x = Foo()
del x


Based on a quick (sorry) look at the code, this seems to be a very simple error. 

On line 302 of Objects/typeobject.c, in the function call_finalizer, there is the following code:

if (--inst->ob_refcnt > 0) {
#ifdef COUNT_ALLOCS
  inst->ob_type->tp_frees--;
#endif
  _PyObject_GC_TRACK(inst);
  return; /* __del__ added a reference; don't delete now */
}

Now, I looked at this a couple weeks ago so I might be rembering wrong(sorry again), but I think this is what I figured:

The call to _PyObject_GC_TRACK is wrong here because the object has not yet been UTRACKED. On line 634 of Objects/classobject.c, there is identical code, but it is correct because UNTRACK  *has* already been called in this case. In other words, it looks like a cut and paste error.




----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2002-07-14 01:12

Message:
Logged In: YES 
user_id=31435

Yes, and this was fixed last week, in current CVS, and on 
the 2.2 maintenance branch (so it will be fixed in 2.2.2 and 
in 2.3).  Your analysis was correct in all respects.  I'm only 
amazed it took so long for anyone to bump into this!  Then 
again, resurrecting objects in __del__ methods is a sick 
thing to do <wink>.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=581210&group_id=5470