From c349d02dbbe4f718c39308713745f184a8c3fd85 Mon Sep 17 00:00:00 2001 From: Mainak Kundu Date: Fri, 27 Sep 2024 16:57:06 -0400 Subject: [PATCH] fix: Session was not garbage collected after settings access --- src/ansys/fluent/core/session_solver.py | 7 ++++--- src/ansys/fluent/core/solver/flobject.py | 13 +++++++------ tests/test_fluent_session.py | 2 ++ 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/ansys/fluent/core/session_solver.py b/src/ansys/fluent/core/session_solver.py index 1f42b208d71..2f03d10f5ea 100644 --- a/src/ansys/fluent/core/session_solver.py +++ b/src/ansys/fluent/core/session_solver.py @@ -213,7 +213,8 @@ def workflow(self): ) return self._workflow - def _interrupt(self, command): + @classmethod + def _interrupt(cls, command): interruptible_commands = [ "solution/run-calculation/iterate", "solution/run-calculation/calculate", @@ -221,7 +222,7 @@ def _interrupt(self, command): ] if pyfluent.SUPPORT_SOLVER_INTERRUPT: if command.path in interruptible_commands: - self.settings.solution.run_calculation.interrupt() + command._root.solution.run_calculation.interrupt() @property def settings(self): @@ -230,7 +231,7 @@ def settings(self): self._settings_root = flobject.get_root( flproxy=self._settings_service, version=self._version, - interrupt=self._interrupt, + interrupt=Solver._interrupt, file_transfer_service=self._file_transfer_service, scheme_eval=self.scheme_eval.scheme_eval, ) diff --git a/src/ansys/fluent/core/solver/flobject.py b/src/ansys/fluent/core/solver/flobject.py index 47fb9987922..fd2ae6f6cfe 100644 --- a/src/ansys/fluent/core/solver/flobject.py +++ b/src/ansys/fluent/core/solver/flobject.py @@ -199,11 +199,12 @@ def __init__(self, name: str | None = None, parent=None): self._setattr("_name", name) self._setattr("_child_alias_objs", {}) - def _root(self, obj): - if obj._parent is None: - return obj + @property + def _root(self): + if self._parent is None: + return self else: - return self._root(obj._parent) + return self._parent._root def set_flproxy(self, flproxy): """Set flproxy object.""" @@ -1758,7 +1759,7 @@ def __call__(self, **kwds): try: return self.execute_command(**kwds) except KeyboardInterrupt: - self._root(self)._on_interrupt(self) + self._root._on_interrupt(self) raise KeyboardInterrupt @@ -1789,7 +1790,7 @@ def __call__(self, *args, **kwds): try: return self.execute_command(*args, **kwds) except KeyboardInterrupt: - self._root(self)._on_interrupt(self) + self._root._on_interrupt(self) raise KeyboardInterrupt diff --git a/tests/test_fluent_session.py b/tests/test_fluent_session.py index e9537035101..65cba826c68 100644 --- a/tests/test_fluent_session.py +++ b/tests/test_fluent_session.py @@ -66,6 +66,7 @@ def print_transcript(transcript): def test_server_exits_when_session_goes_out_of_scope() -> None: def f(): session = pyfluent.launch_fluent() + session.settings _fluent_host_pid = session.connection_properties.fluent_host_pid _cortex_host = session.connection_properties.cortex_host _inside_container = session.connection_properties.inside_container @@ -88,6 +89,7 @@ def f(): def test_server_does_not_exit_when_session_goes_out_of_scope() -> None: def f(): session = pyfluent.launch_fluent(cleanup_on_exit=False) + session.settings _fluent_host_pid = session.connection_properties.fluent_host_pid _cortex_host = session.connection_properties.cortex_host _inside_container = session.connection_properties.inside_container