-
Notifications
You must be signed in to change notification settings - Fork 540
Description
JSBSim has traditionally been employed within monolithic applications (such as FlightGear), where any JSBSim error generally resulted in the termination of the entire program. This assumption has simplified error handling within JSBSim itself: errors were considered unrecoverable since the calling application was expected to terminate when they occurred.
These days are gone now that we are providing a Python module and a Matlab S-function. This has opened up new possibilities for using JSBSim in interactive Read-Eval-Print-Loop (REPL) environments. This shift necessitates a reassessment and upgrade of our error handling and object state management strategies.
Specifically, this issue aims to discuss the following key requirements for robust JSBSim usage within REPLs (including, but not limited to, Python and Matlab):
- REPL Stability: Under no circumstances should JSBSim cause the REPL to crash or terminate unexpectedly. Sudden REPL terminations are extremely frustrating for users, as it can lead to the loss of unsaved work and create a perception of poor software quality.
- Robust Object State Management: Users working in a REPL environment often engage in exploratory, trial-and-error workflows. JSBSim objects may therefore exist in various intermediate or inconsistent states due to user errors. JSBSim should handle these states gracefully, allowing users to recover and continue their work without requiring, for example, the complete destruction and recreation of their instance of
FGFDMExec. This implies the need for mechanisms to:- Detect and report errors clearly and informatively.
- Provide methods for users to query the state of an object?
- Offer options to reset or correct invalid object configurations, such as the ability to call
FGFDMExec::Loadagain after a previous failure. - Prevent operations that would lead to crashes or undefined behavior when an object is in an invalid state. Typically prevent executing
FGFDMExec::RunifFGFDMExec::Loadhas failed.
To address these requirements, the following work areas should be addressed:
- Memory Management: Implement more rigorous memory management practices to prevent leaks, especially when the execution is leaving the happy path (i.e. the ideal execution flow of a program, where all conditions are met, and no errors occur).
- For example, pull requests Fix memory leaks in
FGSwitch#1230 and Memory leak fix and general improvements ofFGDistributor#1245 addressed memory leaks that occurred during error conditions, which was previously not a concern since the operating system would automatically reclaim all reserved memory upon program termination. - The straightforward way to address this requirement is to replace all the calls to
newanddeleteby smart pointers (i.e.unique_ptrandshared_ptr).
- For example, pull requests Fix memory leaks in
- Error Reporting: Ensure that error messages are clear, informative, and provide users with a clear understanding of the severity of the error. There should be no silently ignored or handled errors.
- When JSBSim makes assumptions to address an error and continue execution, it should provide a message explaining the corrective action taken.
- The on-going work on the new logging system is also addressing that point by adding a severity level (i.e.
LogLevel) to each error message.
- Exception Handling: Implement robust exception handling, ensuring that exceptions are caught where appropriate, or converted to a REPL-compatible error format (e.g., converting C++ exceptions to Python exceptions).
This issue serves as a starting point for discussing the necessary modifications and best practices to ensure that JSBSim can be used effectively and reliably in interactive environments. Future pull requests that contribute to this objective should reference this issue, to maintain a clear connection to the requirements and goals outlined here.