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: empty stdout on parallel exec #530

Open
legendecas opened this issue Jun 3, 2019 · 2 comments
Open

child_process: empty stdout on parallel exec #530

legendecas opened this issue Jun 3, 2019 · 2 comments

Comments

@legendecas
Copy link
Contributor

legendecas commented Jun 3, 2019

  • Version: 0.11.5
  • Platform: linux
  • Subsystem: child_process
var childProcess = require('child_process')
var cmd = `cat /proc/meminfo | grep MemAvailable | awk '{ print $2 }'`

childProcess.exec(cmd, (err, stdout) => {
  console.log(err, stdout) // stdout never empty
})
childProcess.exec(cmd, (err, stdout) => {
  console.log(err, stdout) // stdout *possibly* empty
})
@yorkie
Copy link
Member

yorkie commented Sep 21, 2019

It seems that exit event is in front of data, I have patched the following on my linux device:

diff --git a/src/js/child_process.js b/src/js/child_process.js
index a7cbf10f..9a40631a 100644
--- a/src/js/child_process.js
+++ b/src/js/child_process.js
@@ -917,6 +917,7 @@ exports.execFile = function(file /* , args, options, callback */) {
       child.stdout.setEncoding(encoding);

     child.stdout.on('data', function onChildStdout(chunk) {
+      console.log('>>>>', chunk);
       stdoutLen += chunk.length;

       if (stdoutLen > options.maxBuffer) {

And got the reason why it's null:

/ # node test.js
>>>> <Buffer 35 33 30 30 30 0a>
null string 53000

>>>> <Buffer 35 32 39 39 32 0a>
null string 52992

/ # node test.js
>>>> <Buffer 35 32 38 36 30 0a>
null string 52860

null string   # the _stdout is null because the data event has not fired yet :(
>>>> <Buffer 35 32 39 33 32 0a>

@yorkie
Copy link
Member

yorkie commented Sep 21, 2019

I found that pipe can still read the data by uv_stream_t after the onexit callback gets fired, namely the process exits. @lolBig May I ask you chime this in a bit?

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

2 participants