Skip to content

Commit 528dee0

Browse files
Revert "gh-99377: Add audit events for thread creation and clear (GH-99378)"
This reverts commit 19c1462.
1 parent 9db1e17 commit 528dee0

File tree

7 files changed

+7
-117
lines changed

7 files changed

+7
-117
lines changed

Doc/c-api/init.rst

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,25 +1239,12 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
12391239
The global interpreter lock need not be held, but may be held if it is
12401240
necessary to serialize calls to this function.
12411241
1242-
.. audit-event:: cpython.PyThreadState_New id c.PyThreadState_New
1243-
1244-
Raise an auditing event ``cpython.PyThreadState_New`` with Python's thread
1245-
id as the argument. The event will be raised from the thread creating the new
1246-
``PyThreadState``, which may not be the new thread.
1247-
12481242
12491243
.. c:function:: void PyThreadState_Clear(PyThreadState *tstate)
12501244
12511245
Reset all information in a thread state object. The global interpreter lock
12521246
must be held.
12531247
1254-
.. audit-event:: cpython.PyThreadState_Clear id c.PyThreadState_Clear
1255-
1256-
Raise an auditing event ``cpython.PyThreadState_Clear`` with Python's
1257-
thread id as the argument. The event may be raised from a different thread
1258-
than the one being cleared. Exceptions raised from a hook will be treated
1259-
as unraisable and will not abort the operation.
1260-
12611248
.. versionchanged:: 3.9
12621249
This function now calls the :c:member:`PyThreadState.on_delete` callback.
12631250
Previously, that happened in :c:func:`PyThreadState_Delete`.

Doc/library/_thread.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ This module defines the following constants and functions:
5757
When the function raises a :exc:`SystemExit` exception, it is silently
5858
ignored.
5959

60-
.. audit-event:: _thread.start_new_thread function,args,kwargs start_new_thread
61-
6260
.. versionchanged:: 3.8
6361
:func:`sys.unraisablehook` is now used to handle unhandled exceptions.
6462

Lib/test/audit-tests.py

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -419,48 +419,6 @@ def hook(event, args):
419419
sys._getframe()
420420

421421

422-
def test_threading():
423-
import _thread
424-
425-
def hook(event, args):
426-
if event.startswith(("_thread.", "cpython.PyThreadState", "test.")):
427-
print(event, args)
428-
429-
sys.addaudithook(hook)
430-
431-
lock = _thread.allocate_lock()
432-
lock.acquire()
433-
434-
class test_func:
435-
def __repr__(self): return "<test_func>"
436-
def __call__(self):
437-
sys.audit("test.test_func")
438-
lock.release()
439-
440-
i = _thread.start_new_thread(test_func(), ())
441-
lock.acquire()
442-
443-
444-
def test_threading_abort():
445-
# Ensures that aborting PyThreadState_New raises the correct exception
446-
import _thread
447-
448-
class ThreadNewAbortError(Exception):
449-
pass
450-
451-
def hook(event, args):
452-
if event == "cpython.PyThreadState_New":
453-
raise ThreadNewAbortError()
454-
455-
sys.addaudithook(hook)
456-
457-
try:
458-
_thread.start_new_thread(lambda: None, ())
459-
except ThreadNewAbortError:
460-
# Other exceptions are raised and the test will fail
461-
pass
462-
463-
464422
def test_wmi_exec_query():
465423
import _wmi
466424

Lib/test/test_audit.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -186,31 +186,6 @@ def test_sys_getframe(self):
186186

187187
self.assertEqual(actual, expected)
188188

189-
190-
def test_threading(self):
191-
returncode, events, stderr = self.run_python("test_threading")
192-
if returncode:
193-
self.fail(stderr)
194-
195-
if support.verbose:
196-
print(*events, sep='\n')
197-
actual = [(ev[0], ev[2]) for ev in events]
198-
expected = [
199-
("_thread.start_new_thread", "(<test_func>, (), None)"),
200-
("cpython.PyThreadState_New", "(2,)"),
201-
("test.test_func", "()"),
202-
("cpython.PyThreadState_Clear", "(2,)"),
203-
]
204-
205-
self.assertEqual(actual, expected)
206-
207-
def test_threading_abort(self):
208-
# Ensures that aborting PyThreadState_New raises the correct exception
209-
returncode, events, stderr = self.run_python("test_threading_abort")
210-
if returncode:
211-
self.fail(stderr)
212-
213-
214189
def test_wmi_exec_query(self):
215190
import_helper.import_module("_wmi")
216191
returncode, events, stderr = self.run_python("test_wmi_exec_query")

