diff options
author | Steve Dower <steve.dower@python.org> | 2022-11-16 17:15:52 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-16 17:15:52 (GMT) |
commit | 19c1462e8dca3319c8290e2edcce482bd18cb018 (patch) | |
tree | 01d4d3189f912b0444faf55a44e34057e8dc5afb /Python/pystate.c | |
parent | 01fa907aa8e7c475a76b407f35c635b26c9f47f8 (diff) | |
download | cpython-19c1462e8dca3319c8290e2edcce482bd18cb018.zip cpython-19c1462e8dca3319c8290e2edcce482bd18cb018.tar.gz cpython-19c1462e8dca3319c8290e2edcce482bd18cb018.tar.bz2 |
gh-99377: Add audit events for thread creation and clear (GH-99378)
Diffstat (limited to 'Python/pystate.c')
-rw-r--r-- | Python/pystate.c | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/Python/pystate.c b/Python/pystate.c index b94fbf6..d6f2645 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -873,14 +873,29 @@ PyThreadState * PyThreadState_New(PyInterpreterState *interp) { PyThreadState *tstate = new_threadstate(interp); - _PyThreadState_SetCurrent(tstate); + 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_Delete(tstate, 0); + return NULL; + } + } + return tstate; } // We keep this around for (accidental) stable ABI compatibility. @@ -1028,6 +1043,10 @@ _PyInterpreterState_ClearModules(PyInterpreterState *interp) void PyThreadState_Clear(PyThreadState *tstate) { + if (PySys_Audit("cpython.PyThreadState_Clear", "K", tstate->id) < 0) { + PyErr_WriteUnraisable(NULL); + } + int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose; if (verbose && tstate->cframe->current_frame != NULL) { @@ -1545,16 +1564,16 @@ _PyGILState_Init(_PyRuntimeState *runtime) PyStatus _PyGILState_SetTstate(PyThreadState *tstate) { + /* must init with valid states */ + assert(tstate != NULL); + assert(tstate->interp != NULL); + if (!_Py_IsMainInterpreter(tstate->interp)) { /* Currently, PyGILState is shared by all interpreters. The main * interpreter is responsible to initialize it. */ return _PyStatus_OK(); } - /* must init with valid states */ - assert(tstate != NULL); - assert(tstate->interp != NULL); - struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate; gilstate->autoInterpreterState = tstate->interp; |