-
Notifications
You must be signed in to change notification settings - Fork 912
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
Fix/107 #831
Changes from 6 commits
7050923
f637099
1f3862e
7380268
72ea719
fd679e5
29653b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}""" | ||
|
||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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)) | ||
|
||
|
@@ -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. | ||
|
@@ -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): | ||
""" | ||
|
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 $ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() |
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> |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)!).