Improve support for reloading module in multiple workers #402
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 whendlopen()
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)
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
https://github.com/nodejs/node/blob/48fcb205e4577fd0096f6b772ca250c6dbf759e3/src/env.cc#L985-L995 ↩
https://github.com/nodejs/node/blob/668437ccada33c03bb454a19c61b030c028076b0/src/node_binding.cc#L490-L492 + second paragraph in the docs ↩