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 memory leak in rosgraph for kernel < 4.16 and Python 3 #2165

Merged
merged 1 commit into from
Jun 29, 2021

Conversation

schadocalex
Copy link
Contributor

@schadocalex schadocalex commented Jun 24, 2021

Following #2132 and the discussion from #2132 (comment), here is a fix for the memory leak detected in Python 3 with kernel < 4.16.

I see no reason why daemon_threads should depend on the HTTP version. The only reason the changes were grouped together was to "switch off" the change in https://github.com/ros/ros_comm/pull/1287/files, which we observed introduced the problem.

I have tested it with kernel 4.15 (with the bug) and 4.18 (without the bug), works for both in Python3.

The memory leak is due to the ThreadingMixin referencing all threads, with daemon_threads = False and block_on_close = True by default:
https://github.com/python/cpython/blob/3.8/Lib/socketserver.py#L696
It only releases the memory when the server is closed. That's not a real memory leak as the memory is released at the end but roscore is designed to run for a long time. In my project it represents 3GB/hour of unreleased RAM in rosmaster.

Even the native ThreadingHTTPServer has the option enabled by default: https://github.com/python/cpython/blob/3.8/Lib/http/server.py#L144

For Python2 the problem does not exist, since there is no block_on_close option.
https://github.com/python/cpython/blob/v2.7.15/Lib/SocketServer.py#L582

I don't know if I have to put comments in the code explaining the changes or not.

Copy link
Contributor

@sloretz sloretz left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for the thorough investigation and explanation!

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 this pull request may close these issues.

None yet

4 participants