Fix memory leak in rosgraph for kernel < 4.16 and Python 3 #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Following ros#2132 and the discussion from ros#2132 (comment), here is a fix for the memory leak detected in Python 3 with kernel < 4.16.
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
andblock_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 3Go/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#L144For 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