Skip to content

Commit

Permalink
Improve support for reloading module in multiple workers (#402)
Browse files Browse the repository at this point in the history
* fix: use `NODE_MODULE_INIT` macro to improve worker support

* test: add regression test

* fix(test): skip test on windows, since it actually unloads the module (unlike linux) and segfaults
  • Loading branch information
shiftinv authored Sep 25, 2023
1 parent 202b4b2 commit 01e027f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/module/isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,6 @@ void init(Local<Object> target) {

} // namespace ivm

NODE_MODULE_CONTEXT_AWARE(isolated_vm, ivm::init) // NOLINT
NODE_MODULE_INIT(/* exports, module, context */) {
ivm::init(exports);
}
18 changes: 18 additions & 0 deletions tests/worker-reload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { Worker } = require('worker_threads');

if (process.platform === 'win32') {
// unloading the module segfaults on Windows, skip for now
console.log('pass');
process.exit();
}

(async function () {
for (let i = 0; i < 2; i++) {
const worker = new Worker("require('isolated-vm')", { eval: true });
await new Promise((resolve, reject) => {
worker.on('exit', code => code ? reject(new Error(`Worker exited with code: ${code}`)) : resolve());
worker.on('error', error => reject(error));
});
}
console.log('pass')
})().catch(console.error);

0 comments on commit 01e027f

Please sign in to comment.