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

src: add buildflag to force context-aware addons #29631

Closed

Conversation

codebytere
Copy link
Member

@codebytere codebytere commented Sep 20, 2019

This PR adds a new cli flag --force-context-aware whose purpose is to prevent usage of native node addons that aren't context aware. In an embedder context, loading native addons ahas always been challenging due to the need to rebuild, to link to the right node.lib/dll, and to ensure the NODE_MODULE_VERSION is correct.

Since Node doesn't let you load multiple instances of a native module in the same process, even in different v8 contexts, this creates a host of problems for multi-process framework like Electron.

This new flag would allow Electron and other embedders to enforce restrictions more cleanly and prevent unexpected behavior.

More context for embedders can be found here: #18397

cc @MarshallOfSound

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. labels Sep 20, 2019
@codebytere codebytere force-pushed the warn-prevent-context-aware branch 2 times, most recently from aaa73db to 469bdbe Compare September 20, 2019 16:31
doc/api/cli.md Outdated Show resolved Hide resolved
Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM, although a test would be great.

Since Node doesn't let you load multiple instances of a native module in the same process, even in different v8 contexts, this creates a host of problems for multi-process framework like Electron.

Just to make sure there’s no misunderstanding, this is the case for only non-context-aware addons, and that is the point of this PR, right?

src/env.cc Outdated Show resolved Hide resolved
src/node_binding.cc Outdated Show resolved Hide resolved
src/node_options.cc Outdated Show resolved Hide resolved
@addaleax addaleax added addons Issues and PRs related to native addons. semver-minor PRs that contain new features and should be released in the next minor version. and removed lib / src Issues and PRs related to general changes in the lib or src directory. labels Sep 20, 2019
@codebytere
Copy link
Member Author

Yes indeed! Only the case for non-context-aware addons. I'll add a test as soon as i can :)

doc/api/errors.md Outdated Show resolved Hide resolved
src/node_binding.cc Show resolved Hide resolved
@codebytere codebytere force-pushed the warn-prevent-context-aware branch 2 times, most recently from 6c5909f to 93d2420 Compare September 21, 2019 04:02
@codebytere
Copy link
Member Author

@addaleax i think (?) my test should cover it, it's definitely not the prettiest test i've ever written so please let me know if you think there's a better pattern for testing this kind of cli flag!

@codebytere codebytere force-pushed the warn-prevent-context-aware branch 3 times, most recently from 3ac2c4a to c5efc2a Compare September 21, 2019 05:01
Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the test looks good to me too :)

@nodejs-github-bot
Copy link
Collaborator

Copy link
Member

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A test that checks loading a context-aware add-on works would be nice too. You can co-opt one of the other context-aware add-on tests if you like, e.g.:

diff --git a/test/addons/zlib-binding/test.js b/test/addons/zlib-binding/test.js
index b50817aa5d..7a488c9a3b 100644
--- a/test/addons/zlib-binding/test.js
+++ b/test/addons/zlib-binding/test.js
@@ -1,3 +1,4 @@
+// Flags: --force-context-aware
 'use strict';
 
 const common = require('../../common');

doc/api/cli.md Outdated Show resolved Hide resolved
test/addons/force-context-aware/binding.cc Outdated Show resolved Hide resolved
test/addons/force-context-aware/test.js Outdated Show resolved Hide resolved
@Trott Trott added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Sep 24, 2019
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

Trott pushed a commit that referenced this pull request Sep 25, 2019
PR-URL: #29631
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Trott pushed a commit that referenced this pull request Sep 25, 2019
PR-URL: #29631
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
@Trott
Copy link
Member

Trott commented Sep 25, 2019

Landed in 5058c7f...f120e6d

@Trott Trott closed this Sep 25, 2019
@codebytere codebytere deleted the warn-prevent-context-aware branch September 25, 2019 10:46
<a id="ERR_NON_CONTEXT_AWARE_DISABLED"></a>
### ERR_NON_CONTEXT_AWARE_DISABLED

A non-context-aware native addon was loaded in a process that disallows them.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

including a link here for context-aware like in the cli.md file would be helpful

@@ -393,6 +393,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
"silence all process warnings",
&EnvironmentOptions::no_warnings,
kAllowedInEnvironment);
AddOption("--force-context-aware",
"disable loading non-context-aware addons",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extremely minor take it or leave it nit.. but the double negative here feels odd ... "disable ... non" ... perhaps "allow only context-aware addons" as an alternative?

BridgeAR pushed a commit that referenced this pull request Oct 9, 2019
PR-URL: #29631
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
BridgeAR pushed a commit that referenced this pull request Oct 9, 2019
PR-URL: #29631
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
@BridgeAR BridgeAR mentioned this pull request Oct 10, 2019
BridgeAR added a commit that referenced this pull request Oct 10, 2019
Notable changes:

* build:
  * Add `--force-context-aware` flag to prevent usage of native node
    addons that aren't context aware
    #29631
* deprecations:
  * Add documentation-only deprecation for `process._tickCallback()`
    #29781
* esm:
  * Using JSON modules is experimental again
    #29754
* fs:
  * Introduce `opendir()` and `fs.Dir` to iterate through directories
    #29349
* process:
  * Add source-map support to stack traces by using
    `--source-map-support` #29564
* tls:
  * Honor `pauseOnConnect` option
    #29635
  * Add option for private keys for OpenSSL engines
    #28973

PR-URL: #29919
BridgeAR added a commit that referenced this pull request Oct 11, 2019
Notable changes:

* build:
  * Add `--force-context-aware` flag to prevent usage of native node
    addons that aren't context aware
    #29631
* deprecations:
  * Add documentation-only deprecation for `process._tickCallback()`
    #29781
* esm:
  * Using JSON modules is experimental again
    #29754
* fs:
  * Introduce `opendir()` and `fs.Dir` to iterate through directories
    #29349
* process:
  * Add source-map support to stack traces by using
    `--source-map-support` #29564
* tls:
  * Honor `pauseOnConnect` option
    #29635
  * Add option for private keys for OpenSSL engines
    #28973

PR-URL: #29919
BridgeAR added a commit that referenced this pull request Oct 11, 2019
Notable changes:

* build:
  * Add `--force-context-aware` flag to prevent usage of native node
    addons that aren't context aware
    #29631
* deprecations:
  * Add documentation-only deprecation for `process._tickCallback()`
    #29781
* esm:
  * Using JSON modules is experimental again
    #29754
* fs:
  * Introduce `opendir()` and `fs.Dir` to iterate through directories
    #29349
* process:
  * Add source-map support to stack traces by using
    `--source-map-support` #29564
* tls:
  * Honor `pauseOnConnect` option
    #29635
  * Add option for private keys for OpenSSL engines
    #28973

PR-URL: #29919
dhritzkiv added a commit to stackgl/headless-gl that referenced this pull request Dec 31, 2019
dhritzkiv added a commit to stackgl/headless-gl that referenced this pull request Jan 7, 2020
codebytere added a commit to electron/electron that referenced this pull request Sep 21, 2020
codebytere added a commit to electron/electron that referenced this pull request Sep 21, 2020
codebytere added a commit to electron/electron that referenced this pull request Sep 21, 2020
codebytere added a commit to electron/electron that referenced this pull request Sep 21, 2020
jacobthesumurai pushed a commit to noblesamurai/headless-gl that referenced this pull request Jul 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addons Issues and PRs related to native addons. author ready PRs that have at least one approval, no pending requests for changes, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants