Skip to content

Commit de1ae45

Browse files
zhansheng.lzsclaude
andcommitted
Add cycle detection to root_cause traversal in error classes
Addresses PR #150 review feedback: the root_cause property now tracks visited exception IDs to prevent infinite loops if a circular __cause__ chain is accidentally introduced. Also handles empty cause messages gracefully in _format_cause. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent fc04b36 commit de1ae45

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

  • dashscope/finetune/reinforcement/common

dashscope/finetune/reinforcement/common/errors.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@ class _RootCauseMixin:
1414
@property
1515
def root_cause(self) -> "Exception":
1616
root: "Exception" = self # type: ignore[assignment]
17-
while root.__cause__:
17+
seen = {id(root)}
18+
while root.__cause__ and id(root.__cause__) not in seen:
1819
root = root.__cause__
20+
seen.add(id(root))
1921
return root
2022

2123
def _format_cause(self) -> str:
2224
"""Format root cause information if available."""
23-
if self.__cause__ is not None and self is not self.root_cause:
25+
if self.__cause__ is not None:
2426
root = self.root_cause
25-
cause_msg = str(root).split("\n", maxsplit=1)[0][:100]
26-
return f" (caused by: {type(root).__name__}: {cause_msg})"
27+
if self is not root:
28+
cause_msg = str(root).split("\n", maxsplit=1)[0][:100].strip()
29+
if cause_msg:
30+
return f" (caused by: {type(root).__name__}: {cause_msg})"
31+
return f" (caused by: {type(root).__name__})"
2732
return ""
2833

2934

0 commit comments

Comments
 (0)