Skip to content

Synchronous exec stalls permenantly when there is an error/w the shell #7

@celer

Description

@celer

This ShellJS script will stall indefinitely, if 'nosuchfile.txt' doesn't exist.

require('shelljs/global')

exec("cat < nosuchfile.txt")

Will cause shelljs to stall

This fixes it:

  cmd += ' > '+stdoutFile+' 2>&1'; // works on both win/unix

  var script =
   "var child = require('child_process'), path=require('path'), \
        fs = require('fs'); \
    child.exec('"+escape(cmd)+"', {env: process.env}, function(err,stdout,stderr) { \
      fs.writeFileSync('"+escape(codeFile)+"', err ? err.code.toString() : '0'); \
      if(!path.existsSync('"+escape(stdoutFile)+"')){ fs.writeFileSync('"+escape(stdoutFile)+"',stdout.toString()+stderr.toString()); }; \
    }); \
    process.on('uncaughtException',function(err){ \
      if(!path.existsSync('"+escape(stdoutFile)+"')){ fs.writeFileSync('"+escape(stdoutFile)+"',err.toString()); } \
      if(!path.existsSync('"+escape(codeFile)+"')){ fs.writeFileSync('"+escape(codeFile)+"','-1'); } \
    }); \
    process.on('exit',function(){ \
      if(!path.existsSync('"+escape(stdoutFile)+"')){ fs.writeFileSync('"+escape(stdoutFile)+"','Uncaught ShellJS Error'); } \
      if(!path.existsSync('"+escape(codeFile)+"')){ fs.writeFileSync('"+escape(codeFile)+"','-1'); } \
    });"


  if (fs.existsSync(scriptFile)) _unlinkSync(scriptFile);
  if (fs.existsSync(stdoutFile)) _unlinkSync(stdoutFile);
  if (fs.existsSync(codeFile)) _unlinkSync(codeFile);

We have forked your repo, here: https://github.com/croteb/shelljs and will be keeping it current with our fixes.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    execIssues specific to the shell.exec() APIfixBug/defect, or a fix for such a problem

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions