Skip to content

Commit 79b26a5

Browse files
committed
fix infinite loop
1 parent a0e5aa4 commit 79b26a5

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

packages/service-library/src/servicelib/logging_errors.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ def create_troubleshootting_log_message(
3131
def _collect_causes(exc: BaseException) -> str:
3232
causes = []
3333
current = exc.__cause__
34-
while current is not None:
34+
seen = set() # Prevent infinite loops
35+
while current is not None and id(current) not in seen:
36+
seen.add(id(current))
3537
causes.append(f"[{type(current).__name__}]'{current}'")
3638
current = getattr(current, "__cause__", None)
3739
return " <- ".join(causes)

packages/service-library/tests/test_background_task.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,16 @@ async def _func() -> None:
207207
assert mock_func.call_count > 1
208208

209209

210+
class CustomError(Exception):
211+
pass
212+
213+
210214
async def test_periodic_task_logs_error(
211215
mock_background_task: mock.AsyncMock,
212216
task_interval: datetime.timedelta,
213217
caplog: pytest.LogCaptureFixture,
214218
):
215-
mock_background_task.side_effect = RuntimeError("Test error")
219+
mock_background_task.side_effect = CustomError("Test error")
216220

217221
with caplog.at_level(logging.ERROR):
218222
async with periodic_task(

0 commit comments

Comments
 (0)