[issue6531] atexit_callfuncs() crashing within Py_Finalize() when using multiple interpreters.

Graham Dumpleton report at bugs.python.org
Tue Jul 21 13:19:29 CEST 2009


New submission from Graham Dumpleton <Graham.Dumpleton at gmail.com>:

I am seeing a crash within Py_Finalize() with Python 3.0 in mod_wsgi. It looks like the 
patches for issue-4200 were not adequate and that this wasn't picked up at the time.

This new problem I am seeing looks like it may be linked to where the 'atexit' module is 
initialised/imported in a sub interpreter but never imported in the main interpreter. I can 
avoid the crash by having:

    PyImport_ImportModule("atexit");

    Py_Finalize();

At a guess, the problem is because in atexit_callfuncs():

    module = PyState_FindModule(&atexitmodule);
    if (module == NULL)
        return;

still returns a module for case where imported in a sub interpreter but not in main 
interpreter, so doesn't return, but then code which follows:

    modstate = GET_ATEXIT_STATE(module);

    if (modstate->ncallbacks == 0)
        return;

returns NULL for modstate for the main interpreter as PyInit_atexit() had never been called 
for the main interpreter as the 'atexit' module was never imported within that interpreter.

The fix would appear to be to check modstate for being NULL and return. Ie.,

    module = PyState_FindModule(&atexitmodule);
    if (module == NULL)
        return;
    modstate = GET_ATEXIT_STATE(module);

    if (modstate == NULL)
        return;

    if (modstate->ncallbacks == 0)
        return;

The only thing I am uncertain about is why PyState_FindModule() would return an object. I 
cant find any documentation about that function so not entirely sure what it is meant to do. 
I would have thought it would be returning data specific to the interpreter, but if never 
imported in that interpreter, why would there still be an object recorded.

BTW, I have marked this as for Python 3.1 as well, but haven't tested it on that. The code in 
'atexit' module doesn't appear to have changed though so assuming it will die there as well.

For now am using the workaround in mod_wsgi.

----------
components: Interpreter Core
messages: 90753
nosy: grahamd
severity: normal
status: open
title: atexit_callfuncs() crashing within Py_Finalize() when using multiple interpreters.
type: crash
versions: Python 3.0, Python 3.1

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue6531>
_______________________________________


More information about the Python-bugs-list mailing list