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

Fix/107 #831

Merged
merged 7 commits into from
Feb 8, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clients/rospy/src/rospy/impl/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ def run(self):
if cond is not None:
cond.release()

get_topic_manager().check_all()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How "expensive" will this invocation be? It seems to require frequent locks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I rewrote the check function I'd say it is not expensive. The lock is now only acquired when one or more connections need to be removed. The only overhead that we add now is the check for connections that need to be closed which is O(1) with epoll/kqueue (with poll it would be O(n)!).


#call _connect_topic on all URIs as it can check to see whether
#or not a connection exists.
if uris and not self.handler.done:
Expand Down
69 changes: 54 additions & 15 deletions clients/rospy/src/rospy/topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ def isstring(s):
# for interfacing with topics, while _TopicImpl implements the
# underlying connection details.

if not hasattr(select, 'EPOLLRDHUP'):
select.EPOLLRDHUP = 0x2000


class Topic(object):
"""Base class of L{Publisher} and L{Subscriber}"""

Expand Down Expand Up @@ -188,25 +192,28 @@ class Poller(object):
on multiple platforms. NOT thread-safe.
"""
def __init__(self):
try:
if hasattr(select, 'epoll'):
self.poller = select.epoll()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this affect cross platform compatibility? The documentation only mentions Linux, what about OS X and Windows?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Poller class in topics.py provides a unified interface for polling on all three platforms. I cleaned up the code a bit (48f4f0a) and added a fallback to select.poll for Linux < 2.5.44. I did not touch the code for OS X and Windows. So regarding the compatibility I'd say that this PR does not affect the compatibility.

self.add_fd = self.add_epoll
self.remove_fd = self.remove_epoll
self.error_iter = self.error_epoll_iter
elif hasattr(select, 'poll'):
self.poller = select.poll()
self.add_fd = self.add_poll
self.remove_fd = self.remove_poll
self.error_iter = self.error_poll_iter
except:
try:
# poll() not available, try kqueue
self.poller = select.kqueue()
self.add_fd = self.add_kqueue
self.remove_fd = self.remove_kqueue
self.error_iter = self.error_kqueue_iter
self.kevents = []
except:
#TODO: non-Noop impl for Windows
self.poller = self.noop
self.add_fd = self.noop
self.remove_fd = self.noop
self.error_iter = self.noop_iter
elif hasattr(select, 'kqueue'):
self.poller = select.kqueue()
self.add_fd = self.add_kqueue
self.remove_fd = self.remove_kqueue
self.error_iter = self.error_kqueue_iter
self.kevents = []
else:
#TODO: non-Noop impl for Windows
self.poller = self.noop
self.add_fd = self.noop
self.remove_fd = self.noop
self.error_iter = self.noop_iter

def noop(self, *args):
pass
Expand All @@ -228,6 +235,18 @@ def error_poll_iter(self):
if event & (select.POLLHUP | select.POLLERR):
yield fd

def add_epoll(self, fd):
self.poller.register(fd, select.EPOLLHUP|select.EPOLLERR|select.EPOLLRDHUP)

def remove_epoll(self, fd):
self.poller.unregister(fd)

def error_epoll_iter(self):
events = self.poller.poll(0)
for fd, event in events:
if event & (select.EPOLLHUP | select.EPOLLERR | select.EPOLLRDHUP):
yield fd

def add_kqueue(self, fd):
self.kevents.append(select.kevent(fd))

Expand Down Expand Up @@ -439,6 +458,17 @@ def cleanup_cb_wrapper(s):

return True

def check(self):
fds_to_remove = list(self.connection_poll.error_iter())
if fds_to_remove:
with self.c_lock:
new_connections = self.connections[:]
to_remove = [x for x in new_connections if x.fileno() in fds_to_remove]
for x in to_remove:
rospydebug("removing connection to %s, connection error detected"%(x.endpoint_id))
self._remove_connection(new_connections, x)
self.connections = new_connections

def remove_connection(self, c):
"""
Remove connection from topic.
Expand Down Expand Up @@ -1136,6 +1166,15 @@ def close_all(self):
t.close()
self.pubs.clear()
self.subs.clear()


def check_all(self):
"""
Check all registered publication and subscriptions.
"""
with self.lock:
for t in chain(iter(self.pubs.values()), iter(self.subs.values())):
t.check()
dirk-thomas marked this conversation as resolved.
Show resolved Hide resolved

