diff --git a/smocore/marionette/marionette.cpp b/smocore/marionette/marionette.cpp index 277e6ca..699988b 100644 --- a/smocore/marionette/marionette.cpp +++ b/smocore/marionette/marionette.cpp @@ -39,6 +39,7 @@ void ComponentThread::marionetteMain(ComponentThread& self) self.initializeTls(); mrntt::exitCode = EXIT_SUCCESS; static boost::asio::signal_set signals(self.getIoService(), SIGINT); + bool callFinalizeReq = false; try { // Register SIGINT (Ctrl+C) handler to request user shutdown @@ -67,9 +68,9 @@ void ComponentThread::marionetteMain(ComponentThread& self) throw JustPrintUsageNoError(options); } + initializeSalmanoff(); self.getIoService().post([]() { - initializeSalmanoff(); mind.initialize(); }); @@ -110,42 +111,46 @@ void ComponentThread::marionetteMain(ComponentThread& self) } std::cout << __func__ << ": Exited event loop" << "\n"; - shutdownSalmanoff(); } catch (const OptionParser::Exception& e) { - std::ostream &out = std::cout; + std::ostream *out = &std::cout; std::string outUsageMsg; if (typeid(e) == typeid(OptionsParserError)) { mrntt::exitCode = EXIT_FAILURE; + out = &std::cerr; outUsageMsg = std::string(__func__) + ": "; } - out << outUsageMsg << e.what() << std::endl; - mind.finalizeReq([]{ - mrntt::mrntt->getIoService().stop(); - }); - self.getIoService().reset(); - self.getIoService().run(); - return; + *out << outUsageMsg << e.what() << std::endl; + callFinalizeReq = true; } catch (const std::exception& e) { std::cerr << __func__ << ": Exception occurred: " << e.what() << std::endl; mrntt::exitCode = EXIT_FAILURE; - return; + callFinalizeReq = true; } catch (...) { std::cerr << __func__ << ": Unknown exception occurred" << std::endl; mrntt::exitCode = EXIT_FAILURE; - return; + callFinalizeReq = true; } - std::cout << __func__ << ": Exiting normally" << std::endl; + if (callFinalizeReq) + { + mind.finalizeReq([]{ + mrntt::mrntt->getIoService().stop(); + }); + self.getIoService().reset(); + self.getIoService().run(); + } + + shutdownSalmanoff(); } } // namespace smo