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

vm writing to stderr #2373

Closed
reggi opened this issue Aug 14, 2015 · 11 comments
Closed

vm writing to stderr #2373

reggi opened this issue Aug 14, 2015 · 11 comments
Labels
invalid Issues and PRs that are invalid. question Issues that look for answers. vm Issues and PRs related to the vm subsystem.

Comments

@reggi
Copy link
Contributor

reggi commented Aug 14, 2015

The issue is that the code below writes to stderr. Because it's wrapped in a try catch it shouldn't be writing to stderr.

try {
  var vm = require('vm')
  global.require = require
  vm.createScript("require('missing')")
  .runInNewContext(global)
} catch (e) {
}

It only writes this. Not even a complete throw w/ error stack trace.


module.js:338
    throw err;
          ^

I've tried to catch it with this, but it never fires.

process.on('unhandledRejection', function(reason, p){
  console.log(arguments)
})

The code example is here

The tests are here, it only started happening in version 11+.

  • node_js:
    • "0.12" ×
    • "0.11" ×
    • "0.10" ✓
    • "0.8" ✓
    • "0.6" ✓
    • "iojs" ×
    • "iojs-v1.0.4" ×
    • "iojs-v2.5.0" ×
    • "iojs-v3.0.0" ×
@thefourtheye thefourtheye added question Issues that look for answers. vm Issues and PRs related to the vm subsystem. labels Aug 14, 2015
@reggi
Copy link
Contributor Author

reggi commented Aug 14, 2015

@thefourtheye this is a bug, no?

@thefourtheye
Copy link
Contributor

I am not sure if it is related to #2104. cc @indutny @domenic

@reggi
Copy link
Contributor Author

reggi commented Aug 14, 2015

@thefourtheye I tested it out and I was editing and executing different files, I removed it. Sorry.

@reggi
Copy link
Contributor Author

reggi commented Aug 14, 2015

This is interesting.

try {
  var vm = require('vm')
  global.require = require
  var code = [
    'try {',
    'require("missing")',
    '} catch (e) {',
    'throw e ',
    '}'
  ].join('\n')
  vm.createScript(code)
  .runInNewContext(global)
} catch (e) {
}

Writes

evalmachine.<anonymous>:4
 throw e 
       ^

@reggi
Copy link
Contributor Author

reggi commented Aug 14, 2015

Ugg, it has nothing to do explicitly with require.

try {
  var vm = require('vm')
  vm.createScript('throw new Error("hamburger")', "known")
  .runInNewContext(global, "known")
} catch (e) {
}
known:1
throw new Error("hamburger")

@reggi
Copy link
Contributor Author

reggi commented Aug 14, 2015

The error is being caught properly without global...

try {
  var vm = require('vm')
  vm.createScript('throw new Error("hamburger")', "known")
  .runInNewContext({}, "known")
} catch (e) {
}

@indutny
Copy link
Member

indutny commented Aug 14, 2015

Oh god, again :)

@reggi
Copy link
Contributor Author

reggi commented Aug 14, 2015

I found a way to catch the trace, if you clone the require (specifically with lodash _.clone) it allows you to catch the error and get the full stack trace. Perhaps this issue has to do with sharing global variables.

try {
  var _ = require('lodash')
  var vm = require('vm')
  var sb = {}
  sb.require = _.clone(require)
  vm.createScript("require('missing')")
  .runInNewContext(sb)
} catch (e) {
  throw e
}
evalmachine.<anonymous>:1
require('missing')
^
TypeError: require is not a function
    at evalmachine.<anonymous>:1:1
    at ContextifyScript.Script.runInNewContext (vm.js:18:15)
    at Object.<anonymous> (/Users/thomas/Desktop/node-vm-stderr-bug/bug.js:7:4)
    at Module._compile (module.js:430:26)
    at Object.Module._extensions..js (module.js:448:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:471:10)
    at startup (node.js:117:18)
    at node.js:953:3

@reggi
Copy link
Contributor Author

reggi commented Aug 14, 2015

Ug, didn't realize it said require is not a function, grr this is so frustrating...

@reggi
Copy link
Contributor Author

reggi commented Aug 14, 2015

I dont get why half the thrown error is in the stack trace and half is written to stderr......

stderr

module.js:338
    throw err;
          ^

in stack trace

Error: Cannot find module 'missing'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:286:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at evalmachine.<anonymous>:1:1
    at ContextifyScript.Script.runInNewContext (vm.js:18:15)
    at Object.<anonymous> (/Users/thomas/Desktop/node-vm-stderr-bug/bug.js:7:4)
    at Module._compile (module.js:430:26)
    at Object.Module._extensions..js (module.js:448:10)
    at Module.load (module.js:355:32)

@reggi
Copy link
Contributor Author

reggi commented Aug 14, 2015

There's an option in vm, did the trick!!!!!!!!!!

try {
  var _ = require('lodash')
  var vm = require('vm')
  var sb = {}
  sb.require = require
  var script = new vm.Script("require('missing')")
  script.runInNewContext(sb, {
     displayErrors: false
  })
} catch (e) {
   throw e
}

@reggi reggi closed this as completed Aug 14, 2015
@brendanashworth brendanashworth added the invalid Issues and PRs that are invalid. label Aug 14, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid Issues and PRs that are invalid. question Issues that look for answers. vm Issues and PRs related to the vm subsystem.
Projects
None yet
Development

No branches or pull requests

4 participants