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
24 changes: 23 additions & 1 deletion src/ansys/mapdl/core/mapdl_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def __init__(
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 @@ -2263,6 +2263,10 @@ def exit(self): # pragma: no cover
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()

try:
self.exit()
except Exception as e:
Expand All @@ -2272,6 +2276,24 @@ def __del__(self):
except Exception:
pass

def _cleanup_loggers(self):
"""Clean up all the loggers"""
# Detached from ``__del__`` for easier testing
logger = self._log

if logger.hasHandlers():
for each_handler in logger.logger.handlers:
if 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
14 changes: 14 additions & 0 deletions tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2453,3 +2453,17 @@ 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
assert mapdl.logger.std_out_handler

mapdl._cleanup_loggers()

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