]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-154431: Fix data race in `sys.audithook` (#154462)
authorsobolevn <mail@sobolevn.me>
Wed, 22 Jul 2026 12:09:20 +0000 (15:09 +0300)
committerGitHub <noreply@github.com>
Wed, 22 Jul 2026 12:09:20 +0000 (14:09 +0200)
Include/internal/pycore_interp_structs.h
Lib/test/test_free_threading/test_sys.py
Misc/NEWS.d/next/Library/2026-07-22-12-42-53.gh-issue-154431.U2kXXZ.rst [new file with mode: 0644]
Python/pystate.c
Python/sysmodule.c

index 0623adce693d4650a33f0f00d9972bc972ad3016..3d577ff717cc6e1c980ec4cbb518bc06fe3fbc1b 100644 (file)
@@ -977,6 +977,7 @@ struct _is {
     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];
index 271fdd13c62b668eed7cacff356e366ff64b9bef..b8ba933cb01adc0313ec53337ed5a3289582e1dd 100644 (file)
@@ -44,6 +44,20 @@ class SysModuleTest(unittest.TestCase):
         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()
diff --git a/Misc/NEWS.d/next/Library/2026-07-22-12-42-53.gh-issue-154431.U2kXXZ.rst b/Misc/NEWS.d/next/Library/2026-07-22-12-42-53.gh-issue-154431.U2kXXZ.rst
new file mode 100644 (file)
index 0000000..a56c3b3
--- /dev/null
@@ -0,0 +1 @@
+Fixes a data race in free-threading build in :func:`sys.addaudithook`.
index d10b38def32911d2bab0e78209fe2ec753fa004b..b6c34552e94337bb65d6b4a3a11eeb1670bd1391 100644 (file)
@@ -578,6 +578,7 @@ init_interpreter(PyInterpreterState *interp,
     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;
     }
index 9442472b53abbe1d8d5659df107462c871cb98c2..207ccbce41d71db246af2a355b04bc6c608c2f9d 100644 (file)
@@ -236,7 +236,7 @@ should_audit(PyInterpreterState *interp)
         return 0;
     }
     return (interp->runtime->audit_hooks.head
-            || interp->audit_hooks
+            || FT_ATOMIC_LOAD_PTR_ACQUIRE(interp->audit_hooks)
             || PyDTrace_AUDIT_ENABLED());
 }
 
@@ -306,13 +306,14 @@ sys_audit_tstate(PyThreadState *ts, const char *event,
     }
 
     /* 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;
         }
@@ -536,20 +537,29 @@ sys_addaudithook_impl(PyObject *module, PyObject *hook)
     }
 
     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]