Skip to content

Commit 9bbc9a6

Browse files
committed
Host destruction working properly with nodejs.
1 parent 851cfee commit 9bbc9a6

File tree

9 files changed

+49
-21
lines changed

9 files changed

+49
-21
lines changed

source/loader/source/loader.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,18 @@ int loader_initialize_host(const loader_tag tag)
210210
return 1;
211211
}
212212

213-
return loader_impl_initialize(&loader_manager, p, plugin_impl_type(p, loader_impl));
213+
if (loader_impl_initialize(&loader_manager, p, plugin_impl_type(p, loader_impl)) != 0)
214+
{
215+
return 1;
216+
}
217+
else
218+
{
219+
loader_manager_impl manager_impl = plugin_manager_impl_type(&loader_manager, loader_manager_impl);
220+
221+
manager_impl->host = p;
222+
223+
return 0;
224+
}
214225
}
215226

216227
int loader_is_initialized(const loader_tag tag)

source/loaders/node_loader/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ target_link_libraries(${target}
158158
${META_PROJECT_NAME}::metacall # MetaCall library
159159

160160
# TODO: Implement delayed load
161-
# ${NodeJS_LIBRARY} # NodeJS library
161+
${NodeJS_LIBRARY} # NodeJS library
162162

163163
PUBLIC
164164
${DEFAULT_LIBRARIES}

source/loaders/node_loader/bootstrap/lib/bootstrap.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,12 @@ const startup = (impl, ptr, trampoline_exports) => {
436436
'await_function': node_loader_trampoline_await_function(trampoline),
437437
'await_future': node_loader_trampoline_await_future(trampoline),
438438
});
439+
440+
// This function must destroy all the loaders but
441+
// delaying the NodeJS Loader library unloading
442+
if (trampoline_exports) {
443+
process.on('exit', () => trampoline.destroy(node_loader_ptr));
444+
}
439445
} catch (ex) {
440446
console.log('Exception in bootstrap.js trampoline initialization:', ex);
441447
}

source/loaders/node_loader/source/node_loader_impl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4604,11 +4604,11 @@ int node_loader_impl_destroy(loader_impl impl)
46044604
return 1;
46054605
}
46064606

4607-
/* Call destroy function with thread safe */
4608-
node_loader_impl_try_destroy(node_impl);
4609-
46104607
if (loader_impl_get_option_host(impl) == 0)
46114608
{
4609+
/* Call destroy function with thread safe */
4610+
node_loader_impl_try_destroy(node_impl);
4611+
46124612
/* Wait for node thread to finish */
46134613
uv_thread_join(&node_impl->thread);
46144614
}

source/loaders/node_loader/source/node_loader_trampoline.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ napi_value node_loader_trampoline_reject(napi_env env, napi_callback_info info)
244244
napi_value node_loader_trampoline_destroy(napi_env env, napi_callback_info info)
245245
{
246246
napi_status status;
247-
248247
const size_t args_size = 1;
249248
size_t argc = args_size;
250249
napi_value recv;

source/metacall/source/metacall.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ static loader_path plugin_path = { 0 };
7272
static int metacall_plugin_extension_load(void);
7373
static void *metacallv_method(void *target, const char *name, method_invoke_ptr call, vector v, void *args[], size_t size);
7474
static type_id *metacall_type_ids(void *args[], size_t size);
75+
static void metacall_destructor(void);
7576
static void metacall_detour_destructor(void);
7677

7778
/* -- Costructors -- */
@@ -111,10 +112,18 @@ portability_constructor(metacall_constructor)
111112
metacall_value_destroy(config[0].options);
112113
exit(1);
113114
}
115+
116+
/* Register the destructor on exit */
117+
atexit(metacall_destructor);
114118
}
115119
}
116120
}
117121

122+
void metacall_destructor(void)
123+
{
124+
metacall_destroy();
125+
}
126+
118127
/* -- Methods -- */
119128

120129
const char *metacall_serial(void)

source/metacall/source/metacall_link.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ void *metacall_link_hook(void *handle, const char *symbol)
9292
/* Intercept function if any */
9393
void *ptr = set_get(metacall_link_table, (set_key)symbol);
9494

95-
log_write("metacall", LOG_LEVEL_DEBUG, "MetaCall detour link interception: %s -> %p", symbol, ptr);
95+
/* TODO: Disable logs here until log is completely thread safe and async signal safe */
96+
/* log_write("metacall", LOG_LEVEL_DEBUG, "MetaCall detour link interception: %s -> %p", symbol, ptr); */
9697

9798
if (ptr != NULL)
9899
{

source/ports/node_port/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ set(node_port_test_exec "${node_port_test}_executable")
225225
message(STATUS "Test ${node_port_test_exec}")
226226

227227
add_test(NAME ${node_port_test_exec}
228-
COMMAND ${NodeJS_EXECUTABLE} test/index.js
228+
COMMAND ${NodeJS_EXECUTABLE} -e "require('./test.js').main()"
229229
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
230230
)
231231

source/ports/node_port/index.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222

2323
const mod = require('module');
2424
const path = require('path');
25-
const fs = require('fs').promises;
25+
const fs = require('fs');
2626
const { URL } = require('url'); /* TODO: RPC Loader */
2727

28-
async function findFilesRecursively(dirPattern, filePattern, depthLimit = Infinity) {
28+
const findFilesRecursively = (dirPattern, filePattern, depthLimit = Infinity) => {
2929
const stack = [{ dir: dirPattern, depth: 0 }];
3030
const files = [];
3131
const dirRegex = new RegExp(dirPattern);
@@ -43,11 +43,11 @@ async function findFilesRecursively(dirPattern, filePattern, depthLimit = Infini
4343
continue;
4444
}
4545

46-
const items = await fs.readdir(dir);
46+
const items = fs.readdirSync(dir);
4747

4848
for (const item of items) {
4949
const fullPath = path.join(dir, item);
50-
const stat = await fs.stat(fullPath);
50+
const stat = fs.statSync(fullPath);
5151

5252
if (stat.isDirectory()) {
5353
stack.push({ dir: fullPath, depth: depth + 1 });
@@ -61,7 +61,7 @@ async function findFilesRecursively(dirPattern, filePattern, depthLimit = Infini
6161
}
6262

6363
return files;
64-
}
64+
};
6565

6666
const platformInstallPaths = () => {
6767
switch (process.platform) {
@@ -83,7 +83,7 @@ const platformInstallPaths = () => {
8383
}
8484

8585
throw new Error(`Platform ${process.platform} not supported`)
86-
}
86+
};
8787

8888
const searchPaths = () => {
8989
const customPath = process.env['METACALL_INSTALL_PATH'];
@@ -96,21 +96,21 @@ const searchPaths = () => {
9696
}
9797

9898
return platformInstallPaths()
99-
}
99+
};
100100

101-
const findLibrary = async () => {
101+
const findLibrary = () => {
102102
const searchData = searchPaths();
103103

104104
for (const p of searchData.paths) {
105-
const files = await findFilesRecursively(p, searchData.name, 0);
105+
const files = findFilesRecursively(p, searchData.name, 0);
106106

107107
if (files.length !== 0) {
108108
return files[0];
109109
}
110110
}
111111

112112
throw new Error('MetaCall library not found, if you have it in a special folder, define it through METACALL_INSTALL_PATH')
113-
}
113+
};
114114

115115
const addon = (() => {
116116
try {
@@ -127,7 +127,9 @@ const addon = (() => {
127127
*/
128128
process.env['METACALL_HOST'] = 'node';
129129

130-
findLibrary().then(library => {
130+
try {
131+
const library = findLibrary();
132+
131133
const { constants } = require('os');
132134
const m = { exports: {} };
133135

@@ -148,10 +150,10 @@ const addon = (() => {
148150
process.argv = argv;
149151

150152
return m.exports;
151-
}).catch(err => {
153+
} catch (err) {
152154
console.log(err);
153155
process.exit(1);
154-
});
156+
}
155157
}
156158
})();
157159

0 commit comments

Comments
 (0)