-
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 1 commit
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 |
---|---|---|
|
@@ -156,7 +156,7 @@ | |
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) { | ||
|
@@ -165,25 +165,30 @@ | |
temp.push_back(std::pair< std::string,std::string >()); | ||
PyObject *item = PySequence_GetItem($input, i); | ||
if (!PyTuple_Check(item) || PyTuple_Size(item) != 2) { | ||
Py_DECREF(item); | ||
SWIG_fail; | ||
} | ||
PyObject *key = PyTuple_GetItem(item, 0); | ||
PyObject *value = PyTuple_GetItem(item, 1); | ||
std::string *ptr = (std::string *)0; | ||
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 { | ||
Py_DECREF(item); | ||
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 { | ||
Py_DECREF(item); | ||
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. |
||
Py_DECREF(item); | ||
} | ||
$1 = &temp; | ||
} | ||
|
@@ -194,6 +199,7 @@ | |
PyObject *item = PySequence_GetItem($input, i); | ||
if (!PyTuple_Check(item) || PyTuple_Size(item) != 2) { | ||
$1 = 0; | ||
Py_DECREF(item); | ||
break; | ||
} | ||
PyObject *key = PyTuple_GetItem(item, 0); | ||
|
@@ -205,8 +211,10 @@ | |
&& !PyUnicode_Check(value) | ||
&& !PyString_Check(value)) { | ||
$1 = 0; | ||
Py_DECREF(item); | ||
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. 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. Done, pleas check it. |
||
break; | ||
} | ||
Py_DECREF(item); | ||
} | ||
} | ||
|
||
|
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") | ||
big_data = "x" * 10000 | ||
fvs = swsscommon.FieldValuePairs([(big_data, big_data)]) | ||
rss = psutil.Process(os.getpid()).memory_info().rss | ||
for _ in range(OP_COUNT): | ||
t.set("big_data", fvs) | ||
t.get("big_data") | ||
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 delete the "big_data" also after use? 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. Why delete it? I didn't test the del OPs, because the del OPs will not come to my SWIG code |
||
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.