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: crash in gcmodule.c on python reinitialization
Type: crash Stage: resolved
Components: Interpreter Core, Windows Versions: Python 2.6, Python 2.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: nascheme Nosy List: BreamoreBoy, Rhamphoryncus, amiseler, ita, jhylton, mwh, nascheme, ncoghlan, pitrou, terry.reedy
Priority: normal Keywords:

Created on 2005-06-22 14:43 by amiseler, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (13)
msg25610 - (view) Author: Alexander Miseler (amiseler) Date: 2005-06-22 14:43
i have a c++ windows application with embedded python
interpreter.

this code crashs:

Py_Initialize();
PyRun_SimpleString("import gc");
Py_Finalize();
Py_Initialize();
PyRun_SimpleString("import gc");	// <--- BOOM


the problem seems to be the global var "garbage" in
gcmodule.c
the var isn't cleared in the reinitialization and
becomes an invalid pointer.
setting it to NULL in Py_Finalize fixes the crash.
i use python version 2.3.4 but a short look at the
2.4.1 source indicates that the problem is still there.
msg25611 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2005-06-22 15:59
Logged In: YES 
user_id=6656

Bizarrely, I think this got fixed in CVS HEAD just a few days ago.  Are you 
in a position to check that?
msg25612 - (view) Author: Alexander Miseler (amiseler) Date: 2005-06-23 08:11
Logged In: YES 
user_id=693638

yes, Neil Schemenauer (nascheme) added a Py_INCREF(garbage)
to initgc.
that *MIGHT* fix the crash. but only by simply never
releasing the garbage list. the garbage variable is still
never reset to NULL and therefore no new garbage list is
created after reinitialization.
besides: the incref is only in gcinit, but the list can be
created in gcinit OR in handle_finalizers (and while i was
looking at it in the debugger it always was handle_finalizers).

i guess the proper way to fix it would be a cleanup function
for the gcmodule that is called by Py_Finalize. the cleanup
function should do a decref (if Neils incref stays in, i'm
not sure if it is needed) and then reset the global vars.
msg25613 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2005-06-23 08:22
Logged In: YES 
user_id=6656

At this point you might be thinking that Py_NewInterpreter/
Py_EndInterpreter loops are not the best tested things in the world.  You'd 
be right.

Neil's fix *does* fix the crash, but also shares PyObject's between 
interpreter states, which is a bit of a no-no, but otoh, this problem is far 
from unique to gcmodule.c.

> besides: the incref is only in gcinit, but the list can be
> created in gcinit OR in handle_finalizers (and while i was
> looking at it in the debugger it always was handle_finalizers).

That slightly misses the point -- the reason you got a crash was because 
the PyModule_AddObject initgc stole a reference to garbage, so when the 
module got cleaned up, it got deallocated despite there still being a 
reference to it in the module level global.  If initgc is never run, the 
refcount of garbage stays at 1.

> i guess the proper way to fix it would be a cleanup function
> for the gcmodule that is called by Py_Finalize.

Yeah, but quite a lot of modules could do with that, I think.  Do you have 
the energy to work on a 'proper' solution?  I don't.
msg25614 - (view) Author: Neil Schemenauer (nascheme) * (Python committer) Date: 2005-06-23 18:34
Logged In: YES 
user_id=35752

Well, the fact that some things are broken is not a good
excuse to introduce more brokeness.  I guess decrefing the
gc.garbage list and letting any reference cycles contained
in it leak is better than sharing objects between interpreters.
msg57834 - (view) Author: ita (ita) Date: 2007-11-25 23:49
The following still crashes (python 2.5.1):

        for (int i=0; i<1000; ++i)
        {
                Py_Initialize();
                PyRun_SimpleString("import tarfile\n");
                Py_Finalize();
        }

Bindings such as Swig are adding weird hacks just for avoiding the
finalize call and resetting the variables. Not being able to start from
a clean interpreter is unacceptable.
msg58670 - (view) Author: Adam Olsen (Rhamphoryncus) Date: 2007-12-16 05:04
mwh, my threading patch is extensive enough and has enough overlap that
I'm not intimidating by fixing this.  It's low on my list of priorities
though.

So far my tendency is to rip out multiple interpreters, as I haven't
seen what it wants to accomplish.  It's certainly *NOT* completely
isolated interpreters.

As for Py_Finalize, I see 4 purposes to calling it:
1) Finish any lingering bits of the program, such as __del__ methods or
weakref callbacks
2) Delete lingering references so refcount errors or memory leaks may be
more easily discovered
3) Prepare for Py_Initialize to be called again
4) Return the interpreter to a quiescent state such that python.so can
be safely unloaded, then reloaded and Py_Initialize called again

Of these, 3 and 4 are not done.  3 would be easy if Py_Finalize was a
no-op, but that'd undermine 1 and 2.  Given the optimistic
documentation, it's surprising how limited Py_Finalize's scope is.

I've seen some fleeting discussion of allowing extension modules to be
unloaded, which IMO requires returning them to a quiescent state.  Doing
that may lay the foundation for doing it to the entire interpreter.

Anyway, real use cases are critical here.  Although you may think you
want your plugins to unload python, the costs mean you should only do so
very rarely, enough that there's really no advantage to unloading it
(except at program shutdown, for purposes 1 and 2.)
msg214526 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-03-23 00:25
I find it surprising that something flagged up as causing a crash hasn't had a little more TLC, but is this still a genuine problem?
msg214539 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-03-23 02:53
The initial test case was apparently fixed. It is not clear to me that the second, msg57834, is a supported use case. Nick, I know you are busy, but there is no C api category in the experts list. Any comment?

Unless someone can identify a bug in current 2.7 or 3.4+ or propose an enhancement for 3.5, we should close this. gc has been enhanced since 2.4, 2.5, and I would not expect anything said about those versions to apply to 3.4+.
msg214540 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-03-23 02:53
Nick, please read my previous msg if you can find a moment.
msg214549 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2014-03-23 07:12
I don't actually know the internal details of the GC - adding Antoine based on the list of interested devs for the gc module.
msg214593 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-03-23 15:33
Someone interested in this issue should first check whether it still applies to 3.4 or 3.5.
msg232287 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-12-07 23:00
I am closing since the initial problem has been fixed and there is no known current problem to fix.  If someone does discover one, they can reopen or, probably better, open a new issue.
History
Date User Action Args
2022-04-11 14:56:11adminsetgithub: 42110
2014-12-07 23:00:58terry.reedysetstatus: languishing -> closed
resolution: fixed
messages: + msg232287

stage: test needed -> resolved
2014-03-23 15:33:36pitrousetmessages: + msg214593
2014-03-23 07:12:05ncoghlansetnosy: + pitrou
messages: + msg214549
2014-03-23 02:53:42terry.reedysetnosy: + ncoghlan
messages: + msg214540
2014-03-23 02:53:02terry.reedysettype: crash
messages: + msg214539
stage: test needed
2014-03-23 00:25:35BreamoreBoysetnosy: + terry.reedy, BreamoreBoy
messages: + msg214526
components: + Windows
2010-03-01 20:20:29akuchlingsetstatus: open -> languishing
2010-02-21 19:38:57Jeremy.Hyltonsetnosy: + jhylton
2008-01-05 20:41:45christian.heimessetversions: + Python 2.6, Python 2.5
2007-12-16 05:04:06Rhamphoryncussetmessages: + msg58670
2007-11-26 17:20:48Rhamphoryncussetnosy: + Rhamphoryncus
2007-11-25 23:49:36itasetnosy: + ita
messages: + msg57834
2005-06-22 14:43:27amiselercreate