Skip to content

Commit 35ed3e4

Browse files
gh-140936: Fix JIT assertion crash at finalization if some generator is alive (GH-140969)
1 parent 1f381a5 commit 35ed3e4

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,6 +2660,25 @@ def f():
26602660

26612661
f()
26622662

2663+
def test_interpreter_finalization_with_generator_alive(self):
2664+
script_helper.assert_python_ok("-c", textwrap.dedent("""
2665+
import sys
2666+
t = tuple(range(%d))
2667+
def simple_for():
2668+
for x in t:
2669+
x
2670+
2671+
def gen():
2672+
try:
2673+
yield
2674+
except:
2675+
simple_for()
2676+
2677+
sys.settrace(lambda *args: None)
2678+
simple_for()
2679+
g = gen()
2680+
next(g)
2681+
""" % _testinternalcapi.SPECIALIZATION_THRESHOLD))
26632682

26642683

26652684
def global_identity(x):

Python/optimizer.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,13 @@ _PyOptimizer_Optimize(
118118
{
119119
_PyStackRef *stack_pointer = frame->stackpointer;
120120
PyInterpreterState *interp = _PyInterpreterState_GET();
121-
assert(interp->jit);
121+
if (!interp->jit) {
122+
// gh-140936: It is possible that interp->jit will become false during
123+
// interpreter finalization. However, the specialized JUMP_BACKWARD_JIT
124+
// instruction may still be present. In this case, we should
125+
// return immediately without optimization.
126+
return 0;
127+
}
122128
assert(!interp->compiling);
123129
#ifndef Py_GIL_DISABLED
124130
interp->compiling = true;

0 commit comments

Comments
 (0)