diff --git a/Python/optimizer.c b/Python/optimizer.c index 9db894f0bf054a..152e9c65e495d3 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -1669,12 +1669,17 @@ executor_clear(PyObject *op) * free the executor unless we hold a strong reference to it */ _PyExecutorObject *cold = _PyExecutor_GetColdExecutor(); + _PyExecutorObject *cold_dynamic = _PyExecutor_GetColdDynamicExecutor(); Py_INCREF(executor); for (uint32_t i = 0; i < executor->exit_count; i++) { executor->exits[i].temperature = initial_unreachable_backoff_counter(); _PyExecutorObject *e = executor->exits[i].executor; - executor->exits[i].executor = cold; - Py_DECREF(e); + executor->exits[i].executor = NULL; + // Only clear side exit executors in the chain, not + // those that have progress (inserted into bytecode). + if (e != cold && e != cold_dynamic && e->vm_data.code != NULL) { + executor_clear((PyObject *)e); + } } _Py_ExecutorDetach(executor); Py_DECREF(executor);