Skip to content

child process output truncated #2360

Closed
Closed

Description

@ben-ng

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.

Activity

  1. added
    child_processIssues and PRs related to the child_process subsystem.
    on Aug 12, 2015
  2. mscdex commented on Aug 12, 2015

    @mscdex
    Contributor

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

  3. added
    processIssues and PRs related to the process subsystem.
    on Aug 12, 2015
  4. ben-ng commented on Aug 12, 2015

    @ben-ng
    Author

    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.

  5. cjihrig commented on Aug 12, 2015

    @cjihrig
    Contributor

    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.

  6. ben-ng commented on Aug 12, 2015

    @ben-ng
    Author

    @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.

  7. cjihrig commented on Aug 12, 2015

    @cjihrig
    Contributor

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

  8. mohitsud commented on May 20, 2020

    @mohitsud

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    child_processIssues and PRs related to the child_process subsystem.processIssues and PRs related to the process subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions