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

lib: guard inspector console using process var #15008

Closed
wants to merge 1 commit into from

Conversation

danbev
Copy link
Contributor

@danbev danbev commented Aug 24, 2017

Currently the inspector module is always loaded and if it does not
return anything the inspector console setup is skipped.

This commit uses the process.config.variables.v8_enable_inspector
variable to only load the inspector module if it is enabled.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

lib

@nodejs-github-bot nodejs-github-bot added the lib / src Issues and PRs related to general changes in the lib or src directory. label Aug 24, 2017
@danbev
Copy link
Contributor Author

danbev commented Aug 24, 2017

@jasnell jasnell requested a review from bnoordhuis August 24, 2017 15:19
console.timeEnd;
console.trace;
console.warn;
if (process.config.v8_enable_inspector) {
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice to move that condition up in the former if.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, I'll move it. Thanks

@danbev danbev force-pushed the guard-eager-console-init branch from 17a73ac to b17c383 Compare September 1, 2017 05:46
@danbev
Copy link
Contributor Author

danbev commented Sep 1, 2017

@@ -54,7 +54,7 @@
NativeModule.require('internal/process/warning').setup();
NativeModule.require('internal/process/next_tick').setup();
NativeModule.require('internal/process/stdio').setup();
if (browserGlobals) {
if (browserGlobals && process.config.v8_enable_inspector) {
Copy link
Member

Choose a reason for hiding this comment

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

I think the name is actually process.config.variables.v8_enable_inspector?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right, can't believe I missed that. 😞 Will update now. Thanks again!

@BridgeAR
Copy link
Member

BridgeAR commented Sep 1, 2017

You asked for the reason - If I am correct NativeModule.require('module') would have to be called first and that is not the case anymore.

@danbev
Copy link
Contributor Author

danbev commented Sep 1, 2017

If I am correct NativeModule.require('module') would have to be called first and that is not the case anymore.

Thanks! Should we then perhaps do something like the following to still allow for the eager instantiation to happen:

    if (browserGlobals) {
      if (!process.config.variables.v8_enable_inspector) {
        NativeModule.require('module');
      }
      // Instantiate eagerly in case the first call is under stack overflow
      // conditions where instantiation doesn't work.
      const console = global.console;
      console.assert;
      console.clear;
      console.count;
      console.countReset;
      console.dir;
      console.error;
      console.log;
      console.time;
      console.timeEnd;
      console.trace;
      console.warn;
    }

@BridgeAR
Copy link
Member

BridgeAR commented Sep 2, 2017

I would say let us wait for #15111. It should actually also solve the issue here and allow the eager instantiation. I do like the variable part a lot though and I guess it makes sense to switch the inspector check in the mentioned PR to this check instead.

@BridgeAR BridgeAR added the blocked PRs that are blocked by other issues or PRs. label Sep 13, 2017
@BridgeAR BridgeAR mentioned this pull request Sep 15, 2017
4 tasks
@BridgeAR
Copy link
Member

@danbev the mentioned PR just landed. I guess the issue itself should already be fixed but right now the inspector is always "loaded" and it is checked if it actually returns anything or not. Therefore it would still be nice to use process.config.variables.v8_enable_inspector instead. I did not add that to my PR as I forgot about that.

@BridgeAR BridgeAR removed the blocked PRs that are blocked by other issues or PRs. label Sep 19, 2017
@danbev
Copy link
Contributor Author

danbev commented Sep 25, 2017

@BridgeAR Sorry about the late reply (was at a f2f last week and had limited time). I'll take another look into this issue now. Thanks

@danbev danbev force-pushed the guard-eager-console-init branch from 436dd13 to 87b1b97 Compare September 25, 2017 07:16
@danbev danbev changed the title lib: guard console instantiation --wihtout-ssl lib: guard inspector console using process var Sep 25, 2017
@danbev
Copy link
Contributor Author

danbev commented Sep 25, 2017

Rebased and updated CI: https://ci.nodejs.org/job/node-test-pull-request/10249/

@@ -319,10 +319,10 @@
}

function setupInspector(originalConsole, wrappedConsole, Module) {
const { addCommandLineAPI, consoleCall } = process.binding('inspector');
if (!consoleCall) {
if (!process.config.variables.v8_enable_inspector) {
Copy link
Member

Choose a reason for hiding this comment

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

Depending on process.config is dangerous because there are userland modules that completely replace it. During bootstrap it should be safe but problems still could come up.

Copy link
Contributor

Choose a reason for hiding this comment

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

If it ever becomes an issue, we could cache it before user code runs.

Copy link
Member

Choose a reason for hiding this comment

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

Or use process.binding('config') as a stable alternative.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jasnell @cjihrig Thanks, I had not thought about that being an issue and I'm trying to understand how this might happen.

Using process.binding('config') will give access to the builtin module config, but as far as I can tell the information from config.gypi is provided as a native module (via node_js2c). The process object is then configured with that information in setupConfig which is called by node_bootstrap.js start function.

setupConfig deletes the config from the _source and then sets these properties on the process object. Since it deletes the _source this function cannot be called multiple times, for example if it did not we could have done the same thing again.

What I'm having some difficulties understand is that how could a userland module be able to replace this at this stage. Would someone be able to shed some light on this?

Copy link
Member

Choose a reason for hiding this comment

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

Userland code might not be able to replace process.config by this stage, I haven't tested it. What I do know is that we've had issues depending on process.config in later stages elsewhere in the code and we if we are going to use it here then we need to be certain that it won't be a problem. If there is no userland code that can be run before bootstrap gets to this point, then it should be ok.

Copy link
Member

Choose a reason for hiding this comment

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

This is still very early in the bootstrap process and I do not think it is possible to change it at that stage. To be on the safe side we could just add a test for it though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jasnell @BridgeAR Sounds good, I'll add a test for this. Thanks

@danbev danbev force-pushed the guard-eager-console-init branch from 87b1b97 to 5a94460 Compare September 29, 2017 07:46
const assert = require('assert');
assert.strictEqual(process.config.variables.v8_enable_inspector, 1);
process.config.variables.v8_enable_inspector = 100;
assert.strictEqual(process.config.variables.v8_enable_inspector, 100);
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure if this is a appropriate test.
I would have expected the v8_enable_inspector variable to be set to something falsy because we test it to be truthy and afterwards to check if the inspector is set up nevertheless.
Another approach would be to test that inspector is set without any variable manipulation. That should already verify that manipulating it has no impact on this anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@BridgeAR Thanks, I've updated the test. When going through the code I looks like any user land code will be call by Module.runMain, at which point the checking of process.config.variables.v8_enable_inspector done by bootstrap_node.js has already been performed. This also holds true for any preloaded modules. So as far as I can tell the check should be safe unless there are other ways this could happen that I've not taken into account.

@danbev
Copy link
Contributor Author

danbev commented Oct 5, 2017

@BridgeAR Would you mind taking another look at this now and see what you thing? Thanks

@danbev
Copy link
Contributor Author

danbev commented Oct 17, 2017

@BridgeAR ping

const assert = require('assert');
assert.strictEqual(process.config.variables.v8_enable_inspector, 1);
process.config.variables.v8_enable_inspector = null;
assert.strictEqual(process.config.variables.v8_enable_inspector, null);
Copy link
Member

Choose a reason for hiding this comment

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

How do we know here that the inspector is indeed set up properly (I would have expected to test for inspector specific functions or similar)? That is the most important part out of my perspective. As soon as we know that the inspector is set up, we do not even have to manipulate the variable anymore, because it would not have any effect afterwards.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@BridgeAR Thanks for the feedback. I'm not working for a few days but will take another look at this next week.

@jasnell
Copy link
Member

jasnell commented Oct 20, 2017

Thank you for adding the test, but I'm not quite sure it's enough. The earliest user code that can run would be a preload module using -r. The test could likely be improved by having it run with a preload module that sets process.config = {} then checking that the inspector code still works as required.

@danbev danbev force-pushed the guard-eager-console-init branch from 6c47862 to 9395fca Compare October 24, 2017 11:47
@danbev
Copy link
Contributor Author

danbev commented Oct 24, 2017

@BridgeAR @jasnell Sorry about the delay on this. I've taken another stab at this now, please take a look when you get a chance and let me know what you think.

@danbev
Copy link
Contributor Author

danbev commented Nov 9, 2017

@jasnell Would you able to take another look at the test for this and see what you think?

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.

Still LGTM.

Currently the inspector module is always loaded and if it does not
return anything the inspector console setup is skipped.

This commit uses the process.config.variables.v8_enable_inspector
variable to only load the inspector module if it is enabled.
@danbev danbev force-pushed the guard-eager-console-init branch from 9395fca to 87f3f91 Compare November 13, 2017 06:29
@danbev
Copy link
Contributor Author

danbev commented Nov 13, 2017

@danbev
Copy link
Contributor Author

danbev commented Nov 13, 2017

Landed in 1aac4c1

@danbev danbev closed this Nov 13, 2017
danbev added a commit that referenced this pull request Nov 13, 2017
Currently the inspector module is always loaded and if it does not
return anything the inspector console setup is skipped.

This commit uses the process.config.variables.v8_enable_inspector
variable to only load the inspector module if it is enabled.

PR-URL: #15008
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
@danbev danbev deleted the guard-eager-console-init branch November 13, 2017 07:51
evanlucas pushed a commit that referenced this pull request Nov 13, 2017
Currently the inspector module is always loaded and if it does not
return anything the inspector console setup is skipped.

This commit uses the process.config.variables.v8_enable_inspector
variable to only load the inspector module if it is enabled.

PR-URL: #15008
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
@evanlucas evanlucas mentioned this pull request Nov 13, 2017
MylesBorins pushed a commit that referenced this pull request Nov 17, 2017
Currently the inspector module is always loaded and if it does not
return anything the inspector console setup is skipped.

This commit uses the process.config.variables.v8_enable_inspector
variable to only load the inspector module if it is enabled.

PR-URL: #15008
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
@gibfahn gibfahn mentioned this pull request Nov 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib / src Issues and PRs related to general changes in the lib or src directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants