Skip to content

Commit 13ee36e

Browse files
evakravidoddaspk-amzn
authored andcommitted
fix: always delete temporary directory even during exception (aws#3751)
1 parent ad4efb1 commit 13ee36e

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
@@ -429,7 +429,7 @@ def create_tar_file(source_files, target=None):
429429
def _tmpdir(suffix="", prefix="tmp", directory=None):
430430
"""Create a temporary directory with a context manager.
431431
432-
The file is deleted when the context exits.
432+
The file is deleted when the context exits, even when there's an exception.
433433
The prefix, suffix, and dir arguments are the same as for mkstemp().
434434
435435
Args:
@@ -449,8 +449,10 @@ def _tmpdir(suffix="", prefix="tmp", directory=None):
449449
f"directory does not exist: '{directory}'"
450450
)
451451
tmp = tempfile.mkdtemp(suffix=suffix, prefix=prefix, dir=directory)
452-
yield tmp
453-
shutil.rmtree(tmp)
452+
try:
453+
yield tmp
454+
finally:
455+
shutil.rmtree(tmp)
454456

455457

456458
def repack_model(

0 commit comments

Comments
 (0)