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

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

Closed
yinrong opened this issue Jul 8, 2016 · 7 comments
Labels
process Issues and PRs related to the process subsystem. stream Issues and PRs related to the stream subsystem.

Comments

@yinrong
Copy link

yinrong commented Jul 8, 2016

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

@mscdex mscdex added stream Issues and PRs related to the stream subsystem. process Issues and PRs related to the process subsystem. labels Jul 8, 2016
@bnoordhuis
Copy link
Member

bnoordhuis commented Jul 8, 2016

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.

@Judimax
Copy link

Judimax commented Jan 28, 2019

@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

@sam-github
Copy link
Contributor

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

@Judimax
Copy link

Judimax commented Jan 31, 2019

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

ronag added a commit to nxtedition/node that referenced this issue Mar 19, 2020
stdio (stderr & stdout) should for compatibility
reasons not be closed/end():ed. However, this
causes pipeline with a stdio destination to
never finish. This commit fixes this issue at
a performance cost.

Refs: nodejs#7606

Fixes: nodejs#32363
ronag added a commit that referenced this issue Apr 6, 2020
stdio (stderr & stdout) should for compatibility
reasons not be closed/end():ed. However, this
causes pipeline with a stdio destination to
never finish. This commit fixes this issue at
a performance cost.

Refs: #7606

Fixes: #32363

PR-URL: #32373
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
BethGriggs pushed a commit that referenced this issue Apr 7, 2020
stdio (stderr & stdout) should for compatibility
reasons not be closed/end():ed. However, this
causes pipeline with a stdio destination to
never finish. This commit fixes this issue at
a performance cost.

Refs: #7606

Fixes: #32363

PR-URL: #32373
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
targos pushed a commit that referenced this issue Apr 12, 2020
stdio (stderr & stdout) should for compatibility
reasons not be closed/end():ed. However, this
causes pipeline with a stdio destination to
never finish. This commit fixes this issue at
a performance cost.

Refs: #7606

Fixes: #32363

PR-URL: #32373
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
@0xkarambit
Copy link

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

@ronag
Copy link
Member

ronag commented Jul 1, 2020

It does emit the finish event, try:

const { stdout } = require('process');

process.stdout
  .on('finish', () => console.error('finish'))
  .end()

@0xkarambit
Copy link

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
Labels
process Issues and PRs related to the process subsystem. stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

No branches or pull requests

7 participants