Skip to content

Instantly share code, notes, and snippets.

@ejalaa12
Created December 11, 2018 11:19
Show Gist options
  • Save ejalaa12/3f0e76d11840549f1463c84f583b7642 to your computer and use it in GitHub Desktop.
Save ejalaa12/3f0e76d11840549f1463c84f583b7642 to your computer and use it in GitHub Desktop.
Publishers in rospy can't be unregistered without errors

Report

Description

When calling pub.unregister we get warning messages that look like that:

[WARN] [1544525264.664350]: Could not process inbound connection: [/pub_bug] is not a publisher of [/topic2]. Topics are [['/rosout', 'rosgraph_msgs/Log']]{'message_definition': 'string data\n', 'callerid': '/pub_bug', 'tcp_nodelay': '0', 'md5sum': '992ce8a1687cec8c8bd883ec73ca41d1', 'topic': '/topic2', 'type': 'std_msgs/String'}

This script tries to reproduce the error in various conditions to locate where the issue comes from

Experimemts

pub1 is publishing on topic1 with no subscriber.
When calling pub1.unregister() no warning messages appear.

While pub1 is still publishing on topic1,
when subscribing from outside rostopic echo /topic1,
and then unscubscribing by killing the rostopic echo before pub1.unregister is called
we have no warning messages

pub2 is publish on topic2 with one subscriber.
When calling pub2.unregister() we receive warning messages.

pub3 is commented out. But when uncommented. It publishes on topic2 like pub2.
When calling pub2.unregister() we receive NO warning messages.

In both cases: pub3 commented and uncommented.
When you try to create a new publisher on topic2 (with recreate_pub2), no callback are made.

In any case, before and after calling pub2.unregister(),
the subscriber on topic2 receives callbacks of messages from other nodes

After the pub2 is re-created, when calling rostopic echo /topic2 We receive no messages from the pub2 publisher.

Conclusion

  • When calling pub.unregister, if we have no subscriber then the call is successful.
  • Failure to call pub.unregister blocks further publishers from this node to publish to the same topic
#!/usr/bin/env python
import rospy
from std_msgs.msg import String
def log(msg):
print msg
def unregister_pub1(event=None):
global pub1_flag
pub1_flag = False
print 'Unregister Pub 1 ...'
pub1.unregister()
def unregister_pub2(event=None):
global pub2_flag
pub2_flag = False
print 'Unregister Pub 2 ...'
pub2.unregister()
def recreate_pub2(event=None):
global pub2
print 'Re-create pub2'
pub2 = rospy.Publisher('topic2', String, queue_size=2)
pub2_flag = True
def finish(event=None):
global not_finished
not_finished = False
rospy.init_node('pub_bug')
# A publisher to test unregister with no subscriber
pub1 = rospy.Publisher('topic1', String, queue_size=1)
pub1_flag = True
# Two publishers to the same topic with one subscriber
pub2 = rospy.Publisher('topic2', String, queue_size=2)
# pub3 = rospy.Publisher('topic2', String, queue_size=2)
sub = rospy.Subscriber('topic2', String, callback=log)
pub2_flag = True
# Unregister pub1 after 5 seconds
rospy.Timer(rospy.Duration(5), unregister_pub1, oneshot=True)
# Unregister pub2 after 10 seconds
rospy.Timer(rospy.Duration(10), unregister_pub2, oneshot=True)
# Create a new publisher to topic2 after 15 seconds
rospy.Timer(rospy.Duration(15), recreate_pub2, oneshot=True)
# Stop everything after 20 seconds
not_finished = True
rospy.Timer(rospy.Duration(20), finish, oneshot=True)
# Publish periodically
while not rospy.is_shutdown() and not_finished:
if pub1_flag:
pub1.publish('hello from pub1')
if pub2_flag:
pub2.publish('hello from pub2')
# pub3.publish('hello from pub3')
rospy.rostime.wallsleep(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment