From 46a1f6b0af8d88d1e97e844847436b6879e0a534 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Thu, 12 Nov 2026 10:58:50 +0000 Subject: [PATCH] fix(campaign): treat Slurm's default TMPDIR=/tmp as unset in cache_root scripts Slurm sets TMPDIR to its TmpFS (/tmp) when the submit environment has none, so the fill-if-unset TMPDIR never fired (RAL probe job 415134). Refs #1769 Co-Authored-By: Claude Opus 5.5 --- autofit/campaign/scheduler/slurm.py | 17 ++++++++++++----- test_autofit/campaign/test_slurm.py | 9 ++++++--- test_autofit/campaign/test_submit.py | 3 ++- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/autofit/campaign/scheduler/slurm.py b/autofit/campaign/scheduler/slurm.py index 416fb6120..bddf3957b 100644 --- a/autofit/campaign/scheduler/slurm.py +++ b/autofit/campaign/scheduler/slurm.py @@ -71,16 +71,23 @@ def _cache_exports(cache_root: str) -> List[str]: Fill-if-unset exports sending temp files and caches under ``cache_root``. A value already in the job's environment (for example from the submitting shell) - wins. ``JAX_COMPILATION_CACHE_DIR`` uses ``${VAR-...}`` so an explicitly empty value - still disables the JAX cache. A stage's own ``env`` is exported afterwards and so - overrides these. + wins, with one exception: ``TMPDIR=/tmp`` is replaced too, because Slurm sets + ``TMPDIR`` to its ``TmpFS`` (``/tmp``) itself when the submit environment has none, + so a plain fill-if-unset would never fire. ``JAX_COMPILATION_CACHE_DIR`` uses + ``${VAR-...}`` so an explicitly empty value still disables the JAX cache. A stage's + own ``env`` is exported afterwards and so overrides these. """ root = cache_root.rstrip("/") or "/" lines = [] for key, relative in CACHE_DIRS: - operator = "-" if key == "JAX_COMPILATION_CACHE_DIR" else ":-" - # The default sits inside "${...}", so escape for a double-quoted context. + # The default sits inside double quotes, so escape for that context. path = re.sub(r'([\\"$`])', r"\\\1", f"{root}/{relative}") + if key == "TMPDIR": + lines.append( + f'if [ -z "${{TMPDIR:-}}" ] || [ "$TMPDIR" = /tmp ]; then export TMPDIR="{path}"; fi' + ) + continue + operator = "-" if key == "JAX_COMPILATION_CACHE_DIR" else ":-" lines.append(f'export {key}="${{{key}{operator}{path}}}"') lines.append('mkdir -p "$TMPDIR" "$XDG_CACHE_HOME" 2>/dev/null || true') return lines diff --git a/test_autofit/campaign/test_slurm.py b/test_autofit/campaign/test_slurm.py index fc1b9a28c..62b056bd8 100644 --- a/test_autofit/campaign/test_slurm.py +++ b/test_autofit/campaign/test_slurm.py @@ -89,7 +89,7 @@ def test_sbatch_script_cache_root_exports_fill_if_unset(): request.cache_root = "/mnt/ral/x/" lines = sbatch_script(request).splitlines() for line in [ - 'export TMPDIR="${TMPDIR:-/mnt/ral/x/tmp}"', + 'if [ -z "${TMPDIR:-}" ] || [ "$TMPDIR" = /tmp ]; then export TMPDIR="/mnt/ral/x/tmp"; fi', 'export XDG_CACHE_HOME="${XDG_CACHE_HOME:-/mnt/ral/x/.cache}"', 'export NUMBA_CACHE_DIR="${NUMBA_CACHE_DIR:-/mnt/ral/x/.cache/numba}"', # "-" not ":-": an explicitly empty value still disables the JAX cache. @@ -98,8 +98,8 @@ def test_sbatch_script_cache_root_exports_fill_if_unset(): ]: assert line in lines, line # After `set -euo pipefail` and before the stage env, so a stage env value wins. - assert lines.index("set -euo pipefail") < lines.index('export TMPDIR="${TMPDIR:-/mnt/ral/x/tmp}"') - assert lines.index('export TMPDIR="${TMPDIR:-/mnt/ral/x/tmp}"') < lines.index("export NAME='a b'") + assert lines.index("set -euo pipefail") < lines.index('if [ -z "${TMPDIR:-}" ] || [ "$TMPDIR" = /tmp ]; then export TMPDIR="/mnt/ral/x/tmp"; fi') + assert lines.index('if [ -z "${TMPDIR:-}" ] || [ "$TMPDIR" = /tmp ]; then export TMPDIR="/mnt/ral/x/tmp"; fi') < lines.index("export NAME='a b'") def _cache_env(tmp_path, cache_root, environment): @@ -130,6 +130,9 @@ def test_sbatch_script_cache_block_runs_under_bash(tmp_path): tmp_path, root, {"PATH": "/usr/bin:/bin", "TMPDIR": "/scratch", "JAX_COMPILATION_CACHE_DIR": ""} ) assert (tmpdir, jax) == ("/scratch", "") + # Slurm sets TMPDIR=/tmp itself when the submit environment has none: replace it. + tmpdir, _ = _cache_env(tmp_path, root, {"PATH": "/usr/bin:/bin", "TMPDIR": "/tmp"}) + assert tmpdir == f"{root}/tmp" def _run_generated_script(tmp_path, fit_body, signal_usr1): diff --git a/test_autofit/campaign/test_submit.py b/test_autofit/campaign/test_submit.py index 5ffd4cb53..f6f0d035d 100644 --- a/test_autofit/campaign/test_submit.py +++ b/test_autofit/campaign/test_submit.py @@ -78,4 +78,5 @@ def test_dry_run_carries_cache_root(tmp_path): path = write_campaign(tmp_path / "p", CAMPAIGN.replace("site: ral\n", "site: ral\ncache_root: /mnt/ral/x\n")) entries = submit(Campaign.load(path), None, dry_run=True) - assert 'export TMPDIR="${TMPDIR:-/mnt/ral/x/tmp}"' in entries[0]["script"].splitlines() + assert 'XDG_CACHE_HOME="${XDG_CACHE_HOME:-/mnt/ral/x/.cache}"' in entries[0]["script"] + assert 'export TMPDIR="/mnt/ral/x/tmp"' in entries[0]["script"]