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

child_process.execSync causes error: write EBADF #3482

Closed
davej opened this issue Nov 17, 2015 · 12 comments
Closed

child_process.execSync causes error: write EBADF #3482

davej opened this issue Nov 17, 2015 · 12 comments

Comments

@davej
Copy link
Contributor

davej commented Nov 17, 2015

Seeing this issue in the renderer process (works fine in main process) of 0.34.3 on Yosemite. child_process.exec is fine, only execSync fails.

To recreate in the renderer, just do:

require('child_process').execSync('echo $PATH');

The error backtrace:

events.js:141 Uncaught Error: write EBADF
exports._errnoException @ util.js:837
exports._exceptionWithHostPort @ util.js:860
afterWrite @ net.js:760

screen shot 2015-11-17 at 22 54 28

Possibly related to: #1471

@zcbenz
Copy link
Member

zcbenz commented Nov 18, 2015

I'm unable to reproduce it, but I think I can guess out the cause. Can you run this code in renderer and then test whether execSync works?

process.stdout._write = function(_, _, callback) {
  process.nextTick(function() {
    callback(0);
  });
}

@zcbenz zcbenz added the blocked/need-info ❌ Cannot proceed without more information label Nov 18, 2015
@davej
Copy link
Contributor Author

davej commented Nov 18, 2015

It is still causing the same error.

> process.stdout._write = function(_, _, callback) {
    process.nextTick(function() {
      callback(0);
    });
  }
> require('child_process').execSync('echo $PATH');
events.js:141 Uncaught Error: write EBADF

@zcbenz
Copy link
Member

zcbenz commented Nov 18, 2015

Does the error happen if you call require('child_process').execSync('echo $PATH', {stdio: 'ignore'});?

@davej
Copy link
Contributor Author

davej commented Nov 18, 2015

No.

@zcbenz zcbenz added bug platform/macOS machine-dependent and removed blocked/need-info ❌ Cannot proceed without more information labels Nov 18, 2015
@davej
Copy link
Contributor Author

davej commented Nov 18, 2015

@zcbenz After a bit of digging, I realised that it is specific to my app. I've managed to track down a reproducible error scenario, it's very very strange!

In main/browser:

var foo = console;

In renderer:

require('child_process').execSync('echo $PATH');

Here's a repo that you can clone to reproduce it: https://github.com/davej/electron-execSync-test

@davej
Copy link
Contributor Author

davej commented Nov 18, 2015

Tested the repo in my last comment on a different machine machine running OS X 10.11 and also tested on Windows 7.

Results are:

  • Windows 7: OK
  • OS X 10.10 (Yosemite): Error
  • OS X 10.11 (El Capitan): Error

This seems to suggest that the bug is OS X specific but not machine dependent.

@lgodard
Copy link

lgodard commented Jan 22, 2016

Hi
i experience same behaviour on Win7

in a js
const console = require('console');
...
console.log('my dummy message');

this line issues an error
Uncaught Error: EBADF: bad file descriptor, write

if i remove the const console, everything is fine

feel free to ask if precision needed

Laurent

@etiktin
Copy link
Contributor

etiktin commented Jan 22, 2016

@lgodard, the console object is a part of the global object so it's always available and you don't need to require it. The require will always fail, unless you npm install some module called console, so just remove that line.

@davej
Copy link
Contributor Author

davej commented Jan 22, 2016

It may fail but it shouldn't fail with a bad file descriptor message.

@etiktin
Copy link
Contributor

etiktin commented Jan 22, 2016

I agree. It should be Error: Cannot find module 'console' or in this case TypeError: Identifier 'console' has already been declared. The point of my comment was that you shouldn't require console, not the error thrown.

@davej
Copy link
Contributor Author

davej commented Jan 22, 2016

You're probably correct, it's not impossible that console is a module though. It may shim console features or dump the logs to file/db. Anyway, I'm gone horribly off-topic! :-)

@lgodard
Copy link

lgodard commented Jan 23, 2016

Hi,
Thanks a lot for your responses

I agree that console.log() works and displays outputs in DevTools window

But, at least on linux, using require('console') redirects the output in the console where npm start has been launched. It allows to track all the output messages at once (no external module installed)

My point is, it is working for linux but fails on win7 with the "Uncaught Error: EBADF: bad file descriptor, write" message so i can't rely on developping multiplateform

Thanks again

Laurent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants