What is the correct way to do stuff before a node is shutdown? [closed]
I want to do some shutting down code for my node. It is important that I am still fully connected to ROS while that is happening, so putting stuff just after the main loop is not an option.
Currently I would pass NoSigIntHandler to ros::init, install my own SIGINT handler, do stuff there and then call ros::shutdown.
Is this correct, i.e. the intended way or is there a better solution?
Update: I tested this and it works fine for pressing Ctrl-C even from roslaunch. For me this is OK right now.
The only thing that does not work is what Lorenz hinted at: rosnode kill seems to use another method, I get:
[ WARN] [1329394338.937166789, 61.017000000]: Shutdown request received. [ WARN] [1329394338.937223749, 61.017000000]: Reason given for shutdown: [user request]
and my SIGINT handler is not called.
I could try to work around this by checking ros::isShuttingDown
and trying to shutdown my stuff while that is happening, but that doesn't seem safe to me.
I see at least one (possible) problem with your solution: will it handle a
rosnode kill
command, i.e. a shutdown triggered via xmlrpc? I don't know a better solution though.Seems reasonable to me.
@Lorenz: Depends on what rosnode kill does. Does it send SIGINT/SIGTERM? I'll play around with this tomorrow.
Rosnode kill cannot send SIGINT if the node is not owned by the user executing rosnode or if the node is running on a different computer. As far as I know, it uses the shutdown XMLRPC call (http://www.ros.org/wiki/ROS/Slave_API).
Yep, just found that out by trying. The only question left would be: Is it possible to hook in there and stop the shutdown until I am done. I'm OK with Ctrl-C handling for now as I don't use rosnode kill.