Skip to content

Throw from readDeck() instead of std::exit()#6935

Open
hakonhagland wants to merge 1 commit intoOPM:masterfrom
hakonhagland:fix_readdeck_exit
Open

Throw from readDeck() instead of std::exit()#6935
hakonhagland wants to merge 1 commit intoOPM:masterfrom
hakonhagland:fix_readdeck_exit

Conversation

@hakonhagland
Copy link
Contributor

Alternative to #6933.

Problem

When readDeck() encounters unrecoverable parse errors, it calls MPI_Finalize() + std::exit(EXIT_FAILURE). This is problematic because:

  • Unit tests fail silentlystd::exit() bypasses Boost.Test, producing exit code 1 with no test failure report
  • Not idiomatic C++ — functions should signal errors via exceptions, letting callers decide how to handle them
  • MPI cleanup is wrongMPI_Finalize() requires cooperative shutdown from all ranks; on an error path MPI_Abort() is more appropriate

Solution

  • readDeck.cpp: Replace MPI_Finalize() + std::exit(EXIT_FAILURE) with throw std::runtime_error(failureMessage)
  • Main.hpp: Widen catch in Main::initialize_() from std::invalid_argument to std::exception so it catches both existing and new exception types. The existing MPI_Abort() call in the catch block handles MPI cleanup correctly.

Impact

Context Before After
Production MPI_Finalize + std::exit(1) Exception caught by Main::initialize_(), MPI_Abort, return false
Unit tests Silent exit code 1 Boost.Test catches std::runtime_error, reports failure with message
Normal runs No change No change (error path only)

Replace MPI_Finalize + std::exit(EXIT_FAILURE) with
throw std::runtime_error in readDeck() on parse failure.
This lets callers handle the error: Main::initialize_()
catches it and calls MPI_Abort(), while Boost.Test catches
it and reports a proper test failure instead of a silent
exit. Widen the catch clause in Main::initialize_() from
std::invalid_argument to std::exception to cover both
existing and new exception types.
@hakonhagland hakonhagland added the manual:irrelevant This PR is a minor fix and should not appear in the manual label Mar 13, 2026
@hakonhagland
Copy link
Contributor Author

jenkins build this please

@bska
Copy link
Member

bska commented Mar 13, 2026

I still maintain that the unit test justification is just wrong. You should never add a unit test that has parse errors.

@hakonhagland
Copy link
Contributor Author

You should never add a unit test that has parse errors.

Yes, but in my opinion during development it will be helpful to have this to catch deck errors quickly.

@bska
Copy link
Member

bska commented Mar 13, 2026

You should never add a unit test that has parse errors.

Yes, but in my opinion during development it will be helpful to have this to catch deck errors quickly.

And you're making everything more difficult for the slight convenience of something that happens very infrequently. You should use other tools when developing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

manual:irrelevant This PR is a minor fix and should not appear in the manual

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants