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 for memory leak in Python target #1873

Merged
merged 9 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,15 @@ protected void deserialize(
value = action.getName();
FedNativePythonSerialization pickler = new FedNativePythonSerialization();
result.pr(pickler.generateNetworkDeserializerCode(value, null));
result.pr("lf_set(" + receiveRef + ", " + FedSerialization.deserializedVarName + ");\n");
// Use token to set ports and destructor
result.pr(
"lf_token_t* token = lf_new_token((void*)"
+ receiveRef
+ ", "
+ FedSerialization.deserializedVarName
+ ", 1);\n");
result.pr("lf_set_destructor(" + receiveRef + ", python_count_decrement);\n");
result.pr("lf_set_token(" + receiveRef + ", token);\n");
break;
}
case PROTO:
Expand Down Expand Up @@ -179,6 +187,8 @@ protected void serializeAndSend(
result.pr(pickler.generateNetworkSerializerCode(variableToSerialize, null));
result.pr("size_t message_length = " + lengthExpression + ";");
result.pr(sendingFunction + "(" + commonArgs + ", " + pointerExpression + ");\n");
// Decrease the reference count for serialized_pyobject
result.pr("Py_XDECREF(serialized_pyobject);\n");
break;
}
case PROTO:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public StringBuilder generateNetworkSerializerCode(String varName, String origin
serializerCode.append(
" lf_print_error_and_exit(\"Could not serialize " + serializedVarName + ".\");\n");
serializerCode.append("}\n");

return serializerCode;
}

Expand All @@ -106,7 +105,6 @@ public StringBuilder generateNetworkDeserializerCode(String varName, String targ
+ varName
+ "->token->length);\n");
// Deserialize using Pickle
deserializerCode.append("Py_XINCREF(message_byte_array);\n");
deserializerCode.append(
"PyObject* "
+ deserializedVarName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public String generateDelayBody(Action action, VarRef port) {
" lf_schedule_token(" + action.getName() + ", 0, " + ref + "->token);",
"}");
} else {
var value = "self->_lf_" + ref + "->value";
return String.join(
"\n",
"// Create a token.",
Expand All @@ -44,9 +45,10 @@ public String generateDelayBody(Action action, VarRef port) {
"#endif",
"lf_token_t* t = _lf_new_token((token_type_t*)"
+ action.getName()
+ ", self->_lf_"
+ ref
+ "->value, 1);",
+ ", "
+ value
+ ", 1);",
"Py_INCREF(" + value + ");",
"#if NUMBER_OF_WORKERS > 0",
"lf_mutex_unlock(&mutex);",
"#endif",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ private static String setUpMainTarget(
boolean hasMain, String executableName, Stream<String> cSources) {
return ("""
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_compile_definitions(_LF_GARBAGE_COLLECTED)
add_compile_definitions(_PYTHON_TARGET_ENABLED)
add_subdirectory(core)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
set(LF_MAIN_TARGET <pyModuleName>)
Expand Down
Loading