Closed shiftinv closed 1 year ago
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.
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: https://github.com/nodejs/node/issues/48353.This should fix the issue, which was also mentioned in this comment: https://github.com/laverdet/isolated-vm/issues/138#issuecomment-1077856679. 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 thread^1, 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-register[^2], 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.
[^2]: https://github.com/nodejs/node/blob/668437ccada33c03bb454a19c61b030c028076b0/src/node_binding.cc#L490-L492 + second paragraph in the docs