def _add(self, ps, rmap, reg_type):
"""
Expand Down
1 change: 1 addition & 0 deletions test/test_rospy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ if(CATKIN_ENABLE_TESTING)
add_rostest(test/rostest/latch.test)
add_rostest(test/rostest/on_shutdown.test)
add_rostest(test/rostest/sub_to_multiple_pubs.test)
add_rostest(test/rostest/latch_unsubscribe.test)
endif()
55 changes: 55 additions & 0 deletions test/test_rospy/nodes/listener_once.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python
# Software License Agreement (BSD License)
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of Willow Garage, Inc. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Revision $Id: gossipbot.py 1013 2008-05-21 01:08:56Z sfkwc $
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should probably be removed.


## Simple talker demo that listens to std_msgs/Strings published
## to the 'chatter' topic and shuts down afterwards

from __future__ import print_function

import rospy
from std_msgs.msg import String


def callback(data):
print(rospy.get_caller_id(), "I heard %s" % data.data)
rospy.signal_shutdown("Received %s, exiting now" % data.data)


def listener():
rospy.init_node('listener', anonymous=True)
rospy.sleep(rospy.get_param('delay', 0.0))
rospy.Subscriber("chatter", String, callback)
rospy.spin()


if __name__ == '__main__':
listener()
1 change: 1 addition & 0 deletions test/test_rospy/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<build_depend>test_rosmaster</build_depend>

<test_depend>python-numpy</test_depend>
<test_depend>python-psutil</test_depend>
<test_depend>rosbuild</test_depend>
<test_depend>rosgraph</test_depend>
<test_depend>rospy</test_depend>
Expand Down
7 changes: 7 additions & 0 deletions test/test_rospy/test/rostest/latch_unsubscribe.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<launch>
<node name="listener_once_1" pkg="test_rospy" type="listener_once.py" output="screen" />
<node name="listener_once_2" pkg="test_rospy" type="listener_once.py" output="screen">
<param name="delay" value="1.0" type="double" />
</node>
<test test-name="test_latch_unsubscribe" pkg="test_rospy" type="test_latch_unsubscribe.py" />
</launch>
83 changes: 83 additions & 0 deletions test/test_rospy/test/rostest/test_latch_unsubscribe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env python
# Software License Agreement (BSD License)
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of Willow Garage, Inc. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

PKG = 'test_rospy'
NAME = 'test_latch_unsubscribe'

import os
import sys
import unittest

import psutil

from std_msgs.msg import String
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just about to merge this. But on a last view through the diff I saw that std_msgs is currently neither a runtime nor test dependency yet. Can you please add it as a <test_depend>std_msgs</test_depend>.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... but now it's upset because it was already listed as a build dependency. Maybe just switch this package to format 2?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry. I am only using format 2 nowadays. Can you please just remove the added test dependency again. No need to update to the new format in this PR.



def _get_connections(process_info):
if hasattr(process_info, 'connections'): # new naming
return process_info.connections()
elif hasattr(process_info, 'get_connections'): # old naming
return process_info.get_connections()
raise AttributeError('Wrong psutil version?')


def _get_connection_statii(process_info):
return (conn.status for conn in _get_connections(process_info))


class TestLatch(unittest.TestCase):

def setUp(self):
pass

def test_latch(self):
import rospy
proc_info = psutil.Process(os.getpid())
self.assertNotIn('CLOSE_WAIT', _get_connection_statii(proc_info),
'CLOSE_WAIT sockets already before the test. This '
'should not happen at all.')

rospy.init_node(NAME)
pub = rospy.Publisher('chatter', String, latch=True)
pub.publish(String("hello"))
rospy.sleep(0.5)
self.assertNotIn('CLOSE_WAIT', _get_connection_statii(proc_info),
'CLOSE_WAIT sockets after the subscriber exited. '
'(#107)')
rospy.sleep(1.5)
# also check for a second subscriber
self.assertNotIn('CLOSE_WAIT', _get_connection_statii(proc_info),
'CLOSE_WAIT sockets after the second subscriber '
'exited. (#107)')


if __name__ == '__main__':
import rostest
rostest.run(PKG, NAME, TestLatch, sys.argv)