Skip to content

Commit

Permalink
Trying to solve more leaks from nodejs.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Jun 27, 2024
1 parent 02c7f28 commit e585bf5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 52 deletions.
53 changes: 2 additions & 51 deletions source/loaders/node_loader/source/node_loader_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,9 @@ static HMODULE (*get_module_handle_a_ptr)(_In_opt_ LPCSTR) = NULL; /* TODO: Impl

#if 1 // NODE_MAJOR_VERSION < 18
#if NODE_MAJOR_VERSION >= 12
#define node_loader_impl_register_module_id node::ModuleFlags::kLinked
#define node_loader_impl_register_module_id node::ModuleFlags::kLinked | 0x08 /* NM_F_DELETEME */
#else
#define node_loader_impl_register_module_id 0x02 /* NM_F_LINKED */
#define node_loader_impl_register_module_id 0x02 | 0x08 /* NM_F_LINKED | NM_F_DELETEME */
#endif

#define node_loader_impl_register_module(name, fn) \
Expand Down Expand Up @@ -2271,7 +2271,6 @@ void node_loader_impl_func_await_safe(napi_env env, loader_impl_async_func_await

void node_loader_impl_func_destroy_safe(napi_env env, loader_impl_async_func_destroy_safe_type *func_destroy_safe)
{
uint32_t ref_count = 0;
napi_handle_scope handle_scope;

/* Create scope */
Expand All @@ -2280,15 +2279,6 @@ void node_loader_impl_func_destroy_safe(napi_env env, loader_impl_async_func_des
node_loader_impl_exception(env, status);

/* Clear function persistent reference */
status = napi_reference_unref(env, func_destroy_safe->node_func->func_ref, &ref_count);

node_loader_impl_exception(env, status);

if (ref_count != 0)
{
/* TODO: Error handling */
}

status = napi_delete_reference(env, func_destroy_safe->node_func->func_ref);

node_loader_impl_exception(env, status);
Expand Down Expand Up @@ -2398,7 +2388,6 @@ void node_loader_impl_future_await_safe(napi_env env, loader_impl_async_future_a

void node_loader_impl_future_delete_safe(napi_env env, loader_impl_async_future_delete_safe_type *future_delete_safe)
{
uint32_t ref_count = 0;
napi_handle_scope handle_scope;

/* Create scope */
Expand All @@ -2407,15 +2396,6 @@ void node_loader_impl_future_delete_safe(napi_env env, loader_impl_async_future_
node_loader_impl_exception(env, status);

/* Clear promise reference */
status = napi_reference_unref(env, future_delete_safe->node_future->promise_ref, &ref_count);

node_loader_impl_exception(env, status);

if (ref_count != 0)
{
/* TODO: Error handling */
}

status = napi_delete_reference(env, future_delete_safe->node_future->promise_ref);

node_loader_impl_exception(env, status);
Expand Down Expand Up @@ -2626,7 +2606,6 @@ void node_loader_impl_clear_safe(napi_env env, loader_impl_async_clear_safe_type
napi_value clear_str_value;
bool result = false;
napi_handle_scope handle_scope;
uint32_t ref_count = 0;

/* Create scope */
napi_status status = napi_open_handle_scope(env, &handle_scope);
Expand Down Expand Up @@ -2685,15 +2664,6 @@ void node_loader_impl_clear_safe(napi_env env, loader_impl_async_clear_safe_type
}

/* Clear handle persistent reference */
status = napi_reference_unref(env, clear_safe->handle_ref, &ref_count);

node_loader_impl_exception(env, status);

if (ref_count != 0)
{
/* TODO: Error handling */
}

status = napi_delete_reference(env, clear_safe->handle_ref);

node_loader_impl_exception(env, status);
Expand Down Expand Up @@ -4486,7 +4456,6 @@ void node_loader_impl_walk(uv_handle_t *handle, void *arg)

void node_loader_impl_destroy_safe_impl(loader_impl_node node_impl, napi_env env)
{
uint32_t ref_count = 0;
napi_status status;
napi_handle_scope handle_scope;

Expand Down Expand Up @@ -4514,28 +4483,10 @@ void node_loader_impl_destroy_safe_impl(loader_impl_node node_impl, napi_env env
}

/* Clear persistent references */
status = napi_reference_unref(env, node_impl->global_ref, &ref_count);

node_loader_impl_exception(env, status);

if (ref_count != 0)
{
/* TODO: Error handling */
}

status = napi_delete_reference(env, node_impl->global_ref);

node_loader_impl_exception(env, status);

status = napi_reference_unref(env, node_impl->function_table_object_ref, &ref_count);

node_loader_impl_exception(env, status);

if (ref_count != 0)
{
/* TODO: Error handling */
}

status = napi_delete_reference(env, node_impl->function_table_object_ref);

node_loader_impl_exception(env, status);
Expand Down
13 changes: 12 additions & 1 deletion source/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ if(OPTION_TEST_MEMORYCHECK AND (OPTION_BUILD_ADDRESS_SANITIZER OR OPTION_BUILD_T
endif()

if(OPTION_TEST_MEMORYCHECK AND NOT (OPTION_BUILD_ADDRESS_SANITIZER OR OPTION_BUILD_THREAD_SANITIZER OR OPTION_BUILD_MEMORY_SANITIZER))
set(MEMORYCHECK_COMMAND_OPTIONS "${MEMORYCHECK_COMMAND_OPTIONS} --leak-check=full")
# set(MEMORYCHECK_COMMAND_OPTIONS "${MEMORYCHECK_COMMAND_OPTIONS} --show-leak-kinds=all")
set(MEMORYCHECK_COMMAND_OPTIONS "${MEMORYCHECK_COMMAND_OPTIONS} --leak-check=full")
set(MEMORYCHECK_COMMAND_OPTIONS "${MEMORYCHECK_COMMAND_OPTIONS} --trace-children=yes")
set(MEMORYCHECK_COMMAND_OPTIONS "${MEMORYCHECK_COMMAND_OPTIONS} --show-reachable=yes")
set(MEMORYCHECK_COMMAND_OPTIONS "${MEMORYCHECK_COMMAND_OPTIONS} --track-origins=yes")
Expand All @@ -64,6 +64,11 @@ if(OPTION_TEST_MEMORYCHECK AND NOT (OPTION_BUILD_ADDRESS_SANITIZER OR OPTION_BUI
set(MEMORYCHECK_COMMAND_OPTIONS "${MEMORYCHECK_COMMAND_OPTIONS} --suppressions=${CMAKE_CURRENT_SOURCE_DIR}/memcheck/valgrind-python.supp")
set(MEMORYCHECK_COMMAND_OPTIONS "${MEMORYCHECK_COMMAND_OPTIONS} --suppressions=${CMAKE_CURRENT_SOURCE_DIR}/memcheck/valgrind-node.supp")
set(MEMORYCHECK_COMMAND_OPTIONS "${MEMORYCHECK_COMMAND_OPTIONS} --suppressions=${CMAKE_CURRENT_SOURCE_DIR}/memcheck/valgrind-wasm.supp")
set(MEMORYCHECK_COMMAND_OPTIONS "${MEMORYCHECK_COMMAND_OPTIONS} --suppressions=${CMAKE_CURRENT_SOURCE_DIR}/memcheck/valgrind-wasm.supp")

# TODO: Implement automatic detection for valgrind suppressions and create a proper test suite for the CI
# set(MEMORYCHECK_COMMAND_OPTIONS "${MEMORYCHECK_COMMAND_OPTIONS} --suppressions=/usr/lib/valgrind/python3.supp")
# set(MEMORYCHECK_COMMAND_OPTIONS "${MEMORYCHECK_COMMAND_OPTIONS} --suppressions=/usr/lib/valgrind/debian.supp")

# TODO: Memory check does not work properly with CoreCLR
#
Expand All @@ -88,6 +93,12 @@ if(OPTION_TEST_MEMORYCHECK AND NOT (OPTION_BUILD_ADDRESS_SANITIZER OR OPTION_BUI
--timeout 7200
COMMAND ${CMAKE_COMMAND} -E cat "${CMAKE_BINARY_DIR}/Testing/Temporary/MemoryChecker.*.log"
)

# This is needed in order to allow valgrind to properly track malloc in Python
set(TESTS_ENVIRONMENT_VARIABLES
${TESTS_ENVIRONMENT_VARIABLES}
"PYTHONMALLOC=malloc"
)
endif()

#
Expand Down

0 comments on commit e585bf5

Please sign in to comment.