Unable to use the module in 2 threads (worker_threads)
Hi,
I'm trying to use fibers with worker_threads:
"use strict";
const { isMainThread, Worker } = require("worker_threads");
const fibersInMainThread = true;
const fibersInWorker = true;
if (isMainThread) {
// main thread
if (fibersInMainThread) require("fibers");
const worker = new Worker(__filename);
worker.on("error", e => console.error(e));
console.log("main thread OK");
}
else {
// worker
if (fibersInWorker) require("fibers");
console.log("worker OK");
}
If I use fibers in both the main thread and the worker, I get the following error (it does not occur if I use fibers in a single context):
main thread OK
## There is an issue with `node-fibers` ##
`.../node_modules/fibers/bin/linux-x64-64-glibc/fibers.node` is missing.
Try running this to fix the issue: /home/ocoutin/.n/bin/node .../node_modules/fibers/build
Error: Missing binary. See message above.
at Object.<anonymous> (.../node_modules/fibers/fibers.js:23:9)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (.../worker_test.js:20:25)
at Module._compile (internal/modules/cjs/loader.js:689:30)
Is it a bug or is it a fiber limitation that allows its use in only one thread?
I'm using v10.8.0.
Thx!
Sorry, fibers isn't compatible with worker_threads. I took a look into what would be required to get this working and it's definitely possible but it would take a lot of rewriting.
@betandbuzz @laverdet I'm also looking forward this functionality. I have made a little rough attempt to get it working by using NODE_MODULE_INITIALIZER but I've got a segfault as result, I think that due to the statics and globals in the Fiber class. Did you guys took any steps in getting this working? Any guidance will be very welcome :-).
The main issue is statics/globals, yes. Some of these need to be defined per-thread, many need to be defined per-isolate, and some others should remain global but with locking. I suspect libcoro may also have issues with thread safety.
wow, sounds like loads of fun. I'll give it a try....