Skip to content

Commit

Permalink
Solve bug of metacall_inspect called after the destroy of node loader…
Browse files Browse the repository at this point in the history
… has been issued. The destroy method of the node loader should be executed before any destruction of the loader structure itself.
  • Loading branch information
viferga committed Apr 27, 2021
1 parent d4bc051 commit 0a8e426
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
18 changes: 10 additions & 8 deletions source/loader/source/loader_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ void loader_impl_destroy_handle(loader_handle_impl handle_impl)

if (handle_impl->impl->init == 0)
{
log_write("metacall", LOG_LEVEL_DEBUG, "Destroying handle %s", handle_impl->name);

if (loader_impl_function_hook_call(handle_impl->ctx, func_fini_name) != 0)
{
log_write("metacall", LOG_LEVEL_ERROR, "Error when calling destructor from handle impl: %p (%s)", (void *)handle_impl, func_fini_name);
Expand Down Expand Up @@ -1111,12 +1113,6 @@ void loader_impl_destroy(loader_impl impl)
{
log_write("metacall", LOG_LEVEL_DEBUG, "Destroy loader implementation %s", impl->tag);

set_iterate(impl->handle_impl_map, &loader_impl_destroy_handle_map_cb_iterate, NULL);

set_iterate(impl->type_info_map, &loader_impl_destroy_type_map_cb_iterate, NULL);

set_destroy(impl->type_info_map);

if (impl->init == 0)
{
loader_impl_interface interface_impl = loader_impl_symbol(impl);
Expand All @@ -1129,12 +1125,18 @@ void loader_impl_destroy(loader_impl impl)
impl->init = 1;
}

set_iterate(impl->handle_impl_map, &loader_impl_destroy_handle_map_cb_iterate, NULL);

set_destroy(impl->handle_impl_map);

set_iterate(impl->type_info_map, &loader_impl_destroy_type_map_cb_iterate, NULL);

set_destroy(impl->type_info_map);

set_iterate(impl->exec_path_map, &loader_impl_destroy_exec_path_map_cb_iterate, NULL);

set_destroy(impl->exec_path_map);

set_destroy(impl->handle_impl_map);

context_destroy(impl->ctx);

loader_impl_dynlink_destroy(impl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ TEST_F(metacall_node_python_async_after_destroy_test, DefaultConstructor)
{
static const char buffer[] =
/* NodeJS */
"const { metacall, metacall_load_from_memory } = require('" METACALL_NODE_PORT_PATH "');\n"
"const { metacall, metacall_load_from_memory, metacall_inspect } = require('" METACALL_NODE_PORT_PATH "');\n"
"metacall_load_from_memory('py', `"
/* Python */
"def sum(a, b):\n"
" return a + b\n"
"`);\n"
"function log(x) { console.log(x); return x; }\n"
"setTimeout(() => { log(metacall('sum', 3, 4)) === 7 || process.exit(1) }, 2000);\n"
"setTimeout(() => { log(metacall('sum', 3, 4)) === 7 || process.exit(1) }, 4000);\n"
"setTimeout(() => { log(metacall_inspect()) || process.exit(1) }, 4000);\n"
"setTimeout(() => { log(metacall('sum', 3, 4)) === 7 || process.exit(1) }, 6000);\n";

ASSERT_EQ((int)0, (int)metacall_load_from_memory("node", buffer, sizeof(buffer), NULL));
Expand Down

0 comments on commit 0a8e426

Please sign in to comment.