Idle ROS nodes using >100% CPU

asked 2018-07-24 01:16:46 -0500

sgt_droelf gravatar image

updated 2018-07-24 05:40:03 -0500

i've built/written a couple of my own ROS nodes. They are connected like this:

create_data_node mqtt_bridge =>|broker|=> mqtt_bridge => processing_node
        master1/ pc1                        master2/pc2

create_data_node does just what you would expect - creating data and then publishing it onto the mqtt_bridge, which in return will publish them onto the broker. From there another mqtt_bridge (on another pc) received the data and published it to processing_node which in return does some processing. The workflow itself has to be that way, works out when data is created and is not part of this question.

Now: The moment i shut down create_data_node everything on master2/pc2 should enter idle mode, since no data is put in and no callback are called. Yet the CPU usage for this particular node is > 100% and furthermore rosout is using ~150% as well.

On processing_node i'm simply creating a ros::Subscriber and then using ros::spin(), just as follows (stripped example):

int main(int argc, char** argv) {
    ros::init(argc, argv, DEFAULT_NODENAME);
    ros::NodeHandle n;

    ros::Subscriber sub = n.subscribe<messages::testmessage>("topic", 1000, callback);

    return 0;

As far as i understand ros::spin() (in contrary to while(1) { ros::spinOnce();} should not do an idle wait.

So my question simply is - What causes this behaviour and what can i do to prevent this? Thanks in advance!

Edit: Reducing the subscriber queue to 100 resulted in 60% CPU and further reducing to 10 in 50% CPU usage.

edit retag flag offensive close merge delete


have you tried with a queue of 1 for you subscriber?

GuillaumeB gravatar image GuillaumeB  ( 2018-07-24 04:09:25 -0500 )edit

Just to keep things connected: #q298364.

gvdhoorn gravatar image gvdhoorn  ( 2018-07-24 04:35:13 -0500 )edit

@GuillaumeB see my edit.

sgt_droelf gravatar image sgt_droelf  ( 2018-07-24 05:40:09 -0500 )edit

Is the process in your callback a long and heavy one? What kind of message are you using (simple Float32 or huge images)?

GuillaumeB gravatar image GuillaumeB  ( 2018-07-24 06:16:54 -0500 )edit

There are no messages incoming and thus the callback isn't even executed.

sgt_droelf gravatar image sgt_droelf  ( 2018-07-24 06:22:41 -0500 )edit

It tried in my side with a queue of 2000 msg and if I publish msg I can reach 50% of cpu usage but not before sending any msg (0.7%). I would suspect that messages are stacked in your queue and even once you kill the publisher node it is still processing.

GuillaumeB gravatar image GuillaumeB  ( 2018-07-24 08:18:52 -0500 )edit

again, the nodes are in idle mode since there no messages incoming. none are published and thus there should barely be any cpu usage at all

sgt_droelf gravatar image sgt_droelf  ( 2018-07-24 08:23:52 -0500 )edit

But as @GuillaumeB has said, the buffer may still contain messages long after the publisher node has been shutdown. Could you modify your code so that the processing_node logs to the screen when it processes a messages, then you could tell for sure.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-07-24 09:27:10 -0500 )edit