Skip to content

Commit 50b52b4

Browse files
committed
fix: local mode deletion of temp files
1 parent 7268e82 commit 50b52b4

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/sagemaker/local/utils.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414
from __future__ import absolute_import
1515

1616
import os
17+
import logging
1718
import shutil
1819
import subprocess
20+
import errno
1921

2022
from distutils.dir_util import copy_tree
2123
from six.moves.urllib.parse import urlparse
2224

2325
from sagemaker import s3
2426

27+
logger = logging.getLogger(__name__)
28+
2529

2630
def copy_directory_structure(destination_directory, relative_path):
2731
"""Creates intermediate directory structure for relative_path.
@@ -74,7 +78,19 @@ def move_to_destination(source, destination, job_name, sagemaker_session):
7478
else:
7579
raise ValueError("Invalid destination URI, must be s3:// or file://, got: %s" % destination)
7680

77-
shutil.rmtree(source)
81+
try:
82+
shutil.rmtree(source)
83+
except OSError as exc:
84+
# on Linux, when docker writes to any mounted volume, it uses the container's user. In most
85+
# cases this is root. When the container exits and we try to delete them we can't because
86+
# root owns those files. We expect this to happen, so we handle EACCESS. Any other error
87+
# we will raise the exception up.
88+
if exc.errno == errno.EACCES:
89+
logger.warning("Failed to delete: %s Please remove it manually.", source)
90+
else:
91+
logger.error("Failed to delete: %s", source)
92+
raise
93+
7894
return final_uri
7995

8096

tests/unit/test_local_utils.py

+15
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# language governing permissions and limitations under the License.
1313
from __future__ import absolute_import
1414

15+
import errno
1516
import pytest
1617
from mock import patch, Mock
1718

@@ -52,3 +53,17 @@ def test_move_to_destination_s3(recursive_copy):
5253
def test_move_to_destination_illegal_destination():
5354
with pytest.raises(ValueError):
5455
sagemaker.local.utils.move_to_destination("/tmp/data", "ftp://ftp/in/2018", "job", None)
56+
57+
58+
def raise_os_error(args):
59+
err = OSError()
60+
err.errno = errno.EACCES
61+
raise err
62+
63+
64+
@patch("shutil.rmtree", side_effect=raise_os_error)
65+
@patch("sagemaker.local.utils.recursive_copy")
66+
def test_move_to_destination_local_root_failure(recursive_copy, mock_rmtree):
67+
# This should not raise, in case root owns files, make sure it doesn't
68+
sagemaker.local.utils.move_to_destination("/tmp/data", "file:///target/dir/", "job", None)
69+
recursive_copy.assert_called_with("/tmp/data", "/target/dir/")

0 commit comments

Comments
 (0)