From 9f5deeeedd4b9d28efef00f413329ab6ff9d46ac Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 27 Jan 2025 15:51:38 +0100 Subject: [PATCH 1/7] feat: using cached for removing lock file when exiting --- src/ansys/mapdl/core/mapdl_grpc.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index 1274b850e0..76249a1041 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: From c3e03336f67837800cfc5dccba7f7696c845ed6a Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 27 Jan 2025 16:02:58 +0100 Subject: [PATCH 2/7] test: adding tests --- tests/test_mapdl.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/test_mapdl.py b/tests/test_mapdl.py index 7d902e099a..2079303187 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) From 1a2724cf98896c5b675e7607d523472433cda3f8 Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Mon, 27 Jan 2025 15:09:09 +0000 Subject: [PATCH 3/7] chore: adding changelog file 3709.fixed.md [dependabot-skip] --- doc/changelog.d/3709.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/3709.fixed.md diff --git a/doc/changelog.d/3709.fixed.md b/doc/changelog.d/3709.fixed.md new file mode 100644 index 0000000000..0afc2fc81f --- /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 From 3a030e3319bd266dca9875c1da7a85bac21149b6 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 27 Jan 2025 16:20:50 +0100 Subject: [PATCH 4/7] fix: vulnerabilities warning --- src/ansys/mapdl/core/mapdl_grpc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index 76249a1041..1a2b2fca3c 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -3812,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""" @@ -3832,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 From 26399f93601e35465c7d5cfe1e8d2cb53878f908 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 27 Jan 2025 15:02:44 +0100 Subject: [PATCH 5/7] fix: warnings import --- tests/common.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/common.py b/tests/common.py index 142e0a681a..9a7581f843 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 From a827655a4661848b188ed0bac4ff784f52a1c21f Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 27 Jan 2025 17:17:14 +0100 Subject: [PATCH 6/7] chore: revert "fix: warnings import" This reverts commit 5593e6b8abb38785ad94f4b652cea2c15975d6ba. --- tests/common.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/common.py b/tests/common.py index 9a7581f843..142e0a681a 100644 --- a/tests/common.py +++ b/tests/common.py @@ -26,7 +26,6 @@ import subprocess import time from typing import Dict, List -from warnings import warn import psutil From 941d6c0511e37b1389a0986436b0cf241cabe426 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 27 Jan 2025 15:02:44 +0100 Subject: [PATCH 7/7] fix: warnings import --- tests/common.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/common.py b/tests/common.py index 142e0a681a..9a7581f843 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