Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.12: Py_FinalizeEx: the function must be called with the GIL held (_PyObject_DebugMallocStats) #107848

Open
polyvertex opened this issue Aug 10, 2023 · 2 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@polyvertex
Copy link

polyvertex commented Aug 10, 2023

I am embedding Python 3.12.0rc1 in a C++ application on Windows.

With the interpreter compiled in DEBUG mode and running a debugging session with MSVC, I get the following error message right before reaching a breakpoint: Fatal Python error: the function must be called with the GIL held, after Python initialization and before Python finalization, but the GIL is released (the current Python thread state is NULL)

The breakpoint is reached with the following call stack:

<breakpoint>
python312_d.dll!fatal_error_exit(int status) Line 2707
python312_d.dll!fatal_error(int fd, int header, const char * prefix, const char * msg, int status) Line 2889
python312_d.dll!_Py_FatalErrorFunc(const char * func, const char * msg) Line 2905
python312_d.dll!_PyInterpreterState_GET() Line 118
python312_d.dll!_PyObject_DebugMallocStats(_iobuf * out) Line 2551
python312_d.dll!Py_FinalizeEx() Line 1977
<my app calling Py_FinalizeEx() from the main thread and with the GIL acquired>

My application configures the interpreter in so-called isolated mode and with the flag PyConfig.malloc_stats set to 1 (among other ones).

However if I set malloc_stats to 0, the breakpoint is never reached, the interpreter finalizes gracefully and the application terminates with no issue.

To narrow down the issue, I removed all the code between Python initialization and finalization. No builtin module is registered by the application itself, and it does not run nor import any Python module.

This issue might be related to #105690?

Please let me know if any extra information is required!

@polyvertex polyvertex added the type-bug An unexpected behavior, bug, or error label Aug 10, 2023
@ronaldoussoren ronaldoussoren added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Aug 10, 2023
@polyvertex
Copy link
Author

Update: same issue also observed with the current head of branch 3.12 (256586a) and branch main for v3.13 (fc23f34)

@zia-badar
Copy link

zia-badar commented Oct 15, 2023

I was also facing the same issue using embedded python with python 3.12, switching to 3.9 fixed the issue. I got the solution from here

please can someone update the documentation here

fyi: this only happens in debug mode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants