Skip to content

Deep recursive exception breaks traceback printing #118978

Closed as not planned
Closed as not planned
@richard-hajek

Description

@richard-hajek

Bug report

Bug description:

I tried to mess with CPython and try to generate the longest possible sequence of tracebacks.

class CustomException(Exception):
    pass

def recurse(depth=0):
    try:
        raise CustomException()
    except CustomException:
        recurse(depth + 1)


def main():
    recurse()

main()

I expected a chain of "During handling ...."

Traceback (most recent call last):
  File "/home/meowxiik/Cloud/Projects/bully-python/src/bully_python/__init__.py", line 8, in recurse
    raise CustomException()
CustomException

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/meowxiik/Cloud/Projects/bully-python/src/bully_python/__init__.py", line 8, in recurse
    raise CustomException()
CustomException

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/meowxiik/Cloud/Projects/bully-python/src/bully_python/__init__.py", line 8, in recurse
    raise CustomException()
CustomException

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/meowxiik/Cloud/Projects/bully-python/src/bully_python/__init__.py", line 8, in recurse
    raise CustomException()
CustomException

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/meowxiik/Cloud/Projects/bully-python/src/bully_python/__init__.py", line 8, in recurse
    raise CustomException()
CustomException

[rest ommited]

Instead of that I got

object address  : 0x7c3def3ef340
object refcount : 3
object type     : 0x7c3df14aa958
object type name: RecursionError
object repr     : RecursionError('maximum recursion depth exceeded')
lost sys.stderr

This happens around stack size 1000. Python's memory is fine, if I increase the sys.setrecursionlimit, I can safely push the stack into much larger numbers. I don't know what lost sys.stderr means, I can do print("whatever", file=sys.stderr) if I catch this exception easily.

The CPython traceback looks like this

(gdb) bt
#0  _PyObject_Dump (op=0x7ffff5cdec20) at Objects/object.c:489
#1  0x00007ffff6891421 in _PyErr_Display (file=0x7ffff5cc59a0, unused=<optimized out>, value=0x7ffff5cdec20, tb=<optimized out>) at Python/pythonrun.c:1526
#2  0x00007ffff6891586 in PyErr_Display (unused=<optimized out>, value=0x7ffff5cdec20, tb=0x7ffff5c45100) at Python/pythonrun.c:1556
#3  0x00007ffff6895299 in sys_excepthook_impl (value=0x7ffff5cdecd0, traceback=0x79, module=<optimized out>, exctype=<optimized out>) at ./Python/sysmodule.c:772
#4  sys_excepthook (module=<optimized out>, args=0x7fffffffcc00, nargs=<optimized out>) at ./Python/clinic/sysmodule.c.h:102
#5  0x00007ffff6996400 in cfunction_vectorcall_FASTCALL (func=0x7ffff5d441d0, args=0x7fffffffcc00, nargsf=<optimized out>, kwnames=0x0) at Objects/methodobject.c:422
#6  0x00007ffff6966643 in _PyObject_VectorcallTstate (tstate=0x7ffff7e33f68 <_PyRuntime+459656>, callable=0x7ffff5d441d0, args=0x7fffffffcc00, nargsf=3, kwnames=0x0) at ./Include/internal/pycore_call.h:92
#7  0x00007ffff6a87ca7 in _PyObject_FastCallTstate (tstate=0x7ffff5cdec20, func=0x7ffff5cdecd0, args=0x79, nargs=0) at ./Include/internal/pycore_call.h:116
#8  0x00007ffff689118b in _PyErr_PrintEx (tstate=0x7ffff7e33f68 <_PyRuntime+459656>, set_sys_last_vars=1) at Python/pythonrun.c:807
#9  0x00007ffff6890467 in PyErr_PrintEx (set_sys_last_vars=1) at Python/pythonrun.c:845
#10 PyErr_Print () at Python/pythonrun.c:851
#11 _PyRun_SimpleFileObject (fp=0x83a2d0, filename=0x7ffff5cf9680, closeit=<optimized out>, flags=0x7fffffffcce0) at Python/pythonrun.c:439
#12 0x00007ffff6890046 in _PyRun_AnyFileObject (fp=0x83a2d0, filename=0x7ffff5cf9680, closeit=1, flags=0x7fffffffcce0) at Python/pythonrun.c:78
#13 0x00007ffff689b734 in pymain_run_file_obj (program_name=0x7ffff5cf97d0, filename=0x7ffff5cf9680, skip_source_first_line=0) at Modules/main.c:360
#14 0x00007ffff689b4c2 in pymain_run_file (config=0x7ffff7dd6b48 <_PyRuntime+77672>) at Modules/main.c:379
#15 0x00007ffff6b598b0 in pymain_run_python (exitcode=0x7fffffffcddc) at Modules/main.c:629
#16 Py_RunMain () at Modules/main.c:709
#17 0x00007ffff689b055 in pymain_main (args=<optimized out>) at Modules/main.c:739
#18 0x00007ffff689b0b7 in Py_BytesMain (argc=<optimized out>, argv=0x7ffff5cdecd0) at Modules/main.c:763
#19 0x00007ffff6439c88 in __libc_start_call_main (main=main@entry=0x600000 <main>, argc=argc@entry=2, argv=argv@entry=0x7fffffffcfc8) at ../sysdeps/nptl/libc_start_call_main.h:58
#20 0x00007ffff6439d4c in __libc_start_main_impl (main=0x600000 <main>, argc=2, argv=0x7fffffffcfc8, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffcfb8)
    at ../csu/libc-start.c:360
#21 0x00000000006000a9 in _start ()
(gdb) 

I am unfamiliar with the conventions of CPython, if this exit is not considered a bug, please close this issue.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions