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
2 changes: 0 additions & 2 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -857,8 +857,6 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
*/
// XXX Make sure we properly deal with problematic finalizers.

Py_CLEAR(interp->audit_hooks);

// gh-140257: Threads have already been cleared, but daemon threads may
// still access eval_breaker atomically via take_gil() right before they
// hang. Use an atomic store to prevent data races during finalization.
Expand Down
29 changes: 18 additions & 11 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,12 @@ should_audit(PyInterpreterState *interp)
if (!interp) {
return 0;
}
// interp->audit_hooks can only ever be NULL very early during initialization
// or very late during finalization.
int interp_has_audit_hooks = (interp->audit_hooks != NULL
&& PyList_GET_SIZE(interp->audit_hooks) > 0);
return (interp->runtime->audit_hooks.head
|| interp->audit_hooks
|| interp_has_audit_hooks
|| PyDTrace_AUDIT_ENABLED());
}

Expand Down Expand Up @@ -306,7 +310,9 @@ sys_audit_tstate(PyThreadState *ts, const char *event,
}

/* Call interpreter hooks */
if (is->audit_hooks) {
PyObject *audit_hooks = is->audit_hooks;
assert(audit_hooks != NULL);
if (PyList_GET_SIZE(audit_hooks) > 0) {
eventName = PyUnicode_FromString(event);
if (!eventName) {
goto exit;
Expand Down Expand Up @@ -447,6 +453,8 @@ _PySys_ClearAuditHooks(PyThreadState *ts)
PyMem_RawFree(e);
e = n;
}

Py_CLEAR(ts->interp->audit_hooks);
}

static void
Expand Down Expand Up @@ -536,15 +544,7 @@ sys_addaudithook_impl(PyObject *module, PyObject *hook)
}

PyInterpreterState *interp = tstate->interp;
if (interp->audit_hooks == NULL) {
interp->audit_hooks = PyList_New(0);
if (interp->audit_hooks == NULL) {
return NULL;
}
/* Avoid having our list of hooks show up in the GC module */
PyObject_GC_UnTrack(interp->audit_hooks);
}

assert(interp->audit_hooks != NULL);
if (PyList_Append(interp->audit_hooks, hook) < 0) {
return NULL;
}
Expand Down Expand Up @@ -4313,6 +4313,13 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p)

PyInterpreterState *interp = tstate->interp;

PyObject *audit_hooks = PyList_New(0);
if (audit_hooks == NULL) {
goto error;
}
_PyObject_GC_UNTRACK(audit_hooks);
interp->audit_hooks = audit_hooks;

PyObject *modules = _PyImport_InitModules(interp);
if (modules == NULL) {
goto error;
Expand Down
Loading