diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-12-23-23-36-41.gh-issue-143123.-51gt_.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-12-23-23-36-41.gh-issue-143123.-51gt_.rst new file mode 100644 index 00000000000000..04523bdb615bfe --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-12-23-23-36-41.gh-issue-143123.-51gt_.rst @@ -0,0 +1 @@ +Protect the JIT against recursive tracing. diff --git a/Python/optimizer.c b/Python/optimizer.c index 0f8ddb4ba558d3..435b39405d6e5b 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -138,6 +138,12 @@ _PyOptimizer_Optimize( // return immediately without optimization. return 0; } + if (_tstate->jit_tracer_state.initial_state.func == NULL) { + // gh-143123: It is possible for another function to finalize the current + // tracer's state while tracing. This might happen in a + // Python -> C -> Python call. + return 0; + } assert(!interp->compiling); assert(_tstate->jit_tracer_state.initial_state.stack_depth >= 0); #ifndef Py_GIL_DISABLED