Skip to content

Commit 48b7d75

Browse files
[3.14] gh-140936: Fix JIT assertion crash at finalization if some generator is alive (GH-140969) (GH-141494)
gh-140936: Fix JIT assertion crash at finalization if some generator is alive (GH-140969)
1 parent 425e423 commit 48b7d75

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,6 +1972,26 @@ def testfunc(n):
19721972
assert ex is not None
19731973
"""))
19741974

1975+
def test_interpreter_finalization_with_generator_alive(self):
1976+
script_helper.assert_python_ok("-c", textwrap.dedent("""
1977+
import sys
1978+
t = tuple(range(%d))
1979+
def simple_for():
1980+
for x in t:
1981+
x
1982+
1983+
def gen():
1984+
try:
1985+
yield
1986+
except:
1987+
simple_for()
1988+
1989+
sys.settrace(lambda *args: None)
1990+
simple_for()
1991+
g = gen()
1992+
next(g)
1993+
""" % _testinternalcapi.SPECIALIZATION_THRESHOLD))
1994+
19751995

19761996
def global_identity(x):
19771997
return x

Python/optimizer.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,14 @@ _PyOptimizer_Optimize(
116116
_PyExecutorObject **executor_ptr, int chain_depth)
117117
{
118118
_PyStackRef *stack_pointer = frame->stackpointer;
119-
assert(_PyInterpreterState_GET()->jit);
119+
PyInterpreterState *interp = _PyInterpreterState_GET();
120+
if (!interp->jit) {
121+
// gh-140936: It is possible that interp->jit will become false during
122+
// interpreter finalization. However, the specialized JUMP_BACKWARD_JIT
123+
// instruction may still be present. In this case, we should
124+
// return immediately without optimization.
125+
return 0;
126+
}
120127
// The first executor in a chain and the MAX_CHAIN_DEPTH'th executor *must*
121128
// make progress in order to avoid infinite loops or excessively-long
122129
// side-exit chains. We can only insert the executor into the bytecode if

0 commit comments

Comments
 (0)