Title: UnboundLocalError leads to Illegal instruction crashing CPython
Type: crash Stage:
Components: Interpreter Core Versions: Python 3.11
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: lukasz.langa, xxm
Priority: normal Keywords:

Created on 2021-11-22 02:24 by xxm, last changed 2021-11-27 00:10 by terry.reedy.

File name Uploaded Description Edit xxm, 2021-11-22 02:24
Messages (1)
msg406740 - (view) Author: Xinmeng Xia (xxm) Date: 2021-11-22 02:24
The following code can lead to a crash and report Illegal instruction (core dumped)(few times) or Trace/breakpoint trap (core dumped) (very few times) or Segmentation fault (core dumped) (most times) on Python 3.11.
import inspect
import sys
import threading
import unittest
import weakref
import ctypes
from import run_doctest, run_unittest, cpython_only, check_impl_detail
import _testcapi
from types import FunctionType
from test import test_code
import test_code

def test_free_different_thread():
    f = CoExtra.get_func()

    class ThreadTest(threading.Thread):

        def __init__(CoExtra, f, test):
            CoExtra.f = CoExtra
            CoExtra.test = test

        def run(CoExtra):
            del CoExtra.f
            CoExtra.test.assertEqual(test_code.LAST_FREED, 500)
    test_code.SetExtra(f.__code__, test_code.FREE_INDEX, ctypes.c_voidp(500))
    f = ThreadTest(CoExtra, f)
    del tt
    CoExtra.assertEqual(test_code.LAST_FREED, 500)
CoExtra = test_code.CoExtra()

Traceback (most recent call last):
  File "/home/xxm/Desktop/", line 33, in <module>
  File "/home/xxm/Desktop/", line 28, in test_free_different_thread
    del tt
UnboundLocalError: cannot access local variable 'tt' where it is not associated with a value
Illegal instruction (core dumped)/Trace/breakpoint trap (core dumped)/Segmentation fault (core dumped)

Version: python 3.9, python 3.10, python 3.11 on ubuntu 16.04

Reproduce step: and place and test_code in a same directory.
2. run with "python"

The is from cpython' test. We can also annotate "import test_code" and run directly. But it seems that Illegal instruction and Trace/breakpoint trap cannot be reproduced.
Date User Action Args
2021-11-27 00:10:58terry.reedysetnosy: + lukasz.langa
2021-11-22 02:24:42xxmcreate