Skip to content

Commit c3cdfa6

Browse files
committed
fix: local mode deletion of temp files
1 parent 6a782d9 commit c3cdfa6

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,13 +14,17 @@
1414
from __future__ import absolute_import
1515

1616
import os
17+
import logging
1718
import shutil
19+
import errno
1820

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

2224
from sagemaker import s3
2325

26+
logger = logging.getLogger(__name__)
27+
2428

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

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

7995

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)