Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash at exit when an audit hook was added with :c:func:`PySys_AddAuditHook`
before :c:func:`Py_Initialize` and the raw memory allocator changed during startup, for
example with ``PYTHONMALLOC=mimalloc`` or in the :ref:`Python Development Mode <devmode>`.
8 changes: 6 additions & 2 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ _PySys_ClearAuditHooks(PyThreadState *ts)
runtime->audit_hooks.head = NULL;
while (e) {
n = e->next;
PyMem_RawFree(e);
free(e);
e = n;
}
}
Expand Down Expand Up @@ -496,7 +496,11 @@ PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData)
}
}

_Py_AuditHookEntry *e = (_Py_AuditHookEntry*)PyMem_RawMalloc(
/* Use malloc(), not PyMem_RawMalloc(): a hook can be added before

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I don't think that it's worth it to bother with _PyMem_RawMalloc() wrapper (and pass NULL as context). I would prefer to use malloc() / free() directly.

The "new" PyInitConfig C API also uses directly malloc() / free() to avoid troubles with changing the memory allocator.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in e5d45db, thanks: plain malloc() / free() now, like PyInitConfig.

Py_Initialize(), and the entry is freed during finalization, after
PYTHONMALLOC may have changed the raw allocator (and, in debug builds,
the allocator under the debug hooks). */
_Py_AuditHookEntry *e = (_Py_AuditHookEntry*)malloc(
sizeof(_Py_AuditHookEntry));
if (!e) {
if (tstate != NULL) {
Expand Down
Loading