Misc/NEWS.d/next/Core and Builtins/2022-11-11-14-04-01.gh-issue-99377.-CJvWn.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.

Modules/_threadmodule.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,11 +1145,6 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
11451145
return NULL;
11461146
}
11471147

1148-
if (PySys_Audit("_thread.start_new_thread", "OOO",
1149-
func, args, kwargs ? kwargs : Py_None) < 0) {
1150-
return NULL;
1151-
}
1152-
11531148
PyInterpreterState *interp = _PyInterpreterState_GET();
11541149
if (!_PyInterpreterState_HasFeature(interp, Py_RTFLAGS_THREADS)) {
11551150
PyErr_SetString(PyExc_RuntimeError,
@@ -1165,10 +1160,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
11651160
boot->tstate = _PyThreadState_Prealloc(boot->interp);
11661161
if (boot->tstate == NULL) {
11671162
PyMem_Free(boot);
1168-
if (!PyErr_Occurred()) {
1169-
return PyErr_NoMemory();
1170-
}
1171-
return NULL;
1163+
return PyErr_NoMemory();
11721164
}
11731165
boot->runtime = runtime;
11741166
boot->func = Py_NewRef(func);

Python/pystate.c

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -873,29 +873,14 @@ PyThreadState *
873873
PyThreadState_New(PyInterpreterState *interp)
874874
{
875875
PyThreadState *tstate = new_threadstate(interp);
876-
if (tstate) {
877-
_PyThreadState_SetCurrent(tstate);
878-
if (PySys_Audit("cpython.PyThreadState_New", "K", tstate->id) < 0) {
879-
PyThreadState_Clear(tstate);
880-
_PyThreadState_DeleteCurrent(tstate);
881-
return NULL;
882-
}
883-
}
876+
_PyThreadState_SetCurrent(tstate);
884877
return tstate;
885878
}
886879

887880
PyThreadState *
888881
_PyThreadState_Prealloc(PyInterpreterState *interp)
889882
{
890-
PyThreadState *tstate = new_threadstate(interp);
891-
if (tstate) {
892-
if (PySys_Audit("cpython.PyThreadState_New", "K", tstate->id) < 0) {
893-
PyThreadState_Clear(tstate);
894-
_PyThreadState_Delete(tstate, 0);
895-
return NULL;
896-
}
897-
}
898-
return tstate;
883+
return new_threadstate(interp);
899884
}
900885

901886
// We keep this around for (accidental) stable ABI compatibility.
@@ -1043,10 +1028,6 @@ _PyInterpreterState_ClearModules(PyInterpreterState *interp)
10431028
void
10441029
PyThreadState_Clear(PyThreadState *tstate)
10451030
{
1046-
if (PySys_Audit("cpython.PyThreadState_Clear", "K", tstate->id) < 0) {
1047-
PyErr_WriteUnraisable(NULL);
1048-
}
1049-
10501031
int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose;
10511032

10521033
if (verbose && tstate->cframe->current_frame != NULL) {
@@ -1564,16 +1545,16 @@ _PyGILState_Init(_PyRuntimeState *runtime)
15641545
PyStatus
15651546
_PyGILState_SetTstate(PyThreadState *tstate)
15661547
{
1567-
/* must init with valid states */
1568-
assert(tstate != NULL);
1569-
assert(tstate->interp != NULL);
1570-
15711548
if (!_Py_IsMainInterpreter(tstate->interp)) {
15721549
/* Currently, PyGILState is shared by all interpreters. The main
15731550
* interpreter is responsible to initialize it. */
15741551
return _PyStatus_OK();
15751552
}
15761553

1554+
/* must init with valid states */
1555+
assert(tstate != NULL);
1556+
assert(tstate->interp != NULL);
1557+
15771558
struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
15781559

15791560
gilstate->autoInterpreterState = tstate->interp;

0 commit comments

Comments
 (0)