This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Circular reference makes Py_Init crash
Type: Stage:
Components: Interpreter Core Versions: Python 2.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: nascheme Nosy List: afass, gvanrossum, nascheme, nnorwitz
Priority: normal Keywords:

Created on 2002-03-14 04:52 by afass, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Messages (12)
msg9693 - (view) Author: Adam M. Fass (afass) Date: 2002-03-14 04:52
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
msg9694 - (view) Author: Neil Schemenauer (nascheme) * (Python committer) Date: 2002-03-22 18:44
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.
msg9695 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2002-09-06 22:54
Logged In: YES 
user_id=33168

Adam, do you still have this problem, with 2.2.1+?
msg9696 - (view) Author: Adam M. Fass (afass) Date: 2002-09-10 18:32
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.
msg9697 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2002-09-14 00:51
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?
msg9698 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2002-11-03 03:21
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
msg9699 - (view) Author: Neil Schemenauer (nascheme) * (Python committer) Date: 2002-11-07 16:52
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.
msg9700 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2002-11-07 17:58
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.
msg9701 - (view) Author: Neil Schemenauer (nascheme) * (Python committer) Date: 2002-11-17 17:54
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
msg9702 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2002-11-18 13:27
Logged In: YES 
user_id=33168

Neil, this seems like it should be backported, do you agree?
 I suppose if so, we should leave the API in, but make it a
no-op.  I can fix it if you want.
msg9703 - (view) Author: Neil Schemenauer (nascheme) * (Python committer) Date: 2002-11-18 15:28
Logged In: YES 
user_id=35752

Yes please.  Thanks.
msg9704 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2002-11-20 02:38
Logged In: YES 
user_id=33168

Checked in bacport as:
 * Python/pythonrun.c 2.153.6.4
 * Objects/object.c 2.162.6.7
History
Date User Action Args
2022-04-10 16:05:06adminsetgithub: 36257
2002-03-14 04:52:34afasscreate