Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 12 additions & 5 deletions src/cloudai/systems/slurm/single_sbatch_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,20 @@ def run(self):
self.scenario_root.mkdir(parents=True, exist_ok=True)
tr = self.test_scenario.test_runs[0]
job = self._submit_test(tr)
self.jobs.append(job)

if self.shutting_down:
self.system.kill(job)
Comment thread
podkidyshev marked this conversation as resolved.

is_completed = False
while not is_completed:
if self.shutting_down:
break
is_completed = True if self.mode == "dry-run" else self.system.is_job_completed(job)
time.sleep(self.system.monitor_interval)
try:
while not is_completed:
if self.shutting_down:
break
is_completed = True if self.mode == "dry-run" else self.system.is_job_completed(job)
time.sleep(self.system.monitor_interval)
finally:
self.jobs.remove(job)
Comment thread
coderabbitai[bot] marked this conversation as resolved.

self.handle_dse()

Expand Down
62 changes: 62 additions & 0 deletions tests/test_single_sbatch_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,68 @@ def _cmd_gen(_, tr: TestRun):
assert cleanup_calls == expected_paths


def test_run_tracks_and_cancels_job_on_shutdown(sleep_tr: TestRun, slurm_system: SlurmSystem) -> None:
tc = TestScenario(name="tc", test_runs=[sleep_tr])
runner = SingleSbatchRunner(mode="run", system=slurm_system, test_scenario=tc, output_path=slurm_system.output_path)
job = SlurmJob(sleep_tr, id=123)
runner._submit_test = Mock(return_value=job)
runner.handle_dse = Mock()
runner.on_job_completion = Mock()

def shutdown_during_monitoring(monitored_job: SlurmJob) -> bool:
assert monitored_job is job
assert runner.jobs == [job]
runner.shutdown()
return False

with (
patch.object(SlurmSystem, "is_job_completed", side_effect=shutdown_during_monitoring),
patch.object(SlurmSystem, "kill") as kill,
):
runner.run()

kill.assert_called_once_with(job)
assert runner.jobs == []
Comment thread
coderabbitai[bot] marked this conversation as resolved.


def test_run_cancels_job_when_shutdown_occurs_during_submission(sleep_tr: TestRun, slurm_system: SlurmSystem) -> None:
tc = TestScenario(name="tc", test_runs=[sleep_tr])
runner = SingleSbatchRunner(mode="run", system=slurm_system, test_scenario=tc, output_path=slurm_system.output_path)
job = SlurmJob(sleep_tr, id=123)
runner.handle_dse = Mock()
runner.on_job_completion = Mock()

def submit_after_shutdown(_: TestRun) -> SlurmJob:
runner.shutdown()
return job

runner._submit_test = Mock(side_effect=submit_after_shutdown)

with patch.object(SlurmSystem, "kill") as kill:
runner.run()

kill.assert_called_once_with(job)
assert runner.jobs == []


def test_run_removes_completed_job_from_tracking(sleep_tr: TestRun, slurm_system: SlurmSystem) -> None:
tc = TestScenario(name="tc", test_runs=[sleep_tr])
runner = SingleSbatchRunner(mode="run", system=slurm_system, test_scenario=tc, output_path=slurm_system.output_path)
job = SlurmJob(sleep_tr, id=123)
runner._submit_test = Mock(return_value=job)
runner.handle_dse = Mock()
runner.on_job_completion = Mock()

with (
patch.object(SlurmSystem, "is_job_completed", return_value=True),
patch.object(SlurmSystem, "kill") as kill,
):
runner.run()

kill.assert_not_called()
assert runner.jobs == []


def test_pre_test(nccl_tr: TestRun, sleep_tr: TestRun, slurm_system: SlurmSystem) -> None:
nccl_tr.pre_test = TestScenario(name="pre_test", test_runs=[sleep_tr])
tc = TestScenario(name="tc", test_runs=[nccl_tr])
Expand Down
Loading