Skip to content

Commit 5f91d52

Browse files
authored
Merge pull request #3474 from vkarak/bugfix/unset-sbatch-variables
[bugfix] Unset all `SBATCH_*` environment variables before submitting a job
2 parents fa96d82 + 2465b8a commit 5f91d52

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

docs/config_reference.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,17 @@ System Partition Configuration
336336
.. versionadded:: 4.4
337337
The ``ssh`` scheduler is added.
338338

339+
.. versionchanged:: 4.8.1
340+
All ``SBATCH_*`` variables are unset before submitting a job through the Slurm-based backends.
341+
See note below for information.
342+
343+
.. note::
344+
The Slurm-based backends unset all ``SBATCH_*`` environment variables before submitting a job.
345+
This is done to avoid environment variables bypassing ReFrame's configuration.
346+
For example, if the :attr:`~config.systems.partitions.access` options for a partition used ``-A foo`` and ``SBATCH_ACCOUNT=bar`` was set, then the environment variable would override the configuration, as ReFrame emits those (by default) in the submission script.
347+
348+
Job submission in ReFrame is controlled exclusively by the system partition's :attr:`~config.systems.partitions.access` options, the test job's :attr:`~reframe.core.schedulers.Job.options` and the :option:`-J` command-line option.
349+
339350
.. note::
340351

341352
The way that multiple node jobs are submitted using the SGE scheduler can be very site-specific.

reframe/core/schedulers/slurm.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import functools
77
import glob
88
import itertools
9+
import os
910
import re
1011
import shlex
1112
import time
@@ -260,17 +261,35 @@ def emit_preamble(self, job):
260261
# Filter out empty statements before returning
261262
return list(filter(None, preamble))
262263

264+
def sbatch(self, args):
265+
'''Run sbatch using a clean environment
266+
267+
We unset all `SBATCH_*` environment variables before submitting;
268+
submission should be controlled entirely by ReFrame through the
269+
partition's `access` options, the test's `job.options` and the
270+
command-line `-J` option
271+
'''
272+
273+
with rt.temp_environment():
274+
for var in [name for name in os.environ.keys()
275+
if name.startswith('SBATCH_')]:
276+
self.log(f'unsetting environment variable {var}',
277+
logging.DEBUG)
278+
del os.environ[var]
279+
280+
cmd = ' '.join(['sbatch'] + args)
281+
return _run_strict(cmd, timeout=self._submit_timeout)
282+
263283
def submit(self, job):
264-
cmd_parts = ['sbatch']
284+
sbatch_args = []
265285
if self._sched_access_in_submit:
266-
cmd_parts += job.sched_access
286+
sbatch_args += job.sched_access
267287

268-
cmd_parts += [job.script_filename]
269-
cmd = ' '.join(cmd_parts)
288+
sbatch_args += [job.script_filename]
270289
intervals = itertools.cycle([1, 2, 3])
271290
while True:
272291
try:
273-
completed = _run_strict(cmd, timeout=self._submit_timeout)
292+
completed = self.sbatch(sbatch_args)
274293
break
275294
except SpawnedProcessError as e:
276295
error_match = re.search(

unittests/test_environments.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ def test_temp_environment(base_environ, user_runtime, modules_system):
139139
assert not rt.is_env_loaded(environ)
140140

141141

142+
def test_temp_environment_as_snapshot(base_environ):
143+
with rt.temp_environment():
144+
del os.environ['_var0']
145+
os.environ['_var1'] = 'foo'
146+
147+
assert os.environ['_var0'] == 'val0'
148+
assert os.environ['_var1'] == 'val1'
149+
150+
142151
def test_env_load_already_present(base_environ, user_runtime,
143152
modules_system, env0):
144153
modules_system.load_module('testmod_boo')

0 commit comments

Comments
 (0)