Skip to content
Open
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
12 changes: 6 additions & 6 deletions OMPython/ModelicaSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1145,15 +1145,15 @@ def simulate(
cmd_definition = om_cmd.definition()
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
# and check returncode *AND* resultfile
if returncode != 0 and self._result_file.is_file():
if returncode != 0:
# check for an empty (=> 0B) result file which indicates a crash of the model executable
# see: https://github.com/OpenModelica/OMPython/issues/261
# https://github.com/OpenModelica/OpenModelica/issues/13829
if self._result_file.size() == 0:
if self._result_file.is_file() and self._result_file.size() == 0:
self._result_file.unlink()
raise ModelicaSystemError("Empty result file - this indicates a crash of the model executable!")

logger.warning(f"Return code = {returncode} but result file exists!")
logger.warning(f"Return code = {returncode} but result file was created!")

self._simulated = True

Expand Down Expand Up @@ -2144,9 +2144,9 @@ def worker(worker_id, task_queue):
try:
returncode = self.get_session().run_model_executable(cmd_run_data=cmd_definition)
logger.info(f"[Worker {worker_id}] Simulation {resultpath.name} "
f"finished with return code: {returncode}")
except ModelicaSystemError as ex:
logger.warning(f"Simulation error for {resultpath.name}: {ex}")
f"finished with return code {returncode}")
except OMCSessionException as ex:
logger.warning(f"Error executing {repr(cmd_definition.get_cmd())}: {ex}")

# Mark the task as done
task_queue.task_done()
Expand Down
11 changes: 7 additions & 4 deletions OMPython/OMCSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,20 +800,23 @@ def run_model_executable(cmd_run_data: OMCSessionRunData) -> int:
env=my_env,
cwd=cmd_run_data.cmd_cwd_local,
timeout=cmd_run_data.cmd_timeout,
check=True,
check=False,
Copy link

@bilderbuchi bilderbuchi Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting an exception on non-zero exit code was good. Exceptions for handling control flow in Python are totally fine, and obvious to the user, and hard to miss, I would keep that. (I did not realize that that's what's happening because I did not dig deep enough in the call hierarchy)

The additional logging is good, so I'd keep that too. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception on non-zero exit code has to be handled with care. There are possibilities that a result file is created (and usefull) even if the exit code is != 0. An exception here would prevent any processing of such data.

If I remeber correctly, an example is if the model is terminated via Modelica code.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remeber correctly, an example is if the model is terminated via Modelica code.

You mean via terminate()? If yes a nonzero exit code would be strange, because this "successfully terminates the analysis which was carried out".

)
stdout = cmdres.stdout.strip()
stderr = cmdres.stderr.strip()
returncode = cmdres.returncode

logger.debug("OM output for command %s:\n%s", repr(cmdl), stdout)
if returncode != 0:
logger.warning("OM executable run %s with returncode=%d and stdout:\n%s",
repr(cmdl), returncode, stdout)
else:
logger.debug("OM executable run %s with stdout:\n%s", repr(cmdl), stdout)

if stderr:
raise OMCSessionException(f"Error running model executable {repr(cmdl)}: {stderr}")

except subprocess.TimeoutExpired as ex:
raise OMCSessionException(f"Timeout running model executable {repr(cmdl)}") from ex
except subprocess.CalledProcessError as ex:
raise OMCSessionException(f"Error running model executable {repr(cmdl)}") from ex

return returncode

Expand Down
Loading