Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Tries raising the event from a different place, and adds id
  • Loading branch information
zooba committed Nov 15, 2022
commit 9d69a0e774708908af03adb3aa65e99c9fd1047d
17 changes: 9 additions & 8 deletions Doc/c-api/init.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1239,23 +1239,24 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
The global interpreter lock need not be held, but may be held if it is
necessary to serialize calls to this function.

.. audit-event:: cpython.PyThreadState_New "" c.PyThreadState_New
.. audit-event:: cpython.PyThreadState_New id c.PyThreadState_New

Raise an auditing event ``cpython.PyThreadState_New`` with no arguments.
The event will be raised from the creating thread, not the new thread.
Raise an auditing event ``cpython.PyThreadState_New`` with Python's thread
id as the argument. The event will be raised from the thread creating the new
``PyThreadState``, which may not be the new thread.


.. c:function:: void PyThreadState_Clear(PyThreadState *tstate)

Reset all information in a thread state object. The global interpreter lock
must be held.

.. audit-event:: cpython.PyThreadState_Clear "" c.PyThreadState_Clear
.. audit-event:: cpython.PyThreadState_Clear id c.PyThreadState_Clear

Raise an auditing event ``cpython.PyThreadState_Clear`` with no arguments.
The event may be raised from a different thread than the one being
cleared. Exceptions raised from a hook will be treated as unraisable and
will not abort the operation.
Raise an auditing event ``cpython.PyThreadState_Clear`` with Python's
thread id as the argument. The event may be raised from a different thread
than the one being cleared. Exceptions raised from a hook will be treated
as unraisable and will not abort the operation.

.. versionchanged:: 3.9
This function now calls the :c:member:`PyThreadState.on_delete` callback.
Expand Down
22 changes: 15 additions & 7 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -821,11 +821,6 @@ new_threadstate(PyInterpreterState *interp)
PyThreadState *tstate;
_PyRuntimeState *runtime = interp->runtime;

PyThreadState *ts = _PyThreadState_GET();
if (ts && _PySys_Audit(ts, "cpython.PyThreadState_New", NULL) < 0) {
return NULL;
}

// We don't need to allocate a thread state for the main interpreter
// (the common case), but doing it later for the other case revealed a
// reentrancy problem (deadlock). So for now we always allocate before
Expand Down Expand Up @@ -883,14 +878,27 @@ PyThreadState_New(PyInterpreterState *interp)
PyThreadState *tstate = new_threadstate(interp);
if (tstate) {
_PyThreadState_SetCurrent(tstate);
if (PySys_Audit("cpython.PyThreadState_New", "K", tstate->id) < 0) {
PyThreadState_Clear(tstate);
_PyThreadState_DeleteCurrent(tstate);
return NULL;
}
}
return tstate;
}

PyThreadState *
_PyThreadState_Prealloc(PyInterpreterState *interp)
{
return new_threadstate(interp);
PyThreadState *tstate = new_threadstate(interp);
if (tstate) {
if (PySys_Audit("cpython.PyThreadState_New", "K", tstate->id) < 0) {
PyThreadState_Clear(tstate);
_PyThreadState_DeleteCurrent(tstate);
return NULL;
}
}
return tstate;
}

// We keep this around for (accidental) stable ABI compatibility.
Expand Down Expand Up @@ -1038,7 +1046,7 @@ _PyInterpreterState_ClearModules(PyInterpreterState *interp)
void
PyThreadState_Clear(PyThreadState *tstate)
{
if (PySys_Audit("cpython.PyThreadState_Clear", NULL) < 0) {
if (PySys_Audit("cpython.PyThreadState_Clear", "K", tstate->id) < 0) {
PyErr_WriteUnraisable(NULL);
}

Expand Down