This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Random and infinite loop in dealing with recursion error for "try-except "
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Mark.Shannon, serhiy.storchaka, xxm
Priority: normal Keywords:

Created on 2021-01-18 04:17 by xxm, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg385171 - (view) Author: Xinmeng Xia (xxm) Date: 2021-01-18 04:17
In issue 42500, recursive calls in "Try-except" are resolved. This PR has fixed the crashes of some programs, such as program 1. And the core dump error is replaced with RecursiveError.  
However, program 2 will not report a RecursiveError. The program will fall into an infinite loop. Even "Ctrl C" cannot stop the infinite loop. I try to track the execution of this program and insert "print" information(see program 3). The output seems random in execution between try branch and except branch!  I think this is a new bug after fixing 42500. I believe the program should also return RecursiveError.


Program 1
=========================== 
def foo():
    try:
        1/0
    except:
        foo()
foo()
================================

Program 2
================================
def foo():
    try:
        foo()
    except:
        foo()
foo()
================================


Program 3
================================
def foo():
    try:
        print("a")
        foo()
    except:
        print("b")
        foo()

foo()
================================
Output for program3( unexpected infinite random loop. ):
......bbaaaabbabbaabbabbaaabbabbaabbabbaaaaaaaabbabbaabbabbaaabbabbaabbabbaaaabbabbaabbabbaaabbabbaabbabbaaaaabbabbaabbabbaaabbabbaabbabbaaaabbabbaabbabbaaabbabbaabbabbaaaaaabbabbaabbabbaaabbabbaabbabbaaaabb......

>>python -V
Python 3.10.0a4
msg385204 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-01-18 15:59
Funny. But the output is not random. You can generate the sequence of letters by the following simple loop:

s = ''
for i in range(n):
    s = f'a{s}b{s}'

The length of this sequence is 2*(2**n-1). Finally, your code will raise a non-silenced RecursiveError, but it will just take some time (for printing around 2**1000 letters).
msg385260 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-01-19 13:43
Try setting the recursion limit to 10 or so and it should terminate.

The reason ctrl-C doesn't work is that you are catching the KeyboardInterrupt. Never use a plain `except:`, use `except Exception:`
msg385261 - (view) Author: Xinmeng Xia (xxm) Date: 2021-01-19 14:10
oh,I see. By the way, I set the argument of sys.setrecursionlimit to 100000 in this program and a segmentation fault is reported. Is that normal?
msg385408 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-01-21 11:27
Yes, see PEP 651
History
Date User Action Args
2022-04-11 14:59:40adminsetgithub: 87117
2021-08-15 11:42:46iritkatriellinkissue44917 superseder
2021-01-21 11:27:29Mark.Shannonsetstatus: open -> closed
resolution: not a bug
stage: resolved
2021-01-21 11:27:05Mark.Shannonsetmessages: + msg385408
2021-01-19 14:10:02xxmsetmessages: + msg385261
2021-01-19 13:43:14Mark.Shannonsetnosy: + Mark.Shannon
messages: + msg385260
2021-01-18 15:59:07serhiy.storchakasetmessages: + msg385204
2021-01-18 12:01:08serhiy.storchakasetnosy: + serhiy.storchaka
2021-01-18 04:17:00xxmcreate