ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Is there a common design pattern for fatal exit?

asked 2014-08-09 18:29:28 -0500

wpd gravatar image

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.");

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?


edit retag flag offensive close merge delete


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.

wpd gravatar image wpd  ( 2014-08-09 19:30:25 -0500 )edit

3 Answers

Sort by ยป oldest newest most voted

answered 2014-08-10 07:41:44 -0500

dornhege gravatar image

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.

edit flag offensive delete link more


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?

wpd gravatar image wpd  ( 2014-08-10 19:04:14 -0500 )edit

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

dornhege gravatar image dornhege  ( 2014-08-11 04:04:55 -0500 )edit

answered 2020-04-28 19:43:26 -0500

oleg.alexandrov gravatar image

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.

edit flag offensive delete link more

answered 2014-08-10 08:20:50 -0500

Chrissi gravatar image

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.

edit flag offensive delete link more



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?

wpd gravatar image wpd  ( 2014-08-10 18:59:45 -0500 )edit

Question Tools



Asked: 2014-08-09 18:29:28 -0500

Seen: 4,174 times

Last updated: Apr 28 '20