-
Notifications
You must be signed in to change notification settings - Fork 274
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
[swig]: Fix swig template memory leak on issue 17025 #876
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,8 @@ | |
#include "zmqclient.h" | ||
#include "zmqconsumerstatetable.h" | ||
#include "zmqproducerstatetable.h" | ||
#include <memory> | ||
#include <functional> | ||
%} | ||
|
||
%include <std_string.i> | ||
|
@@ -156,31 +158,36 @@ | |
SWIG_Python_AppendOutput($result, temp); | ||
} | ||
|
||
%typemap(in, fragment="SWIG_AsPtr_std_string") | ||
%typemap(in, fragment="SWIG_AsVal_std_string") | ||
const std::vector<std::pair< std::string,std::string >,std::allocator< std::pair< std::string,std::string > > > & | ||
(std::vector< std::pair< std::string,std::string >,std::allocator< std::pair< std::string,std::string > > > temp, | ||
int res) { | ||
res = SWIG_OK; | ||
for (int i = 0; i < PySequence_Length($input); ++i) { | ||
temp.push_back(std::pair< std::string,std::string >()); | ||
PyObject *item = PySequence_GetItem($input, i); | ||
if (!PyTuple_Check(item) || PyTuple_Size(item) != 2) { | ||
std::unique_ptr<PyObject, std::function<void(PyObject *)> > item( | ||
PySequence_GetItem($input, i), | ||
[](PyObject *ptr){ | ||
Py_DECREF(ptr); | ||
}); | ||
if (!PyTuple_Check(item.get()) || PyTuple_Size(item.get()) != 2) { | ||
SWIG_fail; | ||
} | ||
PyObject *key = PyTuple_GetItem(item, 0); | ||
PyObject *value = PyTuple_GetItem(item, 1); | ||
std::string *ptr = (std::string *)0; | ||
PyObject *key = PyTuple_GetItem(item.get(), 0); | ||
PyObject *value = PyTuple_GetItem(item.get(), 1); | ||
std::string str; | ||
|
||
if (PyBytes_Check(key)) { | ||
temp.back().first.assign(PyBytes_AsString(key), PyBytes_Size(key)); | ||
} else if (SWIG_AsPtr_std_string(key, &ptr)) { | ||
temp.back().first = *ptr; | ||
} else if (SWIG_AsVal_std_string(key, &str) != SWIG_ERROR) { | ||
temp.back().first = str; | ||
} else { | ||
SWIG_fail; | ||
} | ||
if (PyBytes_Check(value)) { | ||
temp.back().second.assign(PyBytes_AsString(value), PyBytes_Size(value)); | ||
} else if (SWIG_AsPtr_std_string(value, &ptr)) { | ||
temp.back().second = *ptr; | ||
} else if (SWIG_AsVal_std_string(value, &str) != SWIG_ERROR) { | ||
temp.back().second = str; | ||
} else { | ||
SWIG_fail; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the if-elesif-else block is duplicated. Could you further refactor? #WontFix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel to extract a new function isn't a good idea in our case, same reason as above. |
||
|
@@ -191,13 +198,17 @@ | |
%typemap(typecheck) const std::vector< std::pair< std::string,std::string >,std::allocator< std::pair< std::string,std::string > > > &{ | ||
$1 = 1; | ||
for (int i = 0; i < PySequence_Length($input); ++i) { | ||
PyObject *item = PySequence_GetItem($input, i); | ||
if (!PyTuple_Check(item) || PyTuple_Size(item) != 2) { | ||
std::unique_ptr<PyObject, std::function<void(PyObject *)> > item( | ||
PySequence_GetItem($input, i), | ||
[](PyObject *ptr){ | ||
Py_DECREF(ptr); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This long statement is duplicated. Could you further refactor? #WontFix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Options I see:
1 is better. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like not a good idea to use a new class/function in SWIG code. For example, I tried to create a new file like
Because this function is generated in the swsscommon_wrap.c at compiling time, So I cannot access it in the outside. This function signature isn't fixed so I cannot use a forward-declaration yet. |
||
if (!PyTuple_Check(item.get()) || PyTuple_Size(item.get()) != 2) { | ||
$1 = 0; | ||
break; | ||
} | ||
PyObject *key = PyTuple_GetItem(item, 0); | ||
PyObject *value = PyTuple_GetItem(item, 1); | ||
PyObject *key = PyTuple_GetItem(item.get(), 0); | ||
PyObject *value = PyTuple_GetItem(item.get(), 1); | ||
if (!PyBytes_Check(key) | ||
&& !PyUnicode_Check(key) | ||
&& !PyString_Check(key) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import os | ||
import time | ||
import psutil | ||
import pytest | ||
import multiprocessing | ||
from threading import Thread | ||
|
@@ -851,3 +852,30 @@ def test_SmartSwitchDBConnector(): | |
assert tbl.get("dputest2")[1][1] == ("dashfield2", "dashvalue2") | ||
assert len(SonicDBConfig.getDbKeys()) == len(global_db_config_json["INCLUDES"]) | ||
|
||
|
||
def test_TableSetBinary(): | ||
app_db = swsscommon.DBConnector("APPL_DB", 0, True) | ||
t = swsscommon.Table(app_db, "TABLE") | ||
buff = b"" | ||
for i in range(0, 256): | ||
buff += bytes([i]) | ||
buff = buff.decode('latin-1') | ||
fvs = swsscommon.FieldValuePairs([("binary", buff)]) | ||
t.set("binary", fvs) | ||
(status, fvs) = t.get("binary") | ||
assert status == True | ||
assert fvs[0][1] == buff | ||
|
||
|
||
def test_TableOpsMemoryLeak(): | ||
OP_COUNT = 50000 | ||
app_db = swsscommon.DBConnector("APPL_DB", 0, True) | ||
t = swsscommon.Table(app_db, "TABLE") | ||
long_data = "x" * 100 | ||
fvs = swsscommon.FieldValuePairs([(long_data, long_data)]) | ||
rss = psutil.Process(os.getpid()).memory_info().rss | ||
for _ in range(OP_COUNT): | ||
t.set("long_data", fvs) | ||
t.get("long_data") | ||
assert psutil.Process(os.getpid()).memory_info().rss - rss < OP_COUNT | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Pterosaur There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be close to ZERO, but if there is a memory leak on the set/get OPs, we will got the following log:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Pterosaur if so, please delete the "long_data" key from the table and
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good idea, I will create another PR for teardown. |
||
|
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.
temp
is not a reference. Is it heavy copy? #ClosedThere 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.
It's not a function argument, just to tell the SWIG to generate a temp variable. The generated code looks like:
So, reference isn't available here.