From 9c20cad40e4142f8a5e945fe26e919e598f2bd56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20W=C3=A4lchli?= Date: Thu, 3 Nov 2022 03:14:15 +0100 Subject: [PATCH] Fix srun detection causing permission error on non-SLURM platforms (#15485) * improve srun detection * changelog * try catch is obsolete Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- src/lightning_lite/CHANGELOG.md | 2 +- src/lightning_lite/plugins/environments/slurm.py | 8 +++----- tests/tests_lite/plugins/environments/test_slurm.py | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/lightning_lite/CHANGELOG.md b/src/lightning_lite/CHANGELOG.md index 6211a86942..a4c4bd2698 100644 --- a/src/lightning_lite/CHANGELOG.md +++ b/src/lightning_lite/CHANGELOG.md @@ -48,7 +48,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). ### Fixed -- +- Fix an issue with the SLURM `srun` detection causing permission errors ([#15485](https://github.com/Lightning-AI/lightning/issues/15485)) - diff --git a/src/lightning_lite/plugins/environments/slurm.py b/src/lightning_lite/plugins/environments/slurm.py index 18ba1cafe7..b7a0a1247b 100644 --- a/src/lightning_lite/plugins/environments/slurm.py +++ b/src/lightning_lite/plugins/environments/slurm.py @@ -15,8 +15,8 @@ import logging import os import re +import shutil import signal -import subprocess import sys from typing import Optional @@ -160,10 +160,8 @@ class SLURMEnvironment(ClusterEnvironment): """ if _IS_WINDOWS: return - try: - srun_exists = subprocess.call(["command", "-v", "srun"]) == 0 - except FileNotFoundError: - srun_exists = False + + srun_exists = shutil.which("srun") is not None if srun_exists and not _is_srun_used(): hint = " ".join(["srun", os.path.basename(sys.executable), *sys.argv])[:64] rank_zero_warn( diff --git a/tests/tests_lite/plugins/environments/test_slurm.py b/tests/tests_lite/plugins/environments/test_slurm.py index 281e237d36..768e1f468d 100644 --- a/tests/tests_lite/plugins/environments/test_slurm.py +++ b/tests/tests_lite/plugins/environments/test_slurm.py @@ -126,7 +126,7 @@ def test_srun_available_and_not_used(monkeypatch): expected = "`srun` .* available .* but is not used. HINT: .* srun python train.py --lr 0.01" # pretend `srun` is available - with mock.patch("lightning_lite.plugins.environments.slurm.subprocess.call", return_value=0): + with mock.patch("lightning_lite.plugins.environments.slurm.shutil.which", return_value="/usr/bin/srun"): with pytest.warns(PossibleUserWarning, match=expected): SLURMEnvironment()