Skip to content

Commit ccc39fe

Browse files
fix: using cached version for remove lock on exit (#3709)
* feat: using cached for removing lock file when exiting * test: adding tests * chore: adding changelog file 3709.fixed.md [dependabot-skip] * fix: vulnerabilities warning * fix: warnings import * chore: revert "fix: warnings import" This reverts commit 5593e6b. * fix: warnings import --------- Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent 521ee2f commit ccc39fe

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

doc/changelog.d/3709.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fix: using cached version for remove lock on exit

src/ansys/mapdl/core/mapdl_grpc.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ def _exit_mapdl(self, path: str = None) -> None:
11971197

11981198
self._close_process()
11991199

1200-
self._remove_lock_file(path)
1200+
self._remove_lock_file(path, use_cached=True)
12011201
else:
12021202
self._exit_mapdl_server()
12031203

@@ -1360,17 +1360,24 @@ def _cache_pids(self):
13601360

13611361
self._log.debug(f"Recaching PIDs: {self._pids}")
13621362

1363-
def _remove_lock_file(self, mapdl_path=None):
1363+
def _remove_lock_file(
1364+
self, mapdl_path: str = None, jobname: str = None, use_cached: bool = False
1365+
):
13641366
"""Removes the lock file.
13651367
13661368
Necessary to call this as a segfault of MAPDL or exit(0) will
13671369
not remove the lock file.
13681370
"""
1371+
if jobname is None and use_cached:
1372+
jobname = self._jobname
1373+
elif jobname is None:
1374+
jobname = self.jobname
1375+
13691376
self._log.debug("Removing lock file after exit.")
13701377
if mapdl_path is None: # pragma: no cover
13711378
mapdl_path = self.directory
13721379
if mapdl_path:
1373-
for lockname in [self.jobname + ".lock", "file.lock"]:
1380+
for lockname in [jobname + ".lock", "file.lock"]:
13741381
lock_file = os.path.join(mapdl_path, lockname)
13751382
if os.path.isfile(lock_file):
13761383
try:
@@ -3805,7 +3812,7 @@ def kill_job(self, jobid: int) -> None:
38053812
"""
38063813
cmd = ["scancel", f"{jobid}"]
38073814
# to ensure the job is stopped properly, let's issue the scancel twice.
3808-
subprocess.Popen(cmd)
3815+
subprocess.Popen(cmd) # nosec B603
38093816

38103817
def __del__(self):
38113818
"""In case the object is deleted"""
@@ -3825,6 +3832,6 @@ def __del__(self):
38253832
if not self._start_instance:
38263833
return
38273834

3828-
except Exception as e:
3835+
except Exception as e: # nosec B110
38293836
# This is on clean up.
38303837
pass

tests/test_mapdl.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,13 +1723,25 @@ def test_mode(mapdl, cleared):
17231723
mapdl._mode = "grpc" # Going back to default
17241724

17251725

1726-
def test_remove_lock_file(mapdl, cleared, tmpdir):
1726+
@pytest.mark.parametrize("use_cached", (True, False))
1727+
def test_remove_lock_file(mapdl, cleared, tmpdir, use_cached):
17271728
tmpdir_ = tmpdir.mkdir("ansys")
17281729
lock_file = tmpdir_.join("file.lock")
17291730
with open(lock_file, "w") as fid:
17301731
fid.write("test")
17311732

1732-
mapdl._remove_lock_file(tmpdir_)
1733+
with patch(
1734+
"ansys.mapdl.core.mapdl_grpc.MapdlGrpc.jobname", new_callable=PropertyMock
1735+
) as mock_jb:
1736+
mock_jb.return_value = mapdl._jobname
1737+
1738+
mapdl._remove_lock_file(tmpdir_, use_cached=use_cached)
1739+
1740+
if use_cached:
1741+
mock_jb.assert_not_called()
1742+
else:
1743+
mock_jb.assert_called()
1744+
17331745
assert not os.path.exists(lock_file)
17341746

17351747

0 commit comments

Comments
 (0)