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

exec returning null from command #724

Closed
webOS101 opened this issue May 22, 2017 · 25 comments
Closed

exec returning null from command #724

webOS101 opened this issue May 22, 2017 · 25 comments
Labels
exec Issues specific to the shell.exec() API

Comments

@webOS101
Copy link

Node version (or tell us if you're using electron or some other framework):

v6.2.1

ShellJS version (the most recent version/Github branch you see the bug on):

0.7.7

Operating system:

MacOS X 10.10

Description of the bug:

Occasionally, the exec command returns null instead of something valid. I have an automated build script that executes. Some time during the execution of the script, it fails with the error:

.../scripts/DocParser.js:51
		if (output.code === 0) {
		          ^

TypeError: Cannot read property 'code' of null

Example ShellJS command to reproduce the error:

const output = shelljs.exec(cmd, {silent: true});

These execs are called in a loop and it's not the first one that fails. If I call the script outside of the automation procedure, it runs to completion without issue. I'm super stumped on this one and could use any help debugging the problem.

@nfischer
Copy link
Member

@webOS101 how often can you reproduce this?

@webOS101
Copy link
Author

Fairly consistently. I can reproduce it at will.

nfischer added a commit that referenced this issue May 23, 2017
This branch is only for debugging issue #724
@nfischer
Copy link
Member

@webOS101 would you be willing to pull down a copy of shelljs and try it out locally?

$ shelljsPath=path/to/your/project/node_modules/shelljs/
$ rm -rf "$shelljsPath"
$ git clone https://github.com/shelljs/shelljs.git "$shelljsPath"
$ cd "$shelljsPath"
$ git checkout debug-724-null-exec
$ cd -
$ # try to reproduce the bug again and paste the console output on this bug

@webOS101
Copy link
Author

I saw your commit and added it to my repo. I went with:

    "shelljs": "github:shelljs/shelljs#debug-724-null-exec",

in my package.json since this is run on a separate box through automation.

@webOS101
Copy link
Author

@nfischer
Copy link
Member

@webOS101 Still can't quite identify the error, but the exec: internal error indicates a bug in our code. Could you try pulling in the latest version of that branch and reproducing again? Thanks

@webOS101
Copy link
Author

Perhaps unsurprisingly it clocks in at about 64K. I don't know why this passes for some builds and fails for others, though. Our largest output file is 262K and it can complete without issue.

@nfischer
Copy link
Member

Yeah, it seems to be pretty sizable. You can try increasing exec's buffer size:

shell.exec('your-command-here', { maxBuffer: 1024*1024 });
// default is 200 * 1024

If this does fix the issue, this is probably something we should look out for specifically, so that we can give a better error message.

@nfischer
Copy link
Member

Whoops, made a mistake. ShellJS has a higher default size (20 * 1024 * 1024). So maybe try 1024 * 1024 * 1024

@webOS101
Copy link
Author

{ Error: ENOENT: no such file or directory, open '/var/folders/bn/4f4fcly97sx1g8_3nryzfqxh0000gp/T/shelljs_2bf8e16a4a7cabfe73b1'
    at Error (native)
    at Object.fs.openSync (fs.js:634:18)
    at Object.fs.readFileSync (fs.js:502:33)
    at execSync (/Users/xxx/workspace/xxx-docs/xxx-docs/node_modules/shelljs/src/exec.js:165:24)
    at Object._exec (/Users/xxx/workspace/xxx-docs/xxx-docs/node_modules/shelljs/src/exec.js:289:14)
    at Object.exec (/Users/xxx/workspace/xxx-docs/xxx-docs/node_modules/shelljs/src/common.js:353:23)
    at /Users/xxx/workspace/xxx-docs/xxx-docs/scripts/DocParser.js:42:26
    at Set.forEach (native)
    at getDocumentation (/Users/xxx/workspace/xxx-docs/xxx-docs/scripts/DocParser.js:36:13)
    at init (/Users/xxx/workspace/xxx-docs/xxx-docs/scripts/DocParser.js:151:4)
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/var/folders/bn/4f4fcly97sx1g8_3nryzfqxh0000gp/T/shelljs_2bf8e16a4a7cabfe73b1' }
exec: internal error

@webOS101
Copy link
Author

I tried using the larger buffer, as suggested, but it didn't make a difference. The drive is not full, which was my first thought. Other ideas?

@freitagbr
Copy link
Contributor

It looks like one of the temp file storing the process ext code created by exec is being removed before the process finishes.

@nfischer Remind me why execSync needs to create the temporary files and execute them in node. Is it because we want the stdout/stderr of the spawned process to update in real time?

@nfischer
Copy link
Member

It looks like one of the temp file storing the process ext code created by exec is being removed before the process finishes.

If child_process.exec() in the temp script invokes its callback, then we know the codeFile is actually created on disk, before the child process terminates. If that file is created on disk, then we'll definitely be able to read it by the time we get to that line of code. So that must mean we aren't calling the callback (due to an exception in child_process.exec), or we get some exception early on in the callback.

This sort of thing is actually plausible. Here's a minimal example:

// This example requires unix (since it uses the `yes` command
require('child_process').exec('yes', function (code) { // overflow the maxBuffer with lots of stdio
  console.log('I will never get called');
});

It sounds like our external command has tons of output, so this makes some sense. If this really is the case, then I think we can make some progress by wrapping child_process.exec() in a try-catch.

Unfortunately, I can't find a command that will consistently break shell.exec(). For some reason yes does not repro the bug in shelljs. I need help getting a consistent repro before I can fix this.

@nfischer Remind me why execSync needs to create the temporary files and execute them in node. Is it because we want the stdout/stderr of the spawned process to update in real time?

Yes, that's exactly the reason

@nfischer
Copy link
Member

@webOS101 Would you be able to try the latest version of the branch again? I added a couple more debug logs that should help confirm my suspicions. Thanks!

@nfischer nfischer added the exec Issues specific to the shell.exec() API label Jun 13, 2017
@webOS101
Copy link
Author

I will try to do this. I had to replace that bit in my code so that I could get my build working again. I can probably check out an older version of my scripts to see what happens.

@nfischer
Copy link
Member

Thanks, I appreciate the help debugging. This is a long-time bug we've never been able to repro, and I'd like to get a fix out.

I'm convinced that we need to follow my try-catch suggestion regardless, but I'd like to dig deeper and see if that's really causing this issue.

@mikeweaver
Copy link

FYI: I believe I have a reproducing case of this bug as well.

@nfischer
Copy link
Member

@mikeweaver what happens if you try out that branch? What do the logs show?

$ rm -rf node_modules/shelljs
$ npm install 'github:shelljs/shelljs#debug-724-null-exec'

@nfischer
Copy link
Member

nfischer commented Feb 7, 2018

We did a big refactor of exec() (0.8.1) I think this is resolved. Let me know if that's not the case.

@nfischer nfischer closed this as completed Feb 7, 2018
@Morphexe
Copy link

Morphexe commented Sep 8, 2018

Sadly no, I just hit this when running a script.

const mkdir = shell.mkdir(....)

if (mkdir.code == 0)
This throws a error saying mkdir is undefined.

shell.ls('-Rl', '.').forEach -> same drill " Cannot read property 'forEach' of null"

@nfischer
Copy link
Member

@Morphexe please file a new issue, Your problem appears to not be exec-related, but I need the info from the bug template to diagnose.

@yuanhaisu
Copy link

yuanhaisu commented Sep 17, 2018

ShellJSInternalError: ENOENT: no such file or directory, open '/tmp/shelljs_22c2372f7902a33382c6'
    at Object.openSync (fs.js:434:3)
    at Object.readFileSync (fs.js:339:35)
    at execSync (/home/reporter-skynet/node_modules/shelljs/src/exec.js:89:17)
    at Object._exec (/home/reporter-skynet/node_modules/shelljs/src/exec.js:202:12)
    at Object.exec (/home/reporter-skynet/node_modules/shelljs/src/common.js:335:23)

I met the same promble in shelljs v0.8.2, and my node version is v10.8.0, How can I debug this error , please help me!

@nfischer
Copy link
Member

@yuanhaisu please file a new Github issue. We need all info from the bug template.

@jbcpollak
Copy link

jbcpollak commented Oct 1, 2018

I'm having the same issues as @webOS101 and @yuanhaisu - node 10.9.0, shelljs 0.8.1 and 0.8.2, and macOS 10.14.

Interestingly this was working two days ago, I came back to the computer and now it doesn't work:


$ ./scripts/monitor start test/appA
{ ShellJSInternalError: ENOENT: no such file or directory, open '/var/folders/_4/7rhjy87148140x058565h26m0000gn/T/shelljs_99c7fdf8414fcb4ff6c1'
    at Object.openSync (fs.js:434:3)
    at Object.readFileSync (fs.js:339:35)
    at execSync (/Users/jpollak/src/infrastructure/node_modules/shelljs/src/exec.js:89:17)
    at Object._exec (/Users/jpollak/src/infrastructure/node_modules/shelljs/src/exec.js:202:12)
    at Object.exec (/Users/jpollak/src/infrastructure/node_modules/shelljs/src/common.js:335:23)
    at getNodeVersion (/Users/jpollak/src/infrastructure/src/ProcessRunner.ts:40:9)
    at Object.startProc (/Users/jpollak/src/infrastructure/src/ProcessRunner.ts:52:25)
    at projects.map (/Users/jpollak/src/infrastructure/src/monitor.ts:16:10)
    at Array.map (<anonymous>)
    at start (/Users/jpollak/src/infrastructure/src/monitor.ts:12:33)
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path:
   '/var/folders/_4/7rhjy87148140x058565h26m0000gn/T/shelljs_99c7fdf8414fcb4ff6c1',
  name: 'ShellJSInternalError' }

@nfischer
Copy link
Member

nfischer commented Oct 2, 2018

@jbcpollak I'm sorry, but this should be on a separate Github issue. For example, it's not clear if you're using electron or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
exec Issues specific to the shell.exec() API
Projects
None yet
Development

No branches or pull requests

7 participants