gh-137400: Fix thread-safety issues when profiling all threads (gh-137518)
There were a few thread-safety issues when profiling or tracing all threads via PyEval_SetProfileAllThreads or PyEval_SetTraceAllThreads: * The loop over thread states could crash if a thread exits concurrently (in both the free threading and default build) * The modification of `c_profilefunc` and `c_tracefunc` wasn't thread-safe on the free threading build.
This commit is contained in:
16
Python/generated_cases.c.h
generated
16
Python/generated_cases.c.h
generated
@@ -7334,7 +7334,13 @@
|
||||
}
|
||||
// _MAYBE_INSTRUMENT
|
||||
{
|
||||
if (tstate->tracing == 0) {
|
||||
#ifdef Py_GIL_DISABLED
|
||||
|
||||
int check_instrumentation = 1;
|
||||
#else
|
||||
int check_instrumentation = (tstate->tracing == 0);
|
||||
#endif
|
||||
if (check_instrumentation) {
|
||||
uintptr_t global_version = _Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker) & ~_PY_EVAL_EVENTS_MASK;
|
||||
uintptr_t code_version = FT_ATOMIC_LOAD_UINTPTR_ACQUIRE(_PyFrame_GetCode(frame)->_co_instrumentation_version);
|
||||
if (code_version != global_version) {
|
||||
@@ -10245,7 +10251,13 @@
|
||||
}
|
||||
// _MAYBE_INSTRUMENT
|
||||
{
|
||||
if (tstate->tracing == 0) {
|
||||
#ifdef Py_GIL_DISABLED
|
||||
|
||||
int check_instrumentation = 1;
|
||||
#else
|
||||
int check_instrumentation = (tstate->tracing == 0);
|
||||
#endif
|
||||
if (check_instrumentation) {
|
||||
uintptr_t global_version = _Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker) & ~_PY_EVAL_EVENTS_MASK;
|
||||
uintptr_t code_version = FT_ATOMIC_LOAD_UINTPTR_ACQUIRE(_PyFrame_GetCode(frame)->_co_instrumentation_version);
|
||||
if (code_version != global_version) {
|
||||
|
||||
Reference in New Issue
Block a user