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

ResourceWarning with rosnode ping #2176

Open
git-afsantos opened this issue Aug 17, 2021 · 2 comments · May be fixed by #2381
Open

ResourceWarning with rosnode ping #2176

git-afsantos opened this issue Aug 17, 2021 · 2 comments · May be fixed by #2381

Comments

@git-afsantos
Copy link
Contributor

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 <socket.socket fd=10, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 47764), raddr=('127.0.0.1', 11311)>
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 <socket.socket fd=11, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 49064), raddr=('127.0.1.1', 42105)>
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 <socket.socket fd=10, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 47768), raddr=('127.0.0.1', 11311)>
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 <socket.socket fd=11, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 56510), raddr=('127.0.1.1', 42669)>
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 <socket.socket fd=10, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 47772), raddr=('127.0.0.1', 11311)>
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 <socket.socket fd=11, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 56428), raddr=('127.0.1.1', 42983)>
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 <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
.

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.

@RicardoM17
Copy link

RicardoM17 commented Dec 2, 2024

Hi @git-afsantos Did you end up solving this in some way?

I'm facing the same issue now (this is actually causing a memory leak in my node) so if you have any findings that you could share that would be great.

Edit: I have a fix, I will make a PR today or tomorrow and update this issue accordingly.

@git-afsantos
Copy link
Contributor Author

git-afsantos commented Dec 12, 2024

Hey, @RicardoM17 .
It has been a long time, so I may not recall correctly, but I think that my final solution just ended up dropping ping entirely.

Cheers for opening that PR, though.

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

Successfully merging a pull request may close this issue.

2 participants