Skip to content

child_process instance set encoding will trigger exception #18969

Description

@hstarorg
  • Version: v9.3.0
  • Platform: Darwin 16.7.0 Darwin Kernel Version 16.7.0: Thu Jan 11 22:59:40 PST 2018; root:xnu-3789.73.8~1/RELEASE_X86_64 x86_64
  • Subsystem:

Execute code at node command line.

const cp = require('child_process');

const child = cp.exec('pwd', {encoding: 'invalid'}, (err, stdout, stderr) => {
  console.log('callback', err)
  console.log('callback', typeof stdout, stdout)
})

child.stdout.setEncoding('utf8')

child.stdout.on('data', function (v) {
  console.log(typeof v, v)
})

The output is :

buffer.js:475
      throw new errors.TypeError(
      ^

TypeError [ERR_INVALID_ARG_TYPE]: The "list" argument must be one of type Array, Buffer, or Uint8Array
    at Function.concat (buffer.js:475:13)
    at ChildProcess.exithandler (child_process.js:259:23)
    at ChildProcess.emit (events.js:159:13)
    at maybeClose (internal/child_process.js:943:16)
    at Socket.stream.socket.on (internal/child_process.js:363:11)
    at Socket.emit (events.js:159:13)
    at Pipe._handle.close [as _onclose] (net.js:568:12)

Is this a bug in nodejs?

BTW: The issue finder is @xqin

There didn't trigger the exception if use cp.exec('pwd', {encoding: 'invalid'}) without the callback.

@xqin has a new idea that whether need bind listener on stdout and stderr when user not pass the callback param. The source code at: https://github.com/nodejs/node/blob/master/lib/child_process.js#L322-L358

Activity

  1. xqin commented on Feb 24, 2018

    @xqin
    $ node -v
    v9.3.0
    $ uname -a
    Darwin 16.7.0 Darwin Kernel Version 16.7.0: Thu Jan 11 22:59:40 PST 2018; root:xnu-3789.73.8~1/RELEASE_X86_64 x86_64
    

    code

    $ cat test.js
    const cp = require('child_process');
    
    const child = cp.exec('pwd', {encoding: 'invalid'}, (err, stdout, stderr) => {
      console.log('callback', err)
      console.log('callback', typeof stdout, stdout)
    })
    
    child.stdout.setEncoding('utf8')
    
    child.stdout.on('data', function (v) {
      console.log(typeof v, v)
    })
    
    $ node test.js
    string /Users/xqin
    
    buffer.js:475
          throw new errors.TypeError(
          ^
    
    TypeError [ERR_INVALID_ARG_TYPE]: The "list" argument must be one of type Array, Buffer, or Uint8Array
        at Function.concat (buffer.js:475:13)
        at ChildProcess.exithandler (child_process.js:259:23)
        at ChildProcess.emit (events.js:159:13)
        at maybeClose (internal/child_process.js:943:16)
        at Socket.stream.socket.on (internal/child_process.js:363:11)
        at Socket.emit (events.js:159:13)
        at Pipe._handle.close [as _onclose] (net.js:568:12)
    
  2. added
    child_processIssues and PRs related to the child_process subsystem.
    on Feb 24, 2018
  3. killagu commented on Feb 24, 2018

    @killagu
    Contributor

    It's cp bug.
    cp have not checked the stdout or stderr encoding changed, cp decide by options.encoding to use stdout or Buffer.concat it.when encoding change to utf-8, the Buffer.concat will failed.

    I will raise a PR to fix it.

  4. hstarorg commented on Feb 27, 2018

    @hstarorg
    Author

    @killagu Please see this, whether need bind listener on stdout and stderr when user not pass the callback param. The source code at: https://github.com/nodejs/node/blob/master/lib/child_process.js#L322-L358

  5. killagu commented on Feb 27, 2018

    @killagu
    Contributor

    @hstarorg It should in another issue. One pr resolve one problem.

  6. hstarorg commented on Feb 27, 2018

    @hstarorg
    Author

    @killagu OK, Thanks.

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.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions