diff --git a/doc/changelog.d/3709.fixed.md b/doc/changelog.d/3709.fixed.md new file mode 100644 index 00000000000..0afc2fc81f6 --- /dev/null +++ b/doc/changelog.d/3709.fixed.md @@ -0,0 +1 @@ +fix: using cached version for remove lock on exit \ No newline at end of file diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index 1274b850e0f..1a2b2fca3cf 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -1197,7 +1197,7 @@ def _exit_mapdl(self, path: str = None) -> None: self._close_process() - self._remove_lock_file(path) + self._remove_lock_file(path, use_cached=True) else: self._exit_mapdl_server() @@ -1360,17 +1360,24 @@ def _cache_pids(self): self._log.debug(f"Recaching PIDs: {self._pids}") - def _remove_lock_file(self, mapdl_path=None): + def _remove_lock_file( + self, mapdl_path: str = None, jobname: str = None, use_cached: bool = False + ): """Removes the lock file. Necessary to call this as a segfault of MAPDL or exit(0) will not remove the lock file. """ + if jobname is None and use_cached: + jobname = self._jobname + elif jobname is None: + jobname = self.jobname + self._log.debug("Removing lock file after exit.") if mapdl_path is None: # pragma: no cover mapdl_path = self.directory if mapdl_path: - for lockname in [self.jobname + ".lock", "file.lock"]: + for lockname in [jobname + ".lock", "file.lock"]: lock_file = os.path.join(mapdl_path, lockname) if os.path.isfile(lock_file): try: @@ -3805,7 +3812,7 @@ def kill_job(self, jobid: int) -> None: """ cmd = ["scancel", f"{jobid}"] # to ensure the job is stopped properly, let's issue the scancel twice. - subprocess.Popen(cmd) + subprocess.Popen(cmd) # nosec B603 def __del__(self): """In case the object is deleted""" @@ -3825,6 +3832,6 @@ def __del__(self): if not self._start_instance: return - except Exception as e: + except Exception as e: # nosec B110 # This is on clean up. pass diff --git a/tests/common.py b/tests/common.py index 142e0a681a9..9a7581f843a 100644 --- a/tests/common.py +++ b/tests/common.py @@ -26,6 +26,7 @@ import subprocess import time from typing import Dict, List +from warnings import warn import psutil diff --git a/tests/test_mapdl.py b/tests/test_mapdl.py index 7d902e099ac..2079303187b 100644 --- a/tests/test_mapdl.py +++ b/tests/test_mapdl.py @@ -1723,13 +1723,25 @@ def test_mode(mapdl, cleared): mapdl._mode = "grpc" # Going back to default -def test_remove_lock_file(mapdl, cleared, tmpdir): +@pytest.mark.parametrize("use_cached", (True, False)) +def test_remove_lock_file(mapdl, cleared, tmpdir, use_cached): tmpdir_ = tmpdir.mkdir("ansys") lock_file = tmpdir_.join("file.lock") with open(lock_file, "w") as fid: fid.write("test") - mapdl._remove_lock_file(tmpdir_) + with patch( + "ansys.mapdl.core.mapdl_grpc.MapdlGrpc.jobname", new_callable=PropertyMock + ) as mock_jb: + mock_jb.return_value = mapdl._jobname + + mapdl._remove_lock_file(tmpdir_, use_cached=use_cached) + + if use_cached: + mock_jb.assert_not_called() + else: + mock_jb.assert_called() + assert not os.path.exists(lock_file)