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

Improve support for reloading module in multiple workers #402

Merged
merged 3 commits into from
Sep 25, 2023

Conversation

shiftinv
Copy link
Contributor

@shiftinv shiftinv commented Sep 12, 2023

Summary

Loading the module in one worker, closing that worker again, and then trying to load the module in a second worker fails with Error: Module did not self-register. From what I can tell, the core issue is this one: nodejs/node#48353.

This should fix the issue, which was also mentioned in this comment: #138 (comment). I'm however not too familiar with native Node.js addon development, so I'm unsure if this has any unintended side effects (it really shouldn't, though).

Details

Node.js unloads unreferenced native addons if not on the main thread1, but the shared object doesn't necessarily get fully unloaded from calling dlclose() (at least on Linux, due to... reasons?), which means it won't self-register when dlopen()ing it again from a second worker.

This doesn't happen if isolated-vm is also loaded in the main thread, since Node.js won't unload it in that case. This is the only real workaround I've found so far.

Node.js additionally looks for a well-known export when the module didn't self-register2, but isolated-vm didn't use the NODE_MODULE_INIT macro that produces this name, and instead relied on the self-register mechanism (which, to be fair, works fine in most cases).

Reproducible Code

(nodejs v18.17.1, ubuntu 22.04)

const { Worker } = require('worker_threads');

(async function () {
    for (let i = 0; i < 2; i++) {
        console.log(`creating worker ${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(`worker ${i} finished`);
    }
})().catch(console.error);
creating worker 0
worker 0 finished
creating worker 1
node:internal/modules/cjs/loader:1340
  return process.dlopen(module, path.toNamespacedPath(filename));
                 ^

Error: Module did not self-register: .../node_modules/isolated-vm/out/isolated_vm.node'.

Worth mentioning that the added testcase gets skipped on Windows, which seems to actually try to unload the module properly when the first worker exits, and runs into a segfault instead. Unrelated to the change in this PR, though.

Footnotes

  1. https://github.com/nodejs/node/blob/48fcb205e4577fd0096f6b772ca250c6dbf759e3/src/env.cc#L985-L995

  2. https://github.com/nodejs/node/blob/668437ccada33c03bb454a19c61b030c028076b0/src/node_binding.cc#L490-L492 + second paragraph in the docs

@laverdet
Copy link
Owner

Interesting. The linked nodejs is still open so I'm not sure where they will land on this issue. Honestly I'm surprised isolated-vm even survives dlclose and re-dlopen but I guess I did some things correct here. I'll send this out in the next minor revision and we can give it a shot.

@laverdet laverdet merged commit 01e027f into laverdet:main Sep 25, 2023
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants