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

Unable to use the module in 2 threads (worker_threads) #411

Open
betandbuzz opened this issue May 2, 2019 · 4 comments
Open

Unable to use the module in 2 threads (worker_threads) #411

betandbuzz opened this issue May 2, 2019 · 4 comments

Comments

@betandbuzz
Copy link

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!

@laverdet
Copy link
Owner

laverdet commented May 2, 2019

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.

@alfonso-presa
Copy link

@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 :-).

@laverdet
Copy link
Owner

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.

@alfonso-presa
Copy link

wow, sounds like loads of fun. I'll give it a try....

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

No branches or pull requests

3 participants