classification
Title: recursive in finally clause cause Python interpreter crash.
Type: crash Stage: resolved
Components: Interpreter Core Versions: Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder: crash with unbounded recursion in except statement
View: 42500
Assigned To: Nosy List: ronaldoussoren, stestagg, xxm
Priority: normal Keywords:

Created on 2020-12-16 08:48 by xxm, last changed 2020-12-22 08:32 by ronaldoussoren. This issue is now closed.

Messages (5)
msg383125 - (view) Author: Xinmeng Xia (xxm) Date: 2020-12-16 08:48
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)
-------------------------------------------------------------------------
msg383250 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2020-12-17 15:36
This might be related to #42500
msg383252 - (view) Author: Xinmeng Xia (xxm) Date: 2020-12-17 16:33
In issue #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 #42500. It should be related to traceback module, finally clause and recursion. what do you think?
msg383273 - (view) Author: Steve Stagg (stestagg) Date: 2020-12-17 22:14
This was also fixed by bpo-42500, in commit 4e7a69bdb6 
===
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 ((93a0ef76…)) [1]> git checkout 4e7a69bd
Previous HEAD position was 93a0ef7647 Correct return type in Modules/_ssl.c::sslmodule_legacy (GH-23609)
HEAD is now at 4e7a69bdb6 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
msg383574 - (view) Author: Xinmeng Xia (xxm) Date: 2020-12-22 08:24
Thanks for fixing this, looking forward to the new version.

Could you please kindly change the resolution into fixed and close this report?
History
Date User Action Args
2020-12-22 08:32:50ronaldoussorensetstatus: open -> closed
superseder: crash with unbounded recursion in except statement
resolution: fixed
stage: resolved
2020-12-22 08:24:55xxmsetmessages: + msg383574
2020-12-17 22:14:21stestaggsetnosy: + stestagg
messages: + msg383273
2020-12-17 16:33:57xxmsetmessages: + msg383252
2020-12-17 15:36:21ronaldoussorensetnosy: + ronaldoussoren
messages: + msg383250
2020-12-16 08:48:19xxmcreate