Commit 5395848
committed
When an import fails, `optional_import` captured the live traceback
object and stored it in a `_LazyRaise` closure. The traceback held
references to stack frames containing CUDA tensors, preventing garbage
collection and causing GPU memory leaks.
Replace the live traceback with a string-formatted copy via
`traceback.format_exception()` and clear the original with
`import_exception.__traceback__ = None`. The formatted traceback is
appended to the error message so debugging information is preserved.
Also move three hot-path `optional_import` calls in
`monai/transforms/utils.py` (cucim.skimage, cucim morphology EDT)
from function bodies to module level, eliminating repeated leaked
tracebacks on every invocation.
Fixes #7480, fixes #7727
Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
1 parent daaedaa commit 5395848
3 files changed
Lines changed: 54 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
88 | 92 | | |
89 | 93 | | |
90 | 94 | | |
| |||
1147 | 1151 | | |
1148 | 1152 | | |
1149 | 1153 | | |
1150 | | - | |
1151 | | - | |
| 1154 | + | |
1152 | 1155 | | |
1153 | 1156 | | |
1154 | | - | |
| 1157 | + | |
1155 | 1158 | | |
1156 | 1159 | | |
1157 | 1160 | | |
| |||
1204 | 1207 | | |
1205 | 1208 | | |
1206 | 1209 | | |
1207 | | - | |
1208 | | - | |
1209 | | - | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
1210 | 1213 | | |
1211 | 1214 | | |
1212 | 1215 | | |
1213 | | - | |
| 1216 | + | |
1214 | 1217 | | |
1215 | 1218 | | |
1216 | 1219 | | |
| |||
2463 | 2466 | | |
2464 | 2467 | | |
2465 | 2468 | | |
2466 | | - | |
2467 | | - | |
2468 | | - | |
2469 | | - | |
| 2469 | + | |
2470 | 2470 | | |
2471 | 2471 | | |
2472 | 2472 | | |
| |||
2499 | 2499 | | |
2500 | 2500 | | |
2501 | 2501 | | |
2502 | | - | |
| 2502 | + | |
2503 | 2503 | | |
2504 | 2504 | | |
2505 | 2505 | | |
| |||
| 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