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
1 change: 0 additions & 1 deletion Include/internal/pycore_interp_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,6 @@ 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
1 change: 0 additions & 1 deletion Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,6 @@ 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
2 changes: 1 addition & 1 deletion Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ sys_addaudithook_impl(PyObject *module, PyObject *hook)
}

PyInterpreterState *interp = tstate->interp;
PyMutex mutex = interp->audit_hooks_mutex;
PyMutex mutex = interp->runtime->audit_hooks.mutex;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi, I noticed the mutex here is copied by value onto the stack, then being locked. I'm worried this subtly changes the semantics into locking a stack-local object instead. The typical usage may be to lock the address directly, i.e. PyMutex_Lock(&interp->runtime->audit_hooks.mutex);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Here is a demo shows locking a stack-local mutex won't affect the real mutex.

#include <Python.h>
#include <stdio.h>

static PyMutex shared = {0};

int main(void)
{
    PyMutex mutex = shared;
    PyMutex_Lock(&mutex);
    printf("locked the COPY:  shared=%d copy=%d\n", PyMutex_IsLocked(&shared), PyMutex_IsLocked(&mutex));
    PyMutex_Unlock(&mutex);

    PyMutex_Lock(&shared);
    printf("locked the FIELD: shared=%d\n", PyMutex_IsLocked(&shared));
    PyMutex_Unlock(&shared);
    return 0;
}

Run gcc mutex_copy_demo.c -I Include -I . libpython3.16.a -lpthread -lm -ldl -lutil -o demo && ./demo then it print

locked the COPY:  shared=0 copy=1
locked the FIELD: shared=1

PyMutex_Lock(&mutex);

if (interp->audit_hooks == NULL) {
Expand Down
Loading