-
Notifications
You must be signed in to change notification settings - Fork 341
Fix memory leak in pyext when Selectable is returned to Python #343
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
Merged
lguohan
merged 2 commits into
sonic-net:master
from
pavel-shirshov:pavelsh/fix_memory_leak
May 27, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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.
thanks for fix this memory leak issue. can you describe the nature of this fix? what causes the memory leak?
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.
First line I just changed SWIG_POINTER_OWN to 0, because we don't own the pointer. (in my logic). We just return the pointer which was in the subribers list.
http://www.swig.org/Doc3.0/SWIGDocumentation.html#Chicken_collection
So we don't want to garbage collect the pointer, which still in the subscribers list.
The second change. I found it in the multiple examples.
https://github.com/swig/swig/blob/rel-3.0.11/Lib/python/pyrun.swg#L120
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.
what do you mean by "in my logic“? so, the first one is addressing the bug. I assume.
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.
check this thread. https://sourceforge.net/p/swig/mailman/message/27419191/
it seems to me the root cause of memory is not SWIG_POINTER_OWN, but PyList_Append. I can see that https://github.com/swig/swig/blob/rel-3.0.11/Lib/python/pyrun.swg#L134 did the refcount decrement.
can you keep SWIG_POINTER_OWN, and only change to SWIG_Python_AppendOutput, and check if it solves the memory leak?
I looked around the SWIG_POINTER_OWN, do not have much luck in understanding its usage, seems related to finalizer.
Uh oh!
There was an error while loading. Please reload this page.
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.
@lguohan
This typemap
gives me a crash. As I think because python garbage collects returned Selectable. which we have only one:
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.
@lguohan I read swig documentation in many places + some source code, but not everything. As I understood from the documentation SWIG needs to have a mechanism to garbage collect objects, which are created inside of C++ method, were given outside, and it is user responsibility to remove the objects. To address such objects with python swig introduced *_OWN flag. So by documentation and sources I read I think we definitely don't need *_OWN flag, because we don't want the selectabl object to be removed, after we receive it from Select.
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.
Why I wrote "In my logic", because I didn't dive deep enough for me when I investigated this issue. I didn't run gdb or read stacktrace. I just did series of experiments and implemented the refcnt by myself. But I still don't understand the whole code. I don't understand when we can get $result which are not python list? I still don't understand that.
Uh oh!
There was an error while loading. Please reload this page.
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.
from the compiled code in swsscommon_wrap.cpp, it looks like result will not be PyList.
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.
I think you are right, swig should not own the pointer.
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.
@lguohan
Sorry. I wasn't clear enough. At the first, when I started working with the problem and I reproduce the issue, I found that Selectable object is leaking. Then I found the code, which maps from c++ to python. Play with the code to make sure that this code caused the issue.
Then I played with the code and found that I need to modify ref_cnt. I played with refcnt, and I got crashes. I read documentation and found that *_OWN flag could be the problem. I checked the documentation, sources and another projects and tried with 0 instead of OWN flag and found that it works and I don't see the issue anymore.
But the code I had was ugly, I can't explain it. After that I start looking in to different examples on github and found swig_python_append_output. I didn't find the document for the function but I found the source code and it did the same as my ugly code. So I replace my code with the functions and got this patch with only two lines replaced (one of the partially).
After that I tested the solution thrously for some time.