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: removing io error when logging to closed streams #3273

Merged
merged 14 commits into from
Jul 16, 2024
Merged
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ docker/mapdl/*.sh
tmp.out

# virtual environment
venv/
venv*
.venv/
.venv
.venv*

# Visual studio code local settings
.vscode/
Expand Down
1 change: 1 addition & 0 deletions doc/changelog.d/3273.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix: removing io error when logging to closed streams
27 changes: 26 additions & 1 deletion src/ansys/mapdl/core/mapdl_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
self.check_parameter_names = start_parm.get("check_parameter_names", True)

# Setting up loggers
self._log: logging.Logger = logger.add_instance_logger(
self._log: logger = logger.add_instance_logger(
germa89 marked this conversation as resolved.
Show resolved Hide resolved
self.name, self, level=loglevel
) # instance logger
# adding a file handler to the logger
Expand Down Expand Up @@ -2260,6 +2260,10 @@
def __del__(self):
"""Clean up when complete"""
if self._cleanup:
# removing logging handlers if they are closed to avoid I/O errors
# when exiting after the logger file has been closed.
self._cleanup_loggers()

Check warning on line 2265 in src/ansys/mapdl/core/mapdl_core.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_core.py#L2265

Added line #L2265 was not covered by tests

try:
self.exit()
except Exception as e:
Expand All @@ -2269,6 +2273,27 @@
except Exception:
pass

def _cleanup_loggers(self):
"""Clean up all the loggers"""
# Detached from ``__del__`` for easier testing
if not hasattr(self, "_log"):
return # Early exit if logger has been already cleaned.

Check warning on line 2280 in src/ansys/mapdl/core/mapdl_core.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_core.py#L2280

Added line #L2280 was not covered by tests

logger = self._log

if logger.hasHandlers():
for each_handler in logger.logger.handlers:
if each_handler.stream and not each_handler.stream.closed:
logger.logger.removeHandler(each_handler)

if logger.file_handler:
logger.file_handler.close()
logger.file_handler = None

if logger.std_out_handler:
logger.std_out_handler.close()
logger.std_out_handler = None

def _get_plot_name(self, text: str) -> str:
"""Obtain the plot filename."""
self._log.debug(text)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2453,3 +2453,15 @@ def test_not_correct_et_element(mapdl):
def test_ctrl(mapdl):
mapdl._ctrl("set_verb", 5) # Setting verbosity on the server
mapdl._ctrl("set_verb", 0) # Returning to non-verbose


def test_cleanup_loggers(mapdl):
assert mapdl.logger is not None
assert mapdl.logger.hasHandlers()
assert mapdl.logger.logger.handlers

mapdl._cleanup_loggers()

assert mapdl.logger is not None
assert mapdl.logger.std_out_handler is None
assert mapdl.logger.file_handler is None
Loading