Robotics StackExchange | Archived questions

Is there a common design pattern for fatal exit?

When writing a node, is there a common idiom or design pattern for handling a fatal error, exiting, and cleaning up?

Should I just do something like:

if ( something_bad_happened() ) {
  ROS_FATAL("Something bad happened.");
  exit(1);
}

Does ROS install an atexit() handler to clean up gracefully in this situation?
Or is it generally bad practice to call exit() from within a node?

--wpd

Asked by wpd on 2014-08-09 18:29:28 UTC

Comments

I found this link in the ROS C++ Style Guide. Technically, it doesn't say "Only call exit() at a single well-defined exit point for the application." so I guess this fits within the style guide, but I still wonder what others do.

Asked by wpd on 2014-08-09 19:30:25 UTC

Answers

I'm not aware of a style guide, but in my code I call exit usually only from main mostly during initialization, e.g. when invalid command line parameters/configuration occured or a driver couldn't connect. To exit during a running node something really fatal must have happened as in comparison to anything else exit will definitely make it not working any more.

Referring to the style guide @wpd linked: exit in a library is just evil.

Asked by dornhege on 2014-08-10 07:41:44 UTC

Comments

Yes, that's exactly the case I am asking about -- invalid command lines, bad configuration parameters, etc... detected at node startup. What is the common practice for handling this case? Do folks call ROS_FATAL(), ros::shutdown(), and then exit()? Is there a more elegant mechanism?

Asked by wpd on 2014-08-10 19:04:14 UTC

I use ROS_FATAL + return from main or exit. ros::shutdown can't work yet, because the ROS node isn't running yet.

Asked by dornhege on 2014-08-11 04:04:55 UTC

If I am not mistaken, the preferred way is to call ros::shutdown(); which is what the ros sigint handler does. See Initialization and Shutdown tutorial.

Asked by Chrissi on 2014-08-10 08:20:50 UTC

Comments

I tried that, ros::shutdown() doesn't call exit(), so my code kept executing. I suppose the "safest" thing to do is to display my fatal error message, call ros::shutdown(), and then call exit(). Is that what others do when exiting a node due to a fatal error?

Asked by wpd on 2014-08-10 18:59:45 UTC

I use LOG(FATAL) from Google's Glog. This one can be invoked as std::cout, so it can also print values of variables, which can be useful in a post-mortem.

Asked by oleg.alexandrov on 2020-04-28 19:43:26 UTC

Comments