FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.15] gh-154431: Fix data race in `sys.audithook` (GH-154462) by miss-islington · Pull Request #154477 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .h  (1) .py  (1) .rst  (1) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
1 change: 1 addition & 0 deletions Include/internal/pycore_interp_structs.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,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];
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_free_threading/test_sys.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ def worker(worker_id):
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()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes a data race in free-threading build in :func:`sys.addaudithook`.
1 change: 1 addition & 0 deletions Python/pystate.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,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;
}
Expand Down
26 changes: 18 additions & 8 deletions Python/sysmodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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]
Expand Down
Loading

Back | FazBrowse Home | New Git URL