[Python-bugs-list] [ python-Bugs-529750 ] Circular reference makes Py_Init crash

noreply@sourceforge.net noreply@sourceforge.net
Sun, 17 Nov 2002 09:54:11 -0800


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

Category: Python Interpreter Core
Group: Python 2.2.2
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Adam M. Fass (afass)
Assigned to: Neil Schemenauer (nascheme)
Summary: Circular reference makes Py_Init crash

Initial Comment:
Call Py_Initialize(), create two objects that 
reference each other, then call Py_Finalize() and 
then Py_Intialize() again.  This crashes Python with 
the following error message:

Fatal Python error: UNREF invalid object

The documentation on Py_Finalize() mentions that 
circular references will cause memory leaks, but it 
does not mention this behavior.

Platform info:
* Windows XP
* Visual C++ 6.0
* Python 2.2

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

#include "Python.h"

int main(int argc, char* argv[])
{
	char *code1 = "class TestClass:\n\ti = 3\nt1 
= TestClass()\nt2 = TestClass()\nt1.t = t2\nt2.t = 
t1";
	Py_Initialize();

	PyRun_SimpleString(code1);

	Py_Finalize();

	Py_Initialize();
	Py_Finalize();
}

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

The string "code1" contains this python code:

class TestClass:
   i = 3
t1 = TestClass()
t2 = TestClass()
t1.t = t2
t2.t = t1

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

>Comment By: Neil Schemenauer (nascheme)
Date: 2002-11-17 17:54

Message:
Logged In: YES 
user_id=35752

Fixed by removing _Py_ResetReferences() as suggested by Guido.

Objects/object.c 2.194
Python/pythonrun.c 2.169
Include/object.h 2.112

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

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-11-07 17:58

Message:
Logged In: YES 
user_id=6380

How about we simply get rid of _Py_ResetReferences()? It's
only a debugging thing, it only makes a difference when you
call Py_Initialize() again, it's *wrong* in that case, so I
see no need to keep it.


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

Comment By: Neil Schemenauer (nascheme)
Date: 2002-11-07 16:52

Message:
Logged In: YES 
user_id=35752

A simpler bit of code to trigger the bug:

Py_Initialize();
PyRun_SimpleString("x = []; x.append(x)\n");
Py_Finalize();
Py_Initialize();
Py_Finalize();

Py_DEBUG must be defined.  I'm pretty sure the problem is
caused by _Py_ResetReferences.  It invalidates the invariant
that _Py_ForgetReference is checking.  The invariant check
fails because the list lives across Py_Finalize and is freed
the next time the GC is called.

The correct fix is unclear to me.  Maybe Guido has an idea.

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

Comment By: Neal Norwitz (nnorwitz)
Date: 2002-11-03 03:21

Message:
Logged In: YES 
user_id=33168

I just tested w/a debug build in 2.3 and it crashed on me. 
It's probably a problem in 2.2.2.  Neil, could you try to
look at this?  I removed the second Py_Finalize() and it
still crashed.

(gdb) p *op
$4 = {_ob_next = 0x4020a7b4, _ob_prev = 0x8124418, ob_refcnt
= 0, 
  ob_type = 0x8121140}
Note: ob_refcnt == 0

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

Comment By: Neal Norwitz (nnorwitz)
Date: 2002-09-14 00:51

Message:
Logged In: YES 
user_id=33168

I just tried the sample code on 2.3.0 and 2.2.1+ on Linux. 
This didn't crash or misbehave at all.  Did you compile
python or did you get a binary distribution?  Could there be
an incompatibility?  Can you otherwise use python w/o
problems?  Could it be specific to your box or windows in
general?  Can you build python -with-pydebug?  Can you test
with the python versions in CVS 2.2.1+ or 2.3?

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

Comment By: Adam M. Fass (afass)
Date: 2002-09-10 18:32

Message:
Logged In: YES 
user_id=485533

I just tried my code with 2.2.1 and got the same exact result.  
My platform is still the same: Windows XP and Visual C++ 
6.0.

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

Comment By: Neal Norwitz (nnorwitz)
Date: 2002-09-06 22:54

Message:
Logged In: YES 
user_id=33168

Adam, do you still have this problem, with 2.2.1+?

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

Comment By: Neil Schemenauer (nascheme)
Date: 2002-03-22 18:44

Message:
Logged In: YES 
user_id=35752

I can't reproduce this on Linux with the latest CVS code.
I tried with and without Py_DEBUG defined.

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

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