Skip to content

Commit

Permalink
fix: exiting earlier to avoid exceptions from gRPC calls (#3463)
Browse files Browse the repository at this point in the history
* feat: adding logging and avoid recursively trying to exit even if we have already tried.

* feat: adding logging and avoid recursively trying to exit even if we have already tried.

* feat: avoid issuing gRPC calls when exiting. Added more logging while exiting.

* chore: adding changelog file 3463.fixed.md

* fix: test

---------

Co-authored-by: pyansys-ci-bot <[email protected]>
  • Loading branch information
germa89 and pyansys-ci-bot authored Oct 7, 2024
1 parent af3566d commit 05128b6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/3463.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix: exiting earlier to avoid exceptions from gRPC calls
3 changes: 3 additions & 0 deletions src/ansys/mapdl/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ def wrapper(*args, **kwargs):
except grpc.RpcError as error:

mapdl = retrieve_mapdl_from_args(args)
mapdl._log.debug("A gRPC error has been detected.")

i_attemps += 1
if i_attemps <= n_attempts:
Expand Down Expand Up @@ -443,9 +444,11 @@ def handle_generic_grpc_error(error, func, args, kwargs, reason="", suggestion="
else:
# Making sure we do not keep executing gRPC calls.
mapdl._exited = True
mapdl._exiting = True

# Must close unfinished processes
mapdl._close_process()
mapdl._exiting = False
raise MapdlExitedError(msg)


Expand Down
27 changes: 25 additions & 2 deletions src/ansys/mapdl/core/mapdl_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,14 +1061,19 @@ def exit(self, save=False, force=False, **kwargs):
>>> mapdl.exit()
"""
# check if permitted to start (and hence exit) instances
self._log.debug(
f"Exiting MAPLD gRPC instance {self.ip}:{self.port} on '{self._path}'."
)

if self._exited is None:
self._log.debug("'self._exited' is none.")
return # Some edge cases the class object is not completely initialized but the __del__ method
# is called when exiting python. So, early exit here instead an error in the following
# self.directory command.
# See issue #1796
elif self._exited:
# Already exited.
self._log.debug("Already exited")
return

if save:
Expand All @@ -1091,10 +1096,9 @@ def exit(self, save=False, force=False, **kwargs):
return

self._exiting = True
self._log.debug("Exiting MAPDL")

if not kwargs.pop("fake_exit", False):
# This cannot should not be faked
# This cannot/should not be faked
if self._local:
mapdl_path = self.directory
self._cache_pids() # Recache processes
Expand Down Expand Up @@ -1152,6 +1156,10 @@ def _kill_server(self):
a local process.
"""
if self._exited:
self._log.debug("MAPDL server already exited")
return

try:
self._log.debug("Killing MAPDL server")
except ValueError:
Expand Down Expand Up @@ -1233,6 +1241,7 @@ def _close_process(self, timeout=2): # pragma: no cover
if self.is_alive:
raise MapdlRuntimeError("MAPDL could not be exited.")
else:
self._log.debug("All MAPDL processes exited")
self._exited = True

def _cache_pids(self):
Expand All @@ -1243,6 +1252,7 @@ def _cache_pids(self):
processes.
"""
self._log.debug("Caching PIDs")
self._pids = []

for filename in self.list_files():
Expand All @@ -1264,11 +1274,15 @@ def _cache_pids(self):
try:
parent = psutil.Process(parent_pid)
except psutil.NoSuchProcess:
self._log.debug(f"Parent process does not exist.")
return

children = parent.children(recursive=True)

self._pids = [parent_pid] + [each.pid for each in children]

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

def _remove_lock_file(self, mapdl_path=None):
"""Removes the lock file.
Expand Down Expand Up @@ -2625,10 +2639,16 @@ def is_alive(self) -> bool:
if self._exited:
self._log.debug("MAPDL instance is not alive because it is exited.")
return False

if self.busy:
self._log.debug("MAPDL instance is alive because it is busy.")
return True

if self._exiting:
# It should be exiting so we should not issue gRPC calls
self._log.debug("MAPDL instance is expected to be exiting")
return False

try:
check = bool(self._ctrl("VERSION"))
if check:
Expand All @@ -2642,6 +2662,9 @@ def is_alive(self) -> bool:
return check

except Exception as error:
if self._exited:
return False

self._log.debug(
f"MAPDL instance is not alive because retrieving version failed with:\n{error}"
)
Expand Down

0 comments on commit 05128b6

Please sign in to comment.