'Python C API Py_NewInterpreter() freezes thread when creating new sys.stdin

I am building a Python wrapper for a C++ SDK in Python

The app I am building on top of is using a console that is always listening to input on stdin

Everything worked great before I tried to implement Python subinterpreters, when I try to create a new one with Py_NewInterpreter() the whole thread freezes until some input is entered into the console, then it works as expected.

After lots of debugging I found out that the issue is in Python's internal function copying stdin stream when calling the dup() function, this freezes the whole thread until it can copy stdin (so after the user enters some input)

This issue is only on Windows, on Linux it works as expected.

Here is the full code of the function which creates and starts the interpreter

bool PythonResource::Start() {
    PyThreadState_Swap(Runtime->GetInterpreter());
    Interpreter = Py_NewInterpreter(); // Here it freezes

    FILE* fp = fopen(FilePath.c_str(), "r");
    bool crashed = PyRun_SimpleFile(fp, FilePath.c_str());

    PyThreadState_Swap(Runtime->GetInterpreter());
    return !crashed;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source