Skip to content

'process.stdout cannot be closed': process.stdout should behave like a standard stream #7606

Description

@yinrong
  • Version: latest
  • Platform: all
  • Subsystem: node/lib/internal/process/stdio.js

sample code:

// hang process
setTimeout(function(){}, 9999999)
// in heavy development, there usually exists something that hangs the process.
// we use 'process.exit' to solve this quickly.

var stream = require('stream')
process.stdin
  .pipe(new stream.PassThrough()) // there can be a list of Transform
  .pipe(new stream.PassThrough()) // there can be a list of Transform
  .pipe(new stream.PassThrough()) // there can be a list of Transform
  .on('end', function() {
    // process.exit()
    // exit here will stop stdout from flushing
  })
  .pipe(process.stdout)
  .on('finish', function() {  // this event will not trigger
    process.exit()
  })

execution result: hangs forever

expect: process.stdout should behaves like a standard stream.

Activity

  1. added
    streamIssues and PRs related to Node.js streams.
    processIssues and PRs related to the process subsystem.
    on Jul 8, 2016
  2. bnoordhuis commented on Jul 8, 2016

    @bnoordhuis
    Member

    That behavior is intentional and fixes more issues than it introduces. Most programs fail very badly if stdio could be closed and in fact they did. That's why we made stdio permanent in v0.6.0.

    I'll go ahead and close the issue.

  3. Judimax commented on Jan 28, 2019

    @Judimax

    @bnoordhuis, is this functionality the same for stdin and stderr, how can I properly attach and deattach a Writable Stream to process.stdout out so I can get all my console output to a file

  4. sam-github commented on Jan 31, 2019

    @sam-github
    Contributor

    In my opionion, node lacks process.stdio.reopen(), or something similar -- an equivalent to freopen(3).

  5. Judimax commented on Jan 31, 2019

    @Judimax

    solution I implemented check if this works or its a safe solution
    https://stackoverflow.com/questions/32719923/redirecting-stdout-to-file-nodejs/54407338#54407338

  6. added a commit that references this issue on Mar 19, 2020
  7. added a commit that references this issue on Apr 6, 2020
  8. added a commit that references this issue on Apr 7, 2020
  9. added a commit that references this issue on Apr 12, 2020
  10. 0xkarambit commented on Jul 1, 2020

    @0xkarambit

    I think that the fact that stdout doesn't emit the "finish" event should be mentioned in the nodejs
    docs under the "a note of process io" section on the page about process
    https://nodejs.org/api/process.html#process_a_note_on_process_i_o

  11. ronag commented on Jul 1, 2020

    @ronag
    Member

    It does emit the finish event, try:

    const { stdout } = require('process');
    
    process.stdout
      .on('finish', () => console.error('finish'))
      .end()
  12. 0xkarambit commented on Jul 2, 2020

    @0xkarambit

    Oh yes, it does sorry for that, I am just a beginner.

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

    processIssues and PRs related to the process subsystem.streamIssues and PRs related to Node.js streams.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions