Skip to content

fix: always delete temporary directory even during exception #3751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 3, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/sagemaker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def create_tar_file(source_files, target=None):
def _tmpdir(suffix="", prefix="tmp", directory=None):
"""Create a temporary directory with a context manager.

The file is deleted when the context exits.
The file is deleted when the context exits, even when there's an exception.
The prefix, suffix, and dir arguments are the same as for mkstemp().

Args:
Expand All @@ -381,8 +381,10 @@ def _tmpdir(suffix="", prefix="tmp", directory=None):
f"directory does not exist: '{directory}'"
)
tmp = tempfile.mkdtemp(suffix=suffix, prefix=prefix, dir=directory)
yield tmp
shutil.rmtree(tmp)
try:
yield tmp
finally:
shutil.rmtree(tmp)


def repack_model(
Expand Down