Skip to content

Infinite recursion in logfire._internal.utils.canonicalize_exception_traceback when an ExceptionGroup with a self-reference reaches logfire #1279

@ryan-castner

Description

@ryan-castner

Description

This bug happened to me when using logfire with fastapi and starlette using a StreamingResponse, below is a minimal repro case.

Environment

Python        : 3.11.x
logfire       : 4.0.0           # latest on PyPI at time of writing, occurs on 3.x as well

How to reproduce

  1. Create and activate a fresh virtual-env
pip install "logfire==4.0.0"
  1. Save the following script as main.py:
import logfire
from logfire._internal.utils import canonicalize_exception_traceback

logfire.configure()


def make_loop():
    inner = RuntimeError()
    group = ExceptionGroup("loop", [inner])
    inner.__context__ = group
    return group


if __name__ == "__main__":
    canonicalize_exception_traceback(make_loop())  # RecursionError
  1. Run it:
python main.py

Actual behaviour

The server console floods with an ever-growing traceback that ends in

RecursionError: maximum recursion depth exceeded

The repeat pattern is always inside

logfire/_internal/utils.py: canonicalize_exception_traceback

and the process may eventually exit or hang.

Expected behaviour

logfire should record the exception once without falling into infinite recursion.

Suggested fix

  1. In canonicalize_exception_traceback() keep a seen: set[int] of
    id(exc) values and skip objects that have already been visited.
  2. Alternatively, guard recursion depth with a counter / max-depth.
  3. Add a unit test using the minimal failing object above.

Python, Logfire & OS Versions, related packages (not required)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions