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.

Author vstinner
Recipients kryheb, vstinner
Date 2021-06-21.23:26:12
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1624317972.97.0.540986571164.issue44441@roundup.psfhosted.org>
In-reply-to
Content
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().
History
Date User Action Args
2021-06-21 23:26:13vstinnersetrecipients: + vstinner, kryheb
2021-06-21 23:26:12vstinnersetmessageid: <1624317972.97.0.540986571164.issue44441@roundup.psfhosted.org>
2021-06-21 23:26:12vstinnerlinkissue44441 messages
2021-06-21 23:26:12vstinnercreate