Skip to content

Commit e99d7d6

Browse files
mohamedzeidan2021Mohamed Zeidan
andauthored
fix: write repack launcher with LF endings so pipelines built on Windows work (#6313)
_RepackModelStep._inject_repack_script_and_launcher wrote _repack_script_launcher.sh with text-mode open(), which on Windows translates LF to CRLF. The CRLF bash launcher then fails in the Linux repack container (model.tar.gz\\r / #15 corruption). Open both launcher writes (S3 and local source_dir branches) with newline="\n" so LF is preserved on every host. Fixes #3762 Co-authored-by: Mohamed Zeidan <zeidmo@amazon.com>
1 parent e69cc40 commit e99d7d6

2 files changed

Lines changed: 117 additions & 2 deletions

File tree

‎sagemaker-mlops/src/sagemaker/mlops/workflow/_utils.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,10 @@ def _inject_repack_script_and_launcher(self):
307307
custom_extractall_tarfile(t, targz_contents_dir)
308308

309309
shutil.copy2(fname, os.path.join(targz_contents_dir, REPACK_SCRIPT))
310+
# newline="\n" forces LF endings so the bash launcher stays runnable in the
311+
# Linux repack container even when written from a Windows host (see issue #3762).
310312
with open(
311-
os.path.join(targz_contents_dir, REPACK_SCRIPT_LAUNCHER), "w"
313+
os.path.join(targz_contents_dir, REPACK_SCRIPT_LAUNCHER), "w", newline="\n"
312314
) as launcher_file:
313315
launcher_file.write(LAUNCH_REPACK_SCRIPT_CMD)
314316

@@ -319,7 +321,11 @@ def _inject_repack_script_and_launcher(self):
319321
_save_model(self._source_dir, new_targz_path, self.sagemaker_session, kms_key=None)
320322
else:
321323
shutil.copy2(fname, os.path.join(self._source_dir, REPACK_SCRIPT))
322-
with open(os.path.join(self._source_dir, REPACK_SCRIPT_LAUNCHER), "w") as launcher_file:
324+
# newline="\n" forces LF endings so the bash launcher stays runnable in the
325+
# Linux repack container even when written from a Windows host (see issue #3762).
326+
with open(
327+
os.path.join(self._source_dir, REPACK_SCRIPT_LAUNCHER), "w", newline="\n"
328+
) as launcher_file:
323329
launcher_file.write(LAUNCH_REPACK_SCRIPT_CMD)
324330

325331
@property

‎sagemaker-mlops/tests/unit/workflow/test_utils.py‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import pytest
1818
import os
19+
import tarfile
1920
import tempfile
2021
from unittest.mock import Mock, patch
2122

@@ -318,6 +319,114 @@ def test_inject_repack_script_local_source_dir(self, mock_session, temp_entry_po
318319
assert "#!/bin/bash" in content
319320
assert "python _repack_model.py" in content
320321

322+
# Regression for issue #3762: the launcher is a bash script that runs
323+
# in a Linux container, so it must use LF endings. NOTE: this raw-bytes
324+
# check only actually catches the bug on a Windows host (on Linux/mac the
325+
# old code already emitted LF); the host-independent guard is
326+
# test_inject_repack_launcher_opened_with_lf_newline below.
327+
with open(launcher_path, "rb") as f:
328+
raw = f.read()
329+
assert b"\r\n" not in raw
330+
331+
def test_inject_repack_launcher_opened_with_lf_newline(self, mock_session, temp_entry_point):
332+
"""Regression for #3762: launcher must be opened with newline="\\n".
333+
334+
On Windows, text-mode ``open(..., "w")`` translates ``\\n`` to ``\\r\\n``, which
335+
corrupts the bash launcher when it later runs in the Linux repack container. This
336+
asserts the fix (``newline="\\n"``) is in place independently of the test host OS.
337+
"""
338+
real_open = open
339+
launcher_open_calls = []
340+
341+
def tracking_open(file, mode="r", *args, **kwargs):
342+
if str(file).endswith(REPACK_SCRIPT_LAUNCHER) and "w" in mode:
343+
launcher_open_calls.append(kwargs.get("newline"))
344+
return real_open(file, mode, *args, **kwargs)
345+
346+
with tempfile.TemporaryDirectory() as temp_dir:
347+
with patch("sagemaker.mlops.workflow._utils.image_uris.retrieve") as mock_retrieve:
348+
with patch("sagemaker.train.ModelTrainer") as mock_trainer:
349+
with patch(
350+
"sagemaker.mlops.workflow._utils.TrainingStep.__init__"
351+
) as mock_super:
352+
with patch("builtins.open", side_effect=tracking_open):
353+
mock_retrieve.return_value = "sklearn-image:latest"
354+
mock_trainer_instance = Mock()
355+
mock_trainer_instance.train = Mock(return_value=Mock())
356+
mock_trainer.return_value = mock_trainer_instance
357+
mock_super.return_value = None
358+
359+
_RepackModelStep(
360+
name="repack-step",
361+
sagemaker_session=mock_session,
362+
role="arn:aws:iam::123456789012:role/SageMakerRole",
363+
model_data="s3://bucket/model.tar.gz",
364+
entry_point=temp_entry_point,
365+
source_dir=temp_dir,
366+
)
367+
368+
assert launcher_open_calls, "launcher file was never opened for writing"
369+
assert all(nl == "\n" for nl in launcher_open_calls), (
370+
"launcher must be opened with newline='\\n' to keep LF endings, "
371+
f"got {launcher_open_calls}"
372+
)
373+
374+
def test_inject_repack_launcher_opened_with_lf_newline_s3_source_dir(
375+
self, mock_session, temp_entry_point
376+
):
377+
"""Regression for #3762 on the S3 source_dir branch.
378+
379+
The S3 path downloads, extracts, and rewrites the source tarball; the launcher it
380+
writes into the extracted dir must also use ``newline="\\n"``. Mirrors
381+
``test_inject_repack_launcher_opened_with_lf_newline`` for the other code branch.
382+
"""
383+
real_open = open
384+
launcher_open_calls = []
385+
386+
def tracking_open(file, mode="r", *args, **kwargs):
387+
if str(file).endswith(REPACK_SCRIPT_LAUNCHER) and "w" in mode:
388+
launcher_open_calls.append(kwargs.get("newline"))
389+
return real_open(file, mode, *args, **kwargs)
390+
391+
def fake_download(source_dir, dest_path, session):
392+
# Write a minimal valid .tar.gz so the real tarfile extraction succeeds.
393+
with tempfile.TemporaryDirectory() as seed:
394+
seed_file = os.path.join(seed, "dummy.txt")
395+
with real_open(seed_file, "w") as fh:
396+
fh.write("seed")
397+
with tarfile.open(dest_path, "w:gz") as t:
398+
t.add(seed_file, arcname="dummy.txt")
399+
400+
with patch("sagemaker.mlops.workflow._utils.image_uris.retrieve") as mock_retrieve:
401+
with patch("sagemaker.train.ModelTrainer") as mock_trainer:
402+
with patch("sagemaker.mlops.workflow._utils.TrainingStep.__init__") as mock_super:
403+
with patch(
404+
"sagemaker.mlops.workflow._utils.download_file_from_url",
405+
side_effect=fake_download,
406+
):
407+
with patch("sagemaker.mlops.workflow._utils._save_model"):
408+
with patch("builtins.open", side_effect=tracking_open):
409+
mock_retrieve.return_value = "sklearn-image:latest"
410+
mock_trainer_instance = Mock()
411+
mock_trainer_instance.train = Mock(return_value=Mock())
412+
mock_trainer.return_value = mock_trainer_instance
413+
mock_super.return_value = None
414+
415+
_RepackModelStep(
416+
name="repack-step",
417+
sagemaker_session=mock_session,
418+
role="arn:aws:iam::123456789012:role/SageMakerRole",
419+
model_data="s3://bucket/model.tar.gz",
420+
entry_point=temp_entry_point,
421+
source_dir="s3://bucket/source.tar.gz",
422+
)
423+
424+
assert launcher_open_calls, "launcher file was never opened for writing (S3 branch)"
425+
assert all(nl == "\n" for nl in launcher_open_calls), (
426+
"launcher must be opened with newline='\\n' to keep LF endings, "
427+
f"got {launcher_open_calls}"
428+
)
429+
321430
def test_properties_returns_parent_properties(self, mock_session, temp_entry_point):
322431
"""Test properties returns parent class properties."""
323432
with patch("sagemaker.mlops.workflow._utils.image_uris.retrieve") as mock_retrieve:

0 commit comments

Comments
 (0)