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
- Create and activate a fresh virtual-env
pip install "logfire==4.0.0"
- 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
- Run it:
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
- In
canonicalize_exception_traceback()
keep a seen: set[int]
of
id(exc)
values and skip objects that have already been visited.
- Alternatively, guard recursion depth with a counter / max-depth.
- Add a unit test using the minimal failing object above.
Python, Logfire & OS Versions, related packages (not required)