Skip to content

recursive in finally clause cause Python interpreter crash. #86818

Closed

Description

@xxm
mannequin
BPO 42652
Nosy @ronaldoussoren, @stestagg
Superseder
  • bpo-42500: crash with unbounded recursion in except statement
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-12-22.08:32:50.237>
    created_at = <Date 2020-12-16.08:48:19.501>
    labels = ['interpreter-core', '3.8', '3.9', '3.10', '3.7', 'type-crash']
    title = 'recursive in finally clause cause Python interpreter crash.'
    updated_at = <Date 2020-12-22.08:32:50.237>
    user = 'https://bugs.python.org/xxm'

    bugs.python.org fields:

    activity = <Date 2020-12-22.08:32:50.237>
    actor = 'ronaldoussoren'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-12-22.08:32:50.237>
    closer = 'ronaldoussoren'
    components = ['Interpreter Core']
    creation = <Date 2020-12-16.08:48:19.501>
    creator = 'xxm'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42652
    keywords = []
    message_count = 5.0
    messages = ['383125', '383250', '383252', '383273', '383574']
    nosy_count = 3.0
    nosy_names = ['ronaldoussoren', 'xxm', 'stestagg']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = '42500'
    type = 'crash'
    url = 'https://bugs.python.org/issue42652'
    versions = ['Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10']

    Activity

    1. xxm commented on Dec 16, 2020

      xxmmannequin
      MannequinAuthor

      Considering the following two program,running the program 1 will get expected output: RecursionError

      program 1
      ===========================

      import traceback
      
      def foo():
      	try:
      		1/0
      	except Exception as e:
      		traceback.print_exc()
      	finally:
      		a = 1
      	foo()	
      		
      foo()
      
      ==========================

      ZeroDivisionError: division by zero
      Traceback (most recent call last):
        File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 5, in foo
          1/0
      ZeroDivisionError: division by zero
      Traceback (most recent call last):
        File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 5, in foo
          1/0
      ZeroDivisionError: division by zero
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 5, in foo
          1/0
      ZeroDivisionError: division by zero
      Traceback (most recent call last):
        File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 5, in foo
          1/0
      ZeroDivisionError: division by zero
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 12, in <module>

      File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 10, in foo
      foo()
      File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 10, in foo
      ...
      foo()
      File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 10, in foo
      foo()
      File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 7, in foo
      traceback.print_exc()
      File "/usr/lib/python3.5/traceback.py", line 159, in print_exc
      print_exception(*sys.exc_info(), limit=limit, file=file, chain=chain)
      File "/usr/lib/python3.5/traceback.py", line 100, in print_exception
      type(value), value, tb, limit=limit).format(chain=chain):
      File "/usr/lib/python3.5/traceback.py", line 474, in __init__
      capture_locals=capture_locals)
      File "/usr/lib/python3.5/traceback.py", line 358, in extract
      f.line
      File "/usr/lib/python3.5/traceback.py", line 282, in line
      self._line = linecache.getline(self.filename, self.lineno).strip()
      File "/usr/lib/python3.5/linecache.py", line 16, in getline
      lines = getlines(filename, module_globals)
      File "/usr/lib/python3.5/linecache.py", line 43, in getlines
      if len(entry) != 1:
      RecursionError: maximum recursion depth exceeded in comparison
      ------------------------------------------------------------------------

      However when moving foo() into finally clause, the interpreter crashes.

      program 2
      ==========================

      import traceback
      
      def foo():
      	try:
      		1/0
      	except Exception as e:
      		traceback.print_exc()
      	finally:
      		a = 1
      		foo()	
      		
      foo()

      ==========================
      -----------------------------------------------------------------------------

      File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 10 in foo
      Traceback (most recent call last):
        File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 5, in foo
          1/0
      ZeroDivisionError: division by zero
      Traceback (most recent call last):
        File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 5, in foo
          1/0
      ZeroDivisionError: division by zero
      Traceback (most recent call last):
        File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 5, in foo
          1/0
      ZeroDivisionError: division by zero
      Traceback (most recent call last):
        File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 5, in foo
          1/0
      ZeroDivisionError: division by zero
      Traceback (most recent call last):
        File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 5, in foo
          1/0
      ZeroDivisionError: division by zero
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 7, in foo
          traceback.print_exc()
        File "/usr/lib/python3.5/traceback.py", line 159, in print_exc
          print_exception(*sys.exc_info(), limit=limit, file=file, chain=chain)
        File "/usr/lib/python3.5/traceback.py", line 100, in print_exception
          type(value), value, tb, limit=limit).format(chain=chain):
        File "/usr/lib/python3.5/traceback.py", line 474, in __init__
          capture_locals=capture_locals)
        File "/usr/lib/python3.5/traceback.py", line 358, in extract
          f.line
        File "/usr/lib/python3.5/traceback.py", line 282, in line
          self._line = linecache.getline(self.filename, self.lineno).strip()
        File "/usr/lib/python3.5/linecache.py", line 16, in getline
          lines = getlines(filename, module_globals)
        File "/usr/lib/python3.5/linecache.py", line 43, in getlines
          if len(entry) != 1:
      RecursionError: maximum recursion depth exceeded in comparison
      
      During handling of the above exception, another exception occurred:
      
      
      Traceback (most recent call last):
        File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 5, in foo
          1/0

      File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 10 in foo
      File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 10 in foo
      File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 10 in foo
      ...
      File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 10 in foo
      File "/home/xxm/Desktop/nameChanging/myerror/test1.py", line 10 in foo
      ...
      Aborted (core dumped)
      -------------------------------------------------------------------------

    2. added
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      type-crashA hard crash of the interpreter, possibly with a core dump
      on Dec 16, 2020
    3. ronaldoussoren commented on Dec 17, 2020

      @ronaldoussoren
      Contributor

      This might be related to bpo-42500

    4. xxm commented on Dec 17, 2020

      xxmmannequin
      MannequinAuthor

      In issue bpo-42500, crashes is resulted by recursion and try-except. Program like following will crash the interpreter.
      =====

      def foo():
      	try:
      		1/0
      	except:
      		pass
      	foo()
      foo()

      =====
      However with traceback module, program will no longer crash the interpreter. A recursive Error is returned as expected.
      =====

      import traceback
      def foo():
      	try:
      		1/0
      	except:
      		traceback.print_exc()
      	foo()
      foo()

      =====
      But it is still crash the interpreter in finally clause. I think this might be a new but and it is different from bpo-42500. It should be related to traceback module, finally clause and recursion. what do you think?

    5. stestagg commented on Dec 17, 2020

      stestaggmannequin
      Mannequin

      This was also fixed by bpo-42500, in commit 4e7a69b
      ===

      user@obsidian ~/t/f/cpython ((93a0ef76…))> git checkout 4e7a69bd^
      HEAD is now at 93a0ef7647 Correct return type in Modules/_ssl.c::sslmodule_legacy (GH-23609)
      + make distclean
      + CFLAGS=-O0
      + ./configure --prefix=/home/user/prefix --exec-prefix=/home/user/prefix --cache-file=../config.cache --with-pydebug --without-ensurepip
      + CFLAGS=-O0
      + make -j8 install
      /home/user/cpython/Lib/runpy.py:111: PendingDeprecationWarning: lib2to3 package is deprecated and may not be able to parse Python 3.10+
        __import__(pkg_name)
      /home/user/cpython/Lib/runpy.py:111: PendingDeprecationWarning: lib2to3 package is deprecated and may not be able to parse Python 3.10+
        __import__(pkg_name)
      ++ realpath /home/user/prefix/include/python3.10d/
      + INCLUDE_DIR=/home/user/prefix/include/python3.10d
      + set +e
      + PYTHONHOME=/home/user/prefix
      + /home/user/prefix/bin/python3 ../test.py
      Traceback (most recent call last):
        File "/home/user/cpython/../test.py", line 5, in foo
          1/0
      ZeroDivisionError: division by zero
      Traceback (most recent call last):
        File "/home/user/cpython/../test.py", line 5, in foo
          1/0
      ZeroDivisionError: division by zero
      ...
        File "/home/user/cpython/../test.py", line 10 in foo
        File "/home/user/cpython/../test.py", line 10 in foo
        ...
      ../test.sh: line 33: 384713 Aborted                 (core dumped) PYTHONHOME=$PREFIX $EPREFIX/bin/python3 ../test.py
      + RESULT=134
      + [[ 134 -eq 139 ]]
      + [[ 134 -eq 134 ]]
      + exit 1

      ====
      user@obsidian ~/t/f/cpython ((93a0ef7…)) [1]> git checkout 4e7a69b
      Previous HEAD position was 93a0ef7 Correct return type in Modules/_ssl.c::sslmodule_legacy (GH-23609)
      HEAD is now at 4e7a69b bpo-42500: Fix recursion in or after except (GH-23568)
      ====

      user@obsidian ~/t/f/cpython ((4e7a69bd…))> ../test.sh
      + make distclean
      + CFLAGS=-O0
      + ./configure --prefix=/home/user/prefix --exec-prefix=/home/user/prefix --cache-file=../config.cache --with-pydebug --without-ensurepip
      + CFLAGS=-O0
      + make -j8 install
      /home/user/cpython/Lib/runpy.py:111: PendingDeprecationWarning: lib2to3 package is deprecated and may not be able to parse Python 3.10+
        __import__(pkg_name)
      /home/user/cpython/Lib/runpy.py:111: PendingDeprecationWarning: lib2to3 package is deprecated and may not be able to parse Python 3.10+
        __import__(pkg_name)
      + /home/user/prefix/bin/python3 ../test.py
      Traceback (most recent call last):
        File "/home/user/cpython/../test.py", line 5, in foo
          1/0
      ...
        File "/home/user/cpython/../test.py", line 10, in foo
          foo()	
        File "/home/user/cpython/../test.py", line 10, in foo
          foo()	
        [Previous line repeated 996 more times]
      RecursionError: maximum recursion depth exceeded
      + RESULT=1
      + [[ 1 -eq 139 ]]
      + [[ 1 -eq 134 ]]
      + exit 0
    6. xxm commented on Dec 22, 2020

      xxmmannequin
      MannequinAuthor

      Thanks for fixing this, looking forward to the new version.

      Could you please kindly change the resolution into fixed and close this report?

    7. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.10 (EOL)end of life3.7 (EOL)end of life3.8 (EOL)end of life3.9 (EOL)end of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dump

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions