Commit 252d26e
Fix memory leak in optional_import traceback handling (#8782)
## Summary
- Fix memory leak in `optional_import` where stored traceback objects
retain references to entire call stacks, preventing garbage collection
- Format traceback to string immediately and clear
`import_exception.__traceback__ = None`
- Hoist 3 `optional_import` calls for cucim to module level in
`monai/transforms/utils.py`
Fixes #7480, #7727
## Details
**`monai/utils/module.py`:**
- Replace storing live `__traceback__` object with
`traceback.format_exception()` string
- Clear `import_exception.__traceback__ = None` to break reference chain
- Embed formatted traceback in error message under "Original traceback:"
section
**`monai/transforms/utils.py`:**
- Move cucim `optional_import` calls to module level (consistent with
existing skimage/scipy/cupy pattern)
- Fixes incidental name shadowing in `distance_transform_edt`
## Test plan
- [x] `test_no_traceback_leak` — weakref regression test for the leak
- [x] `test_failed_import_shows_traceback_string` — verifies traceback
in error message
- [x] All 12 `test_optional_import.py` tests pass
- [x] pre-commit, black, ruff, pytype, mypy — all clean
---------
Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
Signed-off-by: Soumya Snigdha Kundu <soumyawork15@gmail.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>1 parent 089f02d commit 252d26e
File tree
3 files changed
+45
-7
lines changed- monai
- transforms
- utils
- tests/utils
3 files changed
+45
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
88 | 91 | | |
89 | 92 | | |
90 | 93 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
368 | 369 | | |
369 | 370 | | |
370 | 371 | | |
371 | | - | |
| 372 | + | |
372 | 373 | | |
| 374 | + | |
373 | 375 | | |
374 | 376 | | |
375 | 377 | | |
| |||
384 | 386 | | |
385 | 387 | | |
386 | 388 | | |
387 | | - | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
388 | 393 | | |
| 394 | + | |
389 | 395 | | |
390 | 396 | | |
391 | 397 | | |
| |||
394 | 400 | | |
395 | 401 | | |
396 | 402 | | |
397 | | - | |
| 403 | + | |
398 | 404 | | |
399 | 405 | | |
400 | 406 | | |
| |||
407 | 413 | | |
408 | 414 | | |
409 | 415 | | |
410 | | - | |
411 | | - | |
412 | | - | |
413 | | - | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
414 | 419 | | |
415 | 420 | | |
416 | 421 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
| 16 | + | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
| |||
75 | 77 | | |
76 | 78 | | |
77 | 79 | | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
78 | 108 | | |
79 | 109 | | |
80 | 110 | | |
0 commit comments