Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

roscpp: getNumSubscribers() does not return 0 when a topic is unsubscribed #1699

Closed
ce047 opened this issue Apr 22, 2019 · 1 comment
Closed
Labels

Comments

@ce047
Copy link

ce047 commented Apr 22, 2019

Tested in melodic.

It seems that the method getNumSubscribers() does not return 0 when a topic is unsubscribed if a roscpp publisher is not publishing.

How to reproduce: Have a simple publisher running that does not publish anything but only checks the number of subscribers

int main(int argc, char** argv)
{
  ros::init(argc, argv, "test_get_num_subscribers");

  ros::NodeHandle nh;
  ros::Publisher pub = nh.advertise<std_msgs::String>("test_topic", 1000);

  ros::Rate r(1);
  while(ros::ok())
  {
    if (pub.getNumSubscribers() > 0)
    {
      ROS_INFO("Someone is listening :)");
    } else {
      ROS_INFO("I am alone :(");
    }
    ros::spinOnce();
    r.sleep();
  }

  return 0;
}

and then subcribe to and unsubscribe from that topic, e.g, with

rostopic echo /test_topic && date +%s
^C
1555934665

What should happen: The publisher node should recognize the new subscriber, print out Someone is listening :) as long as the subscriber is active, and more or less immediately realize that there are no subscribers any more and switch back to I am alone :(.

What happens: The publisher recognizes a new subscriber but does not notice that the subscriber unsubscribes shortly after:

...
[ INFO] [1555934661.098353493]: I am alone :(
[ INFO] [1555934662.098477395]: I am alone :(
[ INFO] [1555934663.098413898]: Someone is listening :)
[ INFO] [1555934664.098555165]: Someone is listening :)
[ INFO] [1555934665.098341645]: Someone is listening :)  (subscriber is shut off again) 
[ INFO] [1555934666.098435134]: Someone is listening :)
[ INFO] [1555934667.098370738]: Someone is listening :)
[ INFO] [1555934668.098414957]: Someone is listening :)
[ INFO] [1555934669.098372386]: Someone is listening :)
[ INFO] [1555934670.098341077]: Someone is listening :)
...

Only after approx. 120 seconds the publisher realizes that there is no subscriber any longer:

...
[ INFO] [1555934784.098598787]: Someone is listening :)
[ INFO] [1555934785.098545304]: Someone is listening :) (120 seconds later)
[ INFO] [1555934786.098460942]: Someone is listening :)
[ INFO] [1555934787.098624760]: I am alone :(
[ INFO] [1555934788.098622298]: I am alone :(
[ INFO] [1555934789.098441571]: I am alone :(
...

Notes:

  • behavior is different when node actually publishes messages when someone listens: then getNumSubscribers() returns 0 immediately after subscribers are gone
  • could not reproduce with rospy
  • the 120 seconds is quite reproducible, so I guess there is a simple timeout set somewhere to update the subscription info
@dirk-thomas
Copy link
Member

Addressed by #1704.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants