Can nodelet's onInit and callbacks be executed in parallel?

asked 2017-07-21 04:26:47 -0500

cellard0or gravatar image

I have a problem where only sometimes one of my nodelets crashes on startup. I identified a possible issue in the order of commands in the onInit() method. Consider the following:

void onInit() {
   subscriber = getNodeHandle().subscribe("something", 100, &ThisNodelet::callback, this);
   publishRate = getPrivateNodeHandle().param("publish_rate", 10.f);
}

void callback(someMessageType msg) {
  if (publishRate == 10.0) {  // publishRate maybe uninitialized?
    ...
  }
}

My question is whether publishRate might be uninitialized when entering the callback, because it might be called right after the subscription happened, not only after onInit() finished. This would be quite an important implication for the order in which to do things in the onInit() method but I did not find documentation about this.

edit retag flag offensive close merge delete

Comments

wiki/nodelet - Threading Model has some hints, but nothing explicit.

I think you can assume that callbacks for msgs will start to be processed as soon as you call subscribe(..), so publishRate would indeed be uninitialised there.

gvdhoorn gravatar image gvdhoorn  ( 2017-07-21 05:28:44 -0500 )edit

Btw: checking equality on float type variables is not really robust.

gvdhoorn gravatar image gvdhoorn  ( 2017-07-21 05:29:17 -0500 )edit

Yeah, this was just to do //something// with it. I would suggest to add that detail to the documentation then, it seems to be very easy to introduce bugs this way, esp. since it is somehow counter intuitive to have anything happen before an init function exits.

cellard0or gravatar image cellard0or  ( 2017-07-21 06:24:23 -0500 )edit

Well .. it's a wiki, so anyone can edit it. It would be great if you could add some additional detail there.

Note that this is most likely not specific to nodelets: any multi-threaded ROS node (fi: AsyncSpinner) would probably behave the same (but I haven't checked).

gvdhoorn gravatar image gvdhoorn  ( 2017-07-21 07:21:47 -0500 )edit

I will do that, thanks for the information!

cellard0or gravatar image cellard0or  ( 2017-07-21 07:40:01 -0500 )edit