Ask Your Question
2

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

edit retag flag offensive close merge delete

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.

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

3 Answers

Sort by ยป oldest newest most voted
1

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

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?

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
0

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
0

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

Comments

1

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

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

Seen: 3,593 times

Last updated: Apr 28 '20