struct _obmalloc_state *obmalloc;
PyObject *audit_hooks;
+ PyMutex audit_hooks_mutex;
PyType_WatchCallback type_watchers[TYPE_MAX_WATCHERS];
PyCode_WatchCallback code_watchers[CODE_MAX_WATCHERS];
PyContext_WatchCallback context_watchers[CONTEXT_MAX_WATCHERS];
workers = [lambda: worker(i) for i in range(5)]
threading_helper.run_concurrently(workers)
+ def test_sys_audit_hooks(self):
+ def _hook(*args):
+ return None
+
+ def adder():
+ for _ in range(100):
+ sys.addaudithook(_hook)
+
+ def auditor():
+ for _ in range(2000):
+ sys.audit("fusil.tsan.test")
+
+ threading_helper.run_concurrently([adder, auditor])
+
if __name__ == "__main__":
unittest.main()
llist_init(&interp->mem_free_queue.head);
llist_init(&interp->asyncio_tasks_head);
interp->asyncio_tasks_lock = (PyMutex){0};
+ interp->audit_hooks_mutex = (PyMutex){0};
for (int i = 0; i < _PY_MONITORING_UNGROUPED_EVENTS; i++) {
interp->monitors.tools[i] = 0;
}
return 0;
}
return (interp->runtime->audit_hooks.head
- || interp->audit_hooks
+ || FT_ATOMIC_LOAD_PTR_ACQUIRE(interp->audit_hooks)
|| PyDTrace_AUDIT_ENABLED());
}
}
/* Call interpreter hooks */
- if (is->audit_hooks) {
+ PyObject *audit_hooks = FT_ATOMIC_LOAD_PTR_ACQUIRE(is->audit_hooks);
+ if (audit_hooks) {
eventName = PyUnicode_FromString(event);
if (!eventName) {
goto exit;
}
- hooks = PyObject_GetIter(is->audit_hooks);
+ hooks = PyObject_GetIter(audit_hooks);
if (!hooks) {
goto exit;
}
}
PyInterpreterState *interp = tstate->interp;
+ PyMutex mutex = interp->audit_hooks_mutex;
+ PyMutex_Lock(&mutex);
+
if (interp->audit_hooks == NULL) {
- interp->audit_hooks = PyList_New(0);
- if (interp->audit_hooks == NULL) {
- return NULL;
+ PyObject *new_list = PyList_New(0);
+ if (new_list == NULL) {
+ goto error;
}
/* Avoid having our list of hooks show up in the GC module */
- PyObject_GC_UnTrack(interp->audit_hooks);
+ PyObject_GC_UnTrack(new_list);
+ FT_ATOMIC_STORE_PTR_RELEASE(interp->audit_hooks, new_list);
}
if (PyList_Append(interp->audit_hooks, hook) < 0) {
- return NULL;
+ goto error;
}
+ PyMutex_Unlock(&mutex);
Py_RETURN_NONE;
+
+error:
+ PyMutex_Unlock(&mutex);
+ return NULL;
}
/*[clinic input]