Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion logfire/_internal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,25 @@ def canonicalize_exception_traceback(exc: BaseException, seen: set[int] | None =
try:
exc_type = type(exc)
parts = [f'\n{exc_type.__module__}.{exc_type.__qualname__}\n----']
num_repeats = 0
if exc.__traceback__:
visited: set[str] = set()
for frame, lineno in traceback.walk_tb(exc.__traceback__):
filename = frame.f_code.co_filename
source_line = linecache.getline(filename, lineno, frame.f_globals).strip()
module = frame.f_globals.get('__name__', filename)
frame_summary = f'{module}.{frame.f_code.co_name}\n {source_line}'
if frame_summary not in visited: # ignore repeated frames
if frame_summary in visited:
num_repeats += 1
if num_repeats >= 100 and isinstance(exc, RecursionError):
# The last few frames of a RecursionError traceback are often *not* the recursive function(s)
# being called repeatedly (which are already deduped here) but instead some other function(s)
# called normally which happen to use up the last bit of the recursion limit.
# These can easily vary between runs and we don't want to pay attention to them,
# the real problem is the recursion itself.
parts.append('\n<recursion detected>')
break
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's what happens without this:

Screenshot 2025-10-03 at 15 38 05

else: # skip repeated frames
visited.add(frame_summary)
parts.append(frame_summary)
seen = seen or set()
Expand Down
63 changes: 63 additions & 0 deletions tests/test_canonicalize_exception.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
from typing import Callable

import pytest
from inline_snapshot import snapshot
Expand Down Expand Up @@ -255,3 +256,65 @@ def test_canonicalize_no_traceback():
builtins.ValueError
----\
""")


def test_recursion():
def foo(b: Callable[[], None]):
b()
foo(b)

def foo2():
foo(foo2)

def bar():
baz()

def baz():
pass

try:
foo(bar)
except Exception as e:
assert canonicalize_exception_traceback(e).replace(__file__, '__file__') == snapshot("""\

builtins.RecursionError
----
tests.test_canonicalize_exception.test_recursion
foo(bar)
tests.test_canonicalize_exception.foo
foo(b)

<recursion detected>\
""")

try:
foo(baz)
except Exception as e:
assert canonicalize_exception_traceback(e).replace(__file__, '__file__') == snapshot("""\

builtins.RecursionError
----
tests.test_canonicalize_exception.test_recursion
foo(baz)
tests.test_canonicalize_exception.foo
foo(b)

<recursion detected>\
""")

try:
foo2()
except Exception as e:
assert canonicalize_exception_traceback(e).replace(__file__, '__file__') == snapshot("""\

builtins.RecursionError
----
tests.test_canonicalize_exception.test_recursion
foo2()
tests.test_canonicalize_exception.foo2
foo(foo2)
tests.test_canonicalize_exception.foo
b()

<recursion detected>\
""")
Loading