From 0b007780cfe6a5350153ade6a7798ef79ff078e5 Mon Sep 17 00:00:00 2001 From: Jendrik Seipp Date: Mon, 27 May 2024 23:11:48 +0200 Subject: [PATCH 1/2] Raise exception in exit_with() to ensure that destructors are called upon program exit. --- src/search/planner.cc | 72 +++++++++++++++++++++----------------- src/search/utils/system.cc | 2 +- src/search/utils/system.h | 12 +++++++ 3 files changed, 52 insertions(+), 34 deletions(-) diff --git a/src/search/planner.cc b/src/search/planner.cc index e86ef8891f..a524825698 100644 --- a/src/search/planner.cc +++ b/src/search/planner.cc @@ -13,38 +13,44 @@ using namespace std; using utils::ExitCode; int main(int argc, const char **argv) { - utils::register_event_handlers(); - - if (argc < 2) { - utils::g_log << usage(argv[0]) << endl; - utils::exit_with(ExitCode::SEARCH_INPUT_ERROR); - } - - bool unit_cost = false; - if (static_cast(argv[1]) != "--help") { - utils::g_log << "reading input..." << endl; - tasks::read_root_task(cin); - utils::g_log << "done reading input!" << endl; - TaskProxy task_proxy(*tasks::g_root_task); - unit_cost = task_properties::is_unit_cost(task_proxy); + try { + utils::register_event_handlers(); + + if (argc < 2) { + utils::g_log << usage(argv[0]) << endl; + utils::exit_with(ExitCode::SEARCH_INPUT_ERROR); + } + + bool unit_cost = false; + if (static_cast(argv[1]) != "--help") { + utils::g_log << "reading input..." << endl; + tasks::read_root_task(cin); + utils::g_log << "done reading input!" << endl; + TaskProxy task_proxy(*tasks::g_root_task); + unit_cost = task_properties::is_unit_cost(task_proxy); + } + + shared_ptr search_algorithm = + parse_cmd_line(argc, argv, unit_cost); + + + utils::Timer search_timer; + search_algorithm->search(); + search_timer.stop(); + utils::g_timer.stop(); + + search_algorithm->save_plan_if_necessary(); + search_algorithm->print_statistics(); + utils::g_log << "Search time: " << search_timer << endl; + utils::g_log << "Total time: " << utils::g_timer << endl; + + ExitCode exitcode = search_algorithm->found_solution() + ? ExitCode::SUCCESS + : ExitCode::SEARCH_UNSOLVED_INCOMPLETE; + exit_with(exitcode); + } catch (const utils::ExitException &e) { + /* To ensure that all destructors are called before the program exits, + we raise an exception in utils::exit_with() and let main() return. */ + return static_cast(e.get_exitcode()); } - - shared_ptr search_algorithm = - parse_cmd_line(argc, argv, unit_cost); - - - utils::Timer search_timer; - search_algorithm->search(); - search_timer.stop(); - utils::g_timer.stop(); - - search_algorithm->save_plan_if_necessary(); - search_algorithm->print_statistics(); - utils::g_log << "Search time: " << search_timer << endl; - utils::g_log << "Total time: " << utils::g_timer << endl; - - ExitCode exitcode = search_algorithm->found_solution() - ? ExitCode::SUCCESS - : ExitCode::SEARCH_UNSOLVED_INCOMPLETE; - exit_with(exitcode); } diff --git a/src/search/utils/system.cc b/src/search/utils/system.cc index 001f68542a..34e25b6d27 100644 --- a/src/search/utils/system.cc +++ b/src/search/utils/system.cc @@ -46,7 +46,7 @@ bool is_exit_code_error_reentrant(ExitCode exitcode) { void exit_with(ExitCode exitcode) { report_exit_code_reentrant(exitcode); - exit(static_cast(exitcode)); + throw ExitException(exitcode); } void exit_with_reentrant(ExitCode exitcode) { diff --git a/src/search/utils/system.h b/src/search/utils/system.h index 1f23ef19c7..773c19e8e0 100644 --- a/src/search/utils/system.h +++ b/src/search/utils/system.h @@ -53,6 +53,18 @@ enum class ExitCode { SEARCH_UNSUPPORTED = 34 }; +class ExitException : public std::exception { + ExitCode exitcode; +public: + explicit ExitException(ExitCode exitcode) + : exitcode(exitcode) { + } + + ExitCode get_exitcode() const { + return exitcode; + } +}; + NO_RETURN extern void exit_with(ExitCode returncode); NO_RETURN extern void exit_with_reentrant(ExitCode returncode); From 07c124c274b990e211c9794f4c48cbf69ec7dc26 Mon Sep 17 00:00:00 2001 From: Jendrik Seipp Date: Tue, 28 May 2024 18:31:59 +0200 Subject: [PATCH 2/2] Add and use non-reentrant report_exit_code() function. --- src/search/utils/system.cc | 15 ++++++++++++++- src/search/utils/system.h | 1 + src/search/utils/system_windows.cc | 13 +++---------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/search/utils/system.cc b/src/search/utils/system.cc index 34e25b6d27..46a2438908 100644 --- a/src/search/utils/system.cc +++ b/src/search/utils/system.cc @@ -44,8 +44,21 @@ bool is_exit_code_error_reentrant(ExitCode exitcode) { } } +void report_exit_code(ExitCode exitcode) { + const char *message = get_exit_code_message_reentrant(exitcode); + bool is_error = is_exit_code_error_reentrant(exitcode); + if (message) { + ostream &stream = is_error ? cerr : cout; + stream << message << endl; + } else { + cerr << "Exitcode: " << static_cast(exitcode) << endl + << "Unknown exitcode." << endl; + abort(); + } +} + void exit_with(ExitCode exitcode) { - report_exit_code_reentrant(exitcode); + report_exit_code(exitcode); throw ExitException(exitcode); } diff --git a/src/search/utils/system.h b/src/search/utils/system.h index 773c19e8e0..9d5218afcb 100644 --- a/src/search/utils/system.h +++ b/src/search/utils/system.h @@ -72,6 +72,7 @@ int get_peak_memory_in_kb(); const char *get_exit_code_message_reentrant(ExitCode exitcode); bool is_exit_code_error_reentrant(ExitCode exitcode); void register_event_handlers(); +void report_exit_code(ExitCode exitcode); void report_exit_code_reentrant(ExitCode exitcode); int get_process_id(); } diff --git a/src/search/utils/system_windows.cc b/src/search/utils/system_windows.cc index a244b68468..3fbe316d34 100644 --- a/src/search/utils/system_windows.cc +++ b/src/search/utils/system_windows.cc @@ -58,16 +58,9 @@ void register_event_handlers() { } void report_exit_code_reentrant(ExitCode exitcode) { - const char *message = get_exit_code_message_reentrant(exitcode); - bool is_error = is_exit_code_error_reentrant(exitcode); - if (message) { - ostream &stream = is_error ? cerr : cout; - stream << message << endl; - } else { - cerr << "Exitcode: " << static_cast(exitcode) << endl - << "Unknown exitcode." << endl; - abort(); - } + /* We call a function that uses ostreams even though this is unsafe in + reentrant code, because we don't know how to do it otherwise on Windows. */ + report_exit_code(exitcode); } int get_process_id() {