Malformed PyImport_Inittab after re-initialization #88607
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
assignee = None closed_at = <Date 2021-06-23.15:48:43.362> created_at = <Date 2021-06-17.10:14:12.876> labels = ['expert-C-API', 'library', '3.9', '3.10', '3.11'] title = 'Malformed PyImport_Inittab after re-initialization' updated_at = <Date 2021-06-24.08:48:55.234> user = 'https://github.com/kryheb'
activity = <Date 2021-06-24.08:48:55.234> actor = 'kryheb' assignee = 'none' closed = True closed_date = <Date 2021-06-23.15:48:43.362> closer = 'vstinner' components = ['Library (Lib)', 'C API'] creation = <Date 2021-06-17.10:14:12.876> creator = 'kryheb' dependencies =  files = ['50115', '50125'] hgrepos =  issue_num = 44441 keywords = ['patch'] message_count = 10.0 messages = ['395985', '396300', '396385', '396404', '396405', '396414', '396427', '396428', '396429', '396462'] nosy_count = 2.0 nosy_names = ['vstinner', 'kryheb'] pr_nums = ['26767', '26874', '26877', '26878'] priority = 'normal' resolution = 'fixed' stage = 'resolved' status = 'closed' superseder = None type = None url = 'https://bugs.python.org/issue44441' versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']
The text was updated successfully, but these errors were encountered:
I observed misbehavior trying to embed the Python interpreter into a C app.
Steps to reproduce:
Observed behavior: The script is executed at first iteration, but re-initialization fails with an error: " Traceback (most recent call last): File "<frozen importlib._bootstrap>", line 1187, in _install_external_importers ModuleNotFoundError: No module named 'posix'
Error: external importer setup failed
Head of modules list at fist run:
An issue discovered on:
Issue still exists on the latest main and on the rc 3.10
inittab-bug.c uses the Python C API in 3 threads:
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:
Thread 0x00007f61ce5c1640 (most recent call first):
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().
I apologize for the inaccurate code sample.
Steps to reproduce:
gdb) r Starting program: /home/kheb/proj/tmp/pyc/a.out [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib64/libthread_db.so.1". Run Python Script >>> This is the python script Python Script completed (0)
Program received signal SIGSEGV, Segmentation fault.
I hope this example helps.