Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: using cached version for remove lock on exit #3709

Merged
merged 7 commits into from
Jan 27, 2025
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
1 change: 1 addition & 0 deletions doc/changelog.d/3709.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix: using cached version for remove lock on exit
17 changes: 12 additions & 5 deletions src/ansys/mapdl/core/mapdl_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@

self._close_process()

self._remove_lock_file(path)
self._remove_lock_file(path, use_cached=True)

Check warning on line 1200 in src/ansys/mapdl/core/mapdl_grpc.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_grpc.py#L1200

Added line #L1200 was not covered by tests
else:
self._exit_mapdl_server()

Expand Down Expand Up @@ -1360,17 +1360,24 @@

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:
Expand Down Expand Up @@ -3805,7 +3812,7 @@
"""
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

Check warning on line 3815 in src/ansys/mapdl/core/mapdl_grpc.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_grpc.py#L3815

Added line #L3815 was not covered by tests

def __del__(self):
"""In case the object is deleted"""
Expand All @@ -3825,6 +3832,6 @@
if not self._start_instance:
return

except Exception as e:
except Exception as e: # nosec B110

Check warning on line 3835 in src/ansys/mapdl/core/mapdl_grpc.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_grpc.py#L3835

Added line #L3835 was not covered by tests
# This is on clean up.
pass
1 change: 1 addition & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import subprocess
import time
from typing import Dict, List
from warnings import warn

import psutil

Expand Down
16 changes: 14 additions & 2 deletions tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
Loading