Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions nemo_gym/sandbox/providers/apptainer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ apptainer:
start_timeout_s: 600
extra_start_args: []
apply_resource_limits: true
private_tmp: false
probe:
command: printf apptainer-sandbox-ready
expected_stdout: apptainer-sandbox-ready
Expand All @@ -109,6 +110,7 @@ Settings for starting the instance (`apptainer instance start`).
| `start_timeout_s` | `600` | Max seconds to wait for `instance start` (`None` = no timeout). |
| `extra_start_args` | `[]` | Extra raw flags appended to `instance start`. |
| `apply_resource_limits` | `true` | Add CPU/memory cgroup flags from `SandboxSpec.resources`. |
| `private_tmp` | `false` | Give each instance its own `/tmp` (`--no-mount tmp` plus a bind of a `tmp` dir inside the instance's staging dir) instead of sharing the host `/tmp` with every other sandbox on the node. |

### `exec` — `ApptainerExecConfig`

Expand Down
8 changes: 8 additions & 0 deletions nemo_gym/sandbox/providers/apptainer/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class ApptainerCreateConfig:
start_timeout_s: float | None = 600
extra_start_args: list[str] = field(default_factory=list)
apply_resource_limits: bool = True
# Give each instance its own /tmp (a ``tmp`` dir in its host staging dir) instead of Apptainer's
# default bind of the host /tmp, which is otherwise shared by every sandbox on the node.
private_tmp: bool = False

def __post_init__(self) -> None:
if self.start_timeout_s is not None and self.start_timeout_s <= 0:
Expand Down Expand Up @@ -445,6 +448,11 @@ async def create(self, spec: SandboxSpec) -> SandboxHandle:
# build the `apptainer instance start` command line.
argv: list[str] = [self._binary, "instance", "start"]
argv += ["--bind", f"{staging_dir}:{mount_point}"]
if self._create_config.private_tmp:
private_tmp_dir = staging_dir / "tmp"
private_tmp_dir.mkdir(exist_ok=True)
private_tmp_dir.chmod(0o1777)
argv += ["--no-mount", "tmp", "--bind", f"{private_tmp_dir}:/tmp"]
Comment thread
linj-glitch marked this conversation as resolved.
Resolved
for bind in self._exec_config.default_binds:
argv += ["--bind", bind]
for bind in extra_binds:
Expand Down
35 changes: 35 additions & 0 deletions tests/unit_tests/test_apptainer_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,41 @@ def responder(argv: list[str]) -> tuple[int, str, str]:
assert any("stop" in call["argv"] for call in rec.calls)


async def test_create_private_tmp_binds_per_instance_tmp(
fake_binary: str, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
staging = tmp_path / "staging"
monkeypatch.setattr(apptainer_provider.tempfile, "mkdtemp", lambda prefix: str(staging.mkdir() or staging))

def responder(argv: list[str]) -> tuple[int, str, str]:
return (0, apptainer_provider.READY_PROBE_EXPECTED, "") if "exec" in argv else (0, "", "")

provider, rec = _make_provider(monkeypatch, responder, create={"private_tmp": True})
await provider.create(SandboxSpec(image="ubuntu:22.04"))

start_argv = next(c["argv"] for c in rec.calls if "start" in c["argv"])
assert _contains_seq(start_argv, ["--no-mount", "tmp", "--bind", f"{staging / 'tmp'}:/tmp"])
assert (staging / "tmp").is_dir()
assert (staging / "tmp").stat().st_mode & 0o7777 == 0o1777


async def test_create_shares_host_tmp_by_default(
fake_binary: str, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
staging = tmp_path / "staging"
monkeypatch.setattr(apptainer_provider.tempfile, "mkdtemp", lambda prefix: str(staging.mkdir() or staging))

def responder(argv: list[str]) -> tuple[int, str, str]:
return (0, apptainer_provider.READY_PROBE_EXPECTED, "") if "exec" in argv else (0, "", "")

provider, rec = _make_provider(monkeypatch, responder)
await provider.create(SandboxSpec(image="ubuntu:22.04"))

start_argv = next(c["argv"] for c in rec.calls if "start" in c["argv"])
assert "--no-mount" not in start_argv
assert not (staging / "tmp").exists()


# --------------------------------------------------------------------------- #
# exec
# --------------------------------------------------------------------------- #
Expand Down
Loading