Skip to content

Commit

Permalink
[issue984] Exit more gracefully when running out of memory.
Browse files Browse the repository at this point in the history
We no longer try to clean up the program state when running out of memory.
  • Loading branch information
ClemensBuechner authored Jan 31, 2024
1 parent d7db49d commit 07e922e
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/search/planner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,5 @@ int main(int argc, const char **argv) {
ExitCode exitcode = search_algorithm->found_solution()
? ExitCode::SUCCESS
: ExitCode::SEARCH_UNSOLVED_INCOMPLETE;
utils::report_exit_code_reentrant(exitcode);
return static_cast<int>(exitcode);
exit_with(exitcode);
}
8 changes: 3 additions & 5 deletions src/search/utils/system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ void exit_with(ExitCode exitcode) {
exit(static_cast<int>(exitcode));
}

void exit_after_receiving_signal(ExitCode exitcode) {
/*
In signal handlers, we have to use the "safe function" _Exit() rather
than the unsafe function exit().
*/
void exit_with_reentrant(ExitCode exitcode) {
/* In signal handlers or when we run out of memory, we have to use the
"safe function" _Exit() rather than the unsafe function exit(). */
report_exit_code_reentrant(exitcode);
_Exit(static_cast<int>(exitcode));
}
Expand Down
2 changes: 1 addition & 1 deletion src/search/utils/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ enum class ExitCode {
};

NO_RETURN extern void exit_with(ExitCode returncode);
NO_RETURN extern void exit_after_receiving_signal(ExitCode returncode);
NO_RETURN extern void exit_with_reentrant(ExitCode returncode);

int get_peak_memory_in_kb();
const char *get_exit_code_message_reentrant(ExitCode exitcode);
Expand Down
4 changes: 2 additions & 2 deletions src/search/utils/system_unix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ static void out_of_memory_handler() {
memory for the stack of the signal handler and raising a signal here.
*/
write_reentrant_str(STDOUT_FILENO, "Failed to allocate memory.\n");
exit_with(ExitCode::SEARCH_OUT_OF_MEMORY);
exit_with_reentrant(ExitCode::SEARCH_OUT_OF_MEMORY);
}

static void signal_handler(int signal_number) {
Expand All @@ -173,7 +173,7 @@ static void signal_handler(int signal_number) {
write_reentrant_int(STDOUT_FILENO, signal_number);
write_reentrant_str(STDOUT_FILENO, " -- exiting\n");
if (signal_number == SIGXCPU) {
exit_after_receiving_signal(ExitCode::SEARCH_OUT_OF_TIME);
exit_with_reentrant(ExitCode::SEARCH_OUT_OF_TIME);
}
raise(signal_number);
}
Expand Down
2 changes: 1 addition & 1 deletion src/search/utils/system_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using namespace std;
namespace utils {
void out_of_memory_handler() {
cout << "Failed to allocate memory." << endl;
exit_with(ExitCode::SEARCH_OUT_OF_MEMORY);
exit_with_reentrant(ExitCode::SEARCH_OUT_OF_MEMORY);
}

void signal_handler(int signal_number) {
Expand Down

0 comments on commit 07e922e

Please sign in to comment.