Is there a safe way to destruct an object that uses a nodelet's multi-threaded NodeHandle to subscribe to a topic?

asked 2022-07-07 10:44:25 -0500

jblee123 gravatar image

I'm using noetic on focal. I have a nodelet that is run-time configurable, in that it contains multiple child member objects that use the nodelet's multihthreaded NodeHandle to subscribe/publish/etc., so the nodelet can perhaps be viewed as a manager of those smaller pieces. At run time, the nodelet has the ability to re-configure those pieces in that it can tear them down and create new ones based on configuration data.

My issue is that if the nodelet is reconfiguring, those worker objects may be in the middle of handling a topic on the MT handler (this is not simply academic -- that exact situation is causing a crash periodically). My question is if there is any way to know when I can safely destruct the worker objects. I know you can unsubscribe to a topic, but when you unsubscribe, a worker thread may have already pulled a topic message to process. I don't think there's a way to handle the situation by putting flags or anything in my handlers since the processing thread may be between removing the message from the queue and actually calling my handler. Similarly, checking the queue size (if that's even accessible, which I don't think it is) won't be an accurate indicator.

The only solutions I can think of are:

  1. Unsubscribe and then wait some X amount of time before object destruction to allow for handlers to finish (worst comes to worst the objects can be squirrelled away somewhere and cleaned up like a minute later in the background), but this seems inelegant.
  2. Come up with a custom MT CallbackQueueInterface that can provide the "I'm not currently handling anything" functionality.
  3. Destroying and restarting the entire nodelet might be an option, but it'd be nice to not have to go that route.

Am I missing any options?

edit retag flag offensive close merge delete


I have the same problem. Did you find an answer?

HLaloge-LC gravatar image HLaloge-LC  ( 2023-03-02 06:47:29 -0500 )edit

Unfortunately not an ideal one. My solution was to add a "shutdown" hook to the worker modules to give them a chance to unsubscribe and then wait a period (one second worked for me, but your case may be different) before continuing with any destruction. That is a band-aid and not a theoretically correct solution -- I have a hunch you'd have to go with my #2 option for that, but that involved more than I had time to get into.

jblee123 gravatar image jblee123  ( 2023-03-02 10:56:45 -0500 )edit