Uncategorized

c++ – Making two Python independent sub-interpreters import a module with Python/C API leads to memory corruption


In a C++ code, I manage to run multiple subinterpreters in different std::threads using Py_NewInterpreterFromConfig with the following configuration:

    PyInterpreterConfig config = {
        .use_main_obmalloc = 0,
        .allow_fork = 0,
        .allow_exec = 0,
        .allow_threads = 0,
        .allow_daemon_threads = 0,
        .check_multi_interp_extensions = 1,
        .gil = PyInterpreterConfig_OWN_GIL,
    };

When I try to import a module with at least two threads, they trigger a memory corruption error. I suppose this is because the module dictionary is not separated among sub-interpreters (as the documentation itself points out, even if sys.modules should be isolated).

I tried both using Import, ImportModule and compiling bytecode directly.

Since they have no reference to the main GIL (do they?) how are they supposed to be able to import modules and execute independently?



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *