ros / ros_comm

ROS communications-related packages, including core client libraries (roscpp, rospy, roslisp) and graph introspection tools (rostopic, rosnode, rosservice, rosparam).
http://wiki.ros.org/ros_comm
752 stars 913 forks source link

ResourceWarning with rosnode ping #2176

Open git-afsantos opened 3 years ago

git-afsantos commented 3 years ago

Greetings!

I have some test code in Python using unittest and rosnode.ping_node(). When running this test code with Python 3 (ROS Noetic), a series of warnings are displayed, stating that sockets are not explicitly closed. I have not seen these warnings before with ROS Melodic and Python 2.7, so I am guessing this has been added in Python 3.x.

For an example, consider the following environment:

#!/usr/bin/env bash
roscore &
rostopic echo /topic

And the following test code:

import sys
import unittest
from rosnode import rosnode_ping_all
import rospy

class TestTracer(unittest.TestCase):
    def test_ping_warning(self):
        rosnode_ping_all(skip_cache=True)

def main():
    rospy.init_node("ping_tester", log_level=rospy.DEBUG)
    unittest.main(module=__name__, argv=[__name__], exit=False)
    sys.stdout.flush()

if __name__ == "__main__":
    main()

Executing this code with Python 3 produces the following output.

Test Output > [DEBUG] [1629206461.806698]: init_node, name[/ping_tester], pid[46274] > [DEBUG] [1629206461.809490]: binding to 0.0.0.0 0 > [DEBUG] [1629206461.811671]: bound to 0.0.0.0 33287 > [DEBUG] [1629206461.814433]: ... service URL is rosrpc://andre-ros-noetic:33287 > [DEBUG] [1629206461.816574]: [/ping_tester/get_loggers]: new Service instance > [DEBUG] [1629206461.820822]: ... service URL is rosrpc://andre-ros-noetic:33287 > [DEBUG] [1629206461.823336]: [/ping_tester/set_logger_level]: new Service instance > /opt/ros/noetic/lib/python3/dist-packages/rosnode/__init__.py:403: ResourceWarning: unclosed > if rosnode_ping(node, max_count=1, verbose=verbose, skip_cache=skip_cache): > ResourceWarning: Enable tracemalloc to get the object allocation traceback > /opt/ros/noetic/lib/python3/dist-packages/rosnode/__init__.py:403: ResourceWarning: unclosed > if rosnode_ping(node, max_count=1, verbose=verbose, skip_cache=skip_cache): > ResourceWarning: Enable tracemalloc to get the object allocation traceback > /opt/ros/noetic/lib/python3/dist-packages/rosnode/__init__.py:403: ResourceWarning: unclosed > if rosnode_ping(node, max_count=1, verbose=verbose, skip_cache=skip_cache): > ResourceWarning: Enable tracemalloc to get the object allocation traceback > /opt/ros/noetic/lib/python3/dist-packages/rosnode/__init__.py:403: ResourceWarning: unclosed > if rosnode_ping(node, max_count=1, verbose=verbose, skip_cache=skip_cache): > ResourceWarning: Enable tracemalloc to get the object allocation traceback > /opt/ros/noetic/lib/python3/dist-packages/rosnode/__init__.py:403: ResourceWarning: unclosed > if rosnode_ping(node, max_count=1, verbose=verbose, skip_cache=skip_cache): > ResourceWarning: Enable tracemalloc to get the object allocation traceback > /opt/ros/noetic/lib/python3/dist-packages/rosnode/__init__.py:403: ResourceWarning: unclosed > if rosnode_ping(node, max_count=1, verbose=verbose, skip_cache=skip_cache): > ResourceWarning: Enable tracemalloc to get the object allocation traceback > ping_test.py:11: ResourceWarning: unclosed > rosnode_ping_all(skip_cache=True) > ResourceWarning: Enable tracemalloc to get the object allocation traceback > . > > Ran 1 test in 0.018s > > OK

Here is an abbreviated version of the issue:

ResourceWarning: Enable tracemalloc to get the object allocation traceback
ping_test.py:11: ResourceWarning: unclosed <socket.socket fd=9, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 47762), raddr=('127.0.0.1', 11311)>
  rosnode_ping_all(skip_cache=True)
ResourceWarning: Enable tracemalloc to get the object allocation traceback

The issue can be resolved by enveloping ServerProxy instances used in ping_node (example 1, example 2) with a context manager, as stated in the docs and as shown in similar reports in StackOverflow.