[issue44441] Malformed PyImport_Inittab after re-initialization

STINNER Victor report at bugs.python.org
Mon Jun 21 19:26:12 EDT 2021


STINNER Victor <vstinner at python.org> added the comment:

inittab-bug.c uses the Python C API in 3 threads:

* Thread A (init_proc) calls Py_InitializeFromConfig()
* Thread B (run_proc) calls Py_RunMain()
* The main thread (main) calls Py_FinalizeEx()

Problem: the thread B (run_proc) doesn't hold the GIL and so I get a fatal error with Python built in debug mode:
------------
$ gcc inittab-bug.c -ggdb -pthread -lpython3.11d -L. -I. -I Include/
$ PYTHONPATH=$PWD/Lib LD_LIBRARY_PATH=$PWD ./a.out
------ Modules: ------
#0 'posix'
#1 'errno'
#2 'pwd'
----------------------
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Run Python Script
Fatal Python error: _PyMem_DebugMalloc: Python memory allocator called without holding the GIL
Python runtime state: initialized

Thread 0x00007f61ce5c1640 (most recent call first):
<no Python frame>
Abandon (core dumped)
------------

If I remove the thread B (comment the comment), I get a similar error in the main thread which calls Py_FinalizeEx().

Please fix your usage of the GIL. For example, you can try to use:

* https://docs.python.org/dev/c-api/init.html#c.PyGILState_Ensure
* https://docs.python.org/dev/c-api/init.html#c.PyGILState_Release

Usually, Python initialization and Python finalization is done in the same thread, but another thread can use the Python C API if it acquires the GIL using PyGILState_Ensure().

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44441>
_______________________________________


More information about the Python-bugs-list mailing list