Skip to content

Commit 29beb96

Browse files
committed
fix: always delete temporary directory even during exception
1 parent c02d74b commit 29beb96

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/sagemaker/utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def create_tar_file(source_files, target=None):
361361
def _tmpdir(suffix="", prefix="tmp", directory=None):
362362
"""Create a temporary directory with a context manager.
363363
364-
The file is deleted when the context exits.
364+
The file is deleted when the context exits, even when there's an exception.
365365
The prefix, suffix, and dir arguments are the same as for mkstemp().
366366
367367
Args:
@@ -381,8 +381,10 @@ def _tmpdir(suffix="", prefix="tmp", directory=None):
381381
f"directory does not exist: '{directory}'"
382382
)
383383
tmp = tempfile.mkdtemp(suffix=suffix, prefix=prefix, dir=directory)
384-
yield tmp
385-
shutil.rmtree(tmp)
384+
try:
385+
yield tmp
386+
finally:
387+
shutil.rmtree(tmp)
386388

387389

388390
def repack_model(

0 commit comments

Comments
 (0)