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

Cannot be used in Worker threads (Node 10.x) #204

Closed
RomainMuller opened this issue Feb 8, 2021 · 4 comments · Fixed by cdk8s-team/cdk8s-operator#228
Closed

Cannot be used in Worker threads (Node 10.x) #204

RomainMuller opened this issue Feb 8, 2021 · 4 comments · Fixed by cdk8s-team/cdk8s-operator#228

Comments

@RomainMuller
Copy link
Contributor

The module cannot be loaded within a Worker thread, as process.chdir is not available (undefined) in there. The following line causes a TypeError: Object prototype may only be an Object or null: undefined:

if (Object.setPrototypeOf) Object.setPrototypeOf(process.chdir, chdir)

I reckon the solution would be to have graceful-fs also not provide chdir in this scenario.

RomainMuller added a commit to RomainMuller/node-graceful-fs that referenced this issue Feb 8, 2021
RomainMuller added a commit to aws/jsii that referenced this issue Feb 8, 2021
This works around isaacs/node-graceful-fs#204, where `graceful-fs`
cannot be loaded from within `Worker` threads. Since the language
generators are loaded in worker threads whenever they are available,
they cannot use `fs-extra` which itself uses `graceful-fs`.

This is hopefully a temporary measure, until isaacs/node-graceful-fs#205
is merged & released.
@vijayshukla30
Copy link

When you going to release this fix getting error not able to even start the application.
Screenshot 2021-02-08 at 8 19 08 PM

mergify bot pushed a commit to aws/jsii that referenced this issue Feb 9, 2021
This works around isaacs/node-graceful-fs#204, where `graceful-fs`
cannot be loaded from within `Worker` threads. Since the language
generators are loaded in worker threads whenever they are available,
they cannot use `fs-extra` which itself uses `graceful-fs`.
    
This is hopefully a temporary measure, until isaacs/node-graceful-fs#205
is merged & released.

---

By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license].

[Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
@coreyfarrell
Copy link
Collaborator

What version of node.js does this effect? In my tests typeof process.chdir === 'function' from a worker thread.

@RomainMuller
Copy link
Contributor Author

RomainMuller commented Feb 9, 2021

At least in node 10, as per https://nodejs.org/docs/latest-v10.x/api/process.html#process_process_chdir_directory:

This feature is not available in Worker threads.

Although the same note still stands on the latest release, although it appears to be defined there:

/// worker-demo.js

const { Worker, isMainThread, parentPort } = require('worker_threads');

if (isMainThread) {
  const worker = new Worker(__filename);
  worker.on('message', ({ chdir }) => console.error(`(${process.version}) process.chdir is a ${chdir}`))
} else {
  parentPort.postMessage({ chdir: typeof process.chdir });
}
$ /usr/local/opt/node@10/bin/node --experimental-worker worker-demo.js
(v10.23.2) process.chdir is a undefined

$ /usr/local/opt/node@12/bin/node --experimental-worker worker-demo.js
(v12.20.1) process.chdir is a function

@RomainMuller RomainMuller changed the title Cannot be used in Worker threads Cannot be used in Worker threads (Node 10.x) Feb 9, 2021
RomainMuller added a commit to aws/jsii that referenced this issue Feb 9, 2021
isaacs/node-graceful-fs#204 prevents `graceful-fs` (a dependency of
`fs-extra`) from loading in a worker thread when using node < 12. The
issue affects only version `4.2.5`, so pinning both `jsii-rosetta` and
`jsii-pacmak` (both of which may use worker threads when available) to
the last known good version: `4.2.4`, until a fixed release of
`graceful-fs` is released.
RomainMuller added a commit to aws/jsii that referenced this issue Feb 9, 2021
…2554)

isaacs/node-graceful-fs#204 prevents `graceful-fs` (a dependency of
`fs-extra`) from loading in a worker thread when using node < 12. The
issue affects only version `4.2.5`, so pinning both `jsii-rosetta` and
`jsii-pacmak` (both of which may use worker threads when available) to
the last known good version: `4.2.4`, until a fixed release of
`graceful-fs` is released.
@isaacs isaacs closed this as completed in f762c74 Feb 9, 2021
@mf-corey
Copy link

Thanks for the quick fix on this issue. I encountered it as well, it was preventing my Electron app from starting in node 10.20.1. The fix merged in this PR is the correct strategy, but I had luck launching my app with this small stop-gap that swallows the error... Use at your own risk.

var originalSetPrototypeOf = Object.setPrototypeOf;
Object.setPrototypeOf = function(a, b) {
  try {
    return originalSetPrototypeOf.apply(null, arguments);
  } catch (_error) {
    return console.error(_error);
  }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment