Author vstinner
Recipients kryheb, vstinner
Date 2021-06-21.23:26:12
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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/
------ 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:


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().
Date User Action Args
2021-06-21 23:26:13vstinnersetrecipients: + vstinner, kryheb
2021-06-21 23:26:12vstinnersetmessageid: <>
2021-06-21 23:26:12vstinnerlinkissue44441 messages
2021-06-21 23:26:12vstinnercreate