@@ -138,6 +138,12 @@ _PyOptimizer_Optimize(
138138 // return immediately without optimization.
139139 return 0 ;
140140 }
141+ _PyExecutorObject * prev_executor = _tstate -> jit_tracer_state -> initial_state .executor ;
142+ if (prev_executor != NULL && !prev_executor -> vm_data .valid ) {
143+ // gh-143604: If we are a side exit executor and the original executor is no
144+ // longer valid, don't compile to prevent a reference leak.
145+ return 0 ;
146+ }
141147 assert (!interp -> compiling );
142148 assert (_tstate -> jit_tracer_state -> initial_state .stack_depth >= 0 );
143149#ifndef Py_GIL_DISABLED
@@ -1015,7 +1021,7 @@ Py_NO_INLINE int
10151021_PyJit_TryInitializeTracing (
10161022 PyThreadState * tstate , _PyInterpreterFrame * frame , _Py_CODEUNIT * curr_instr ,
10171023 _Py_CODEUNIT * start_instr , _Py_CODEUNIT * close_loop_instr , int curr_stackdepth , int chain_depth ,
1018- _PyExitData * exit , int oparg )
1024+ _PyExitData * exit , int oparg , _PyExecutorObject * current_executor )
10191025{
10201026 _PyThreadStateImpl * _tstate = (_PyThreadStateImpl * )tstate ;
10211027 if (_tstate -> jit_tracer_state == NULL ) {
@@ -1062,6 +1068,7 @@ _PyJit_TryInitializeTracing(
10621068 tracer -> initial_state .close_loop_instr = close_loop_instr ;
10631069 tracer -> initial_state .code = (PyCodeObject * )Py_NewRef (code );
10641070 tracer -> initial_state .func = (PyFunctionObject * )Py_NewRef (func );
1071+ tracer -> initial_state .executor = (_PyExecutorObject * )Py_XNewRef (current_executor );
10651072 tracer -> initial_state .exit = exit ;
10661073 tracer -> initial_state .stack_depth = curr_stackdepth ;
10671074 tracer -> initial_state .chain_depth = chain_depth ;
@@ -1089,6 +1096,7 @@ _PyJit_FinalizeTracing(PyThreadState *tstate)
10891096 _PyJitTracerState * tracer = _tstate -> jit_tracer_state ;
10901097 Py_CLEAR (tracer -> initial_state .code );
10911098 Py_CLEAR (tracer -> initial_state .func );
1099+ Py_CLEAR (tracer -> initial_state .executor );
10921100 Py_CLEAR (tracer -> prev_state .instr_code );
10931101 tracer -> prev_state .code_curr_size = CODE_SIZE_EMPTY ;
10941102 tracer -> prev_state .code_max_size = UOP_MAX_TRACE_LENGTH /2 - 1 ;
0 commit comments