]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-140936: Fix JIT assertion crash at finalization if some generator is alive (GH...
authorMikhail Efimov <efimov.mikhail@gmail.com>
Wed, 12 Nov 2025 19:04:02 +0000 (22:04 +0300)
committerGitHub <noreply@github.com>
Wed, 12 Nov 2025 19:04:02 +0000 (19:04 +0000)
Lib/test/test_capi/test_opt.py
Python/optimizer.c

index 4e94f62d35eba29229542299667d7601dbc96ec1..e65556fb28f92d6d0952d0056fb4dcd86c54cb3b 100644 (file)
@@ -2660,6 +2660,25 @@ class TestUopsOptimization(unittest.TestCase):
 
         f()
 
+    def test_interpreter_finalization_with_generator_alive(self):
+        script_helper.assert_python_ok("-c", textwrap.dedent("""
+            import sys
+            t = tuple(range(%d))
+            def simple_for():
+                for x in t:
+                    x
+
+            def gen():
+                try:
+                    yield
+                except:
+                    simple_for()
+
+            sys.settrace(lambda *args: None)
+            simple_for()
+            g = gen()
+            next(g)
+        """ % _testinternalcapi.SPECIALIZATION_THRESHOLD))
 
 
 def global_identity(x):
index f44f8a9614b8467ff8d63a524abaf1225b87b575..3b7e2dafab85bbd3819ddb1a7199a61d3db79500 100644 (file)
@@ -118,7 +118,13 @@ _PyOptimizer_Optimize(
 {
     _PyStackRef *stack_pointer = frame->stackpointer;
     PyInterpreterState *interp = _PyInterpreterState_GET();
-    assert(interp->jit);
+    if (!interp->jit) {
+        // gh-140936: It is possible that interp->jit will become false during
+        // interpreter finalization. However, the specialized JUMP_BACKWARD_JIT
+        // instruction may still be present. In this case, we should
+        // return immediately without optimization.
+        return 0;
+    }
     assert(!interp->compiling);
 #ifndef Py_GIL_DISABLED
     interp->compiling = true;