◐ Shell
clean mode source ↗

bpo-35368: Add assertions in pymalloc to check GIL by vstinner · Pull Request #10866 · python/cpython

Conversation

@vstinner

Add "assert(PyGILState_Check());" assertion to pymalloc functions
like pymalloc_alloc() to ensure that the GIL is held.

Remove also _PyGILState_check_enabled define: use directly
_PyRuntime.gilstate.check_enabled.

Note: The debug hook already uses _PyMem_DebugCheckGIL() to check
that the GIL is held.

https://bugs.python.org/issue35368

Add "assert(PyGILState_Check());" assertion to pymalloc functions
like pymalloc_alloc() to ensure that the GIL is held.

Remove also _PyGILState_check_enabled define: use directly
_PyRuntime.gilstate.check_enabled.

Note: The debug hook already uses _PyMem_DebugCheckGIL() to check
that the GIL is held.

@vstinner

This change is not really a bugfix. If Python is compiled in debug mode, debug hooks are installed by default and so _PyMem_DebugCheckGIL() is checked by each PyMem_Malloc() and PyObject_Malloc() call.

This change only impacts PYTHONMALLOC=pymalloc case on Python compiled in debug mode.

The assertion is more here as a "documentation" that pymalloc is not thread-safe (currently).

@vstinner

This change is not really a bugfix. If Python is compiled in debug mode, debug hooks are installed by default and so _PyMem_DebugCheckGIL() is checked by each PyMem_Malloc() and PyObject_Malloc() call.

This PR adds an overhead at runtime for little benefit, so I abandon my PR.

Labels