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 output truncated #2360

Closed
ben-ng opened this issue Aug 12, 2015 · 6 comments
Closed

child process output truncated #2360

ben-ng opened this issue Aug 12, 2015 · 6 comments
Labels
child_process Issues and PRs related to the child_process subsystem. process Issues and PRs related to the process subsystem.

Comments

@ben-ng
Copy link

ben-ng commented Aug 12, 2015

Create these two files:

// reproduce.js
var cp = require('child_process').spawn('node', ['output.js'])
  , buff = []

cp.stdout.on('data', function (d) {
  buff.push(d)
})

cp.stdout.on('end', function () {
  console.log(buff.join('').split('\n').length)
})
// output.js
for (var i=0, ii=20000; i<ii; ++i) {
  process.stdout.write(i + ' lorem ipsum dolor sit amet\n')
}

process.exit(0)

Run

node output.js

Note that you see 19999 as the last row in your console.

Run

node reproduce.js

Note that this script is supposed to output 20001. It doesn't, because the stdout from child_process ends before all the output is actually returned. It sends a nondeterministic amount of data before end is fired.

Removing the process.exit(0) from output.js resolves the problem. This is the workaround I'm using in my app. This is a regression; the code worked just fine in node 0.12

Also interesting: the bug reproduces on OSX, but it doesn't seem to happen on Heroku, which I think uses a 64 bit linux.

@mscdex mscdex added the child_process Issues and PRs related to the child_process subsystem. label Aug 12, 2015
@mscdex
Copy link
Contributor

mscdex commented Aug 12, 2015

This is a duplicate of #2049 although there is no public fix for this yet...

@mscdex mscdex added the process Issues and PRs related to the process subsystem. label Aug 12, 2015
@ben-ng
Copy link
Author

ben-ng commented Aug 12, 2015

Hm, are you sure its precisely the same bug? Whats peculiar to me is that when output.js is run directly, i always get all the output back, even when I make the loop run to 100000. Its only when I run it as a child process does the output get truncated.

@cjihrig
Copy link
Contributor

cjihrig commented Aug 12, 2015

It does seem to be the same issue, and adding process.stdout._handle.setBlocking(true) to the child process seems to fix it, as recommended in #2049.

@ben-ng
Copy link
Author

ben-ng commented Aug 12, 2015

@cjihrig great! should this issue be closed then? i worded the issue differently from the duplicates, so i wonder if leaving it open will help others find this instead of spending time debugging themselves.

@cjihrig
Copy link
Contributor

cjihrig commented Aug 12, 2015

Yea, let's close the duplicate. People will be able to find this now.

@mohitsud
Copy link

Adding this option fixed it:

stdio: 'inherit'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
child_process Issues and PRs related to the child_process subsystem. process Issues and PRs related to the process subsystem.
Projects
None yet
Development

No branches or pull requests

4 participants