Skip to content

Commit 89aec8b

Browse files
Fix OOM in CI by clearing chained exception tracebacks (#5776)
1 parent 23201dd commit 89aec8b

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

tests/conftest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ def pytest_runtest_makereport(item, call):
3434
yield
3535
if call.when == "call" and call.excinfo is not None:
3636
traceback.clear_frames(call.excinfo.tb)
37+
# Also clear all reachable chained exception tracebacks (both __context__ and __cause__ at
38+
# every node): when OOM fires inside a try/except in the trainer, the OOM becomes __context__
39+
# of the outer exception and its traceback holds frame locals (model, tensors) that prevent gc
40+
# from releasing CUDA memory even after clear_frames above.
41+
stack, seen = [call.excinfo.value], set()
42+
while stack:
43+
exc = stack.pop()
44+
if exc is None or id(exc) in seen:
45+
continue
46+
seen.add(id(exc))
47+
if exc.__traceback__ is not None:
48+
traceback.clear_frames(exc.__traceback__)
49+
exc.__traceback__ = None
50+
stack.append(exc.__context__)
51+
stack.append(exc.__cause__)
3752

3853

3954
# ============================================================================

0 commit comments

Comments
 (0)