Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

stdout not flushed upon exit with output redirect #1669

Closed
nzakas opened this issue Sep 7, 2011 · 20 comments
Closed

stdout not flushed upon exit with output redirect #1669

nzakas opened this issue Sep 7, 2011 · 20 comments

Comments

@nzakas
Copy link

nzakas commented Sep 7, 2011

Using Node.js v0.4.10 on Ubuntu.

If I write a large amount of data to stdout, either via console.log() or process.stdout.write(), and then try to redirect the output to a file, the file remains empty even though the output never appears in the console. I'm not forcing the process to exit, it's completing its task and then exiting on its own. I expected stdout to flush at this point, but it appears not to.

This behavior can be seen with CSS Lint v0.5.0

npm install [email protected]
csslint some_css_file.css > results.txt

I tried working around this by using:

fs.writeSync(1, message);

However, this is causing other errors.

Is this behavior expected? If so, how can I ensure stdout is flushed so that output can be captured as usual?

@isaacs
Copy link

isaacs commented Sep 8, 2011

You can force it to flush like this:

var flushed = process.stdout.flush()
if (!flushed) process.once("drain", function () {
  process.exit(0)
})

However, pending data on the stdout socket should prevent an exit, so this is a bug.

@nzakas
Copy link
Author

nzakas commented Sep 8, 2011

Thanks, I'll use that as a workaround.

@nzakas
Copy link
Author

nzakas commented Oct 14, 2011

Looks like flush() was removed in the latest Node. Is there another way to force the flush now?

@sh1mmer
Copy link

sh1mmer commented Nov 2, 2011

Flush was unofficial/undocumented, sorry.

Nicholas would you be able to provide an atomic testcase of your large data case to reproduce this bug. Then we can get a patch in.

Thanks

@nzakas
Copy link
Author

nzakas commented Nov 4, 2011

Hey, travelling at the moment so don't have time to whip a standalone demo up. If you try installing [email protected] and run it on the demo file (https://github.com/stubbornella/csslint/blob/master/demos/demo.css), then try to capture the output, you'll end up with an empty file. CSS Lint also has tagged versions in source control, so you could pull the v0.5.0 tag from GitHub and get the source locally as well.

@gcp
Copy link

gcp commented Nov 8, 2011

Flush was unofficial/undocumented, sorry.

That may be, but as nzakas already asked, is there any way to control flushing now? stdout/stderr were made unbuffered for TTY's and files, but not for pipes. I have an application that uses pipes and it's surprising that node 0.6.x is now suddenly a regression for that use case, because there's no way to ensure data is actually flushed as soon as possible.

Using writeSync seems to have the wanted behavior, but there is nothing in the documentation saying this will ensure a stdout flush (IIRC standard Unix behavior is to flush when there's a newline for text mode, or something?), so I'd appreciate a definite word on this.

@ichernev
Copy link

Here is a simple test to see if its (not) working. For me (node v0.4.12) its not working correctly.

https://gist.github.com/1357983

@kripken
Copy link

kripken commented Dec 4, 2011

I hit this issue too, and can reproduce it with ichernev's test case from the last comment on node 0.5.5pre. When output is redirected, nothing is written, but when it is not redirected, things are printed out properly.

@cowboy
Copy link

cowboy commented Jan 24, 2012

FWIW, I've worked around this in my own code with the super-hacky:

process.stdout = process.stderr;

But since I'm developing a CLI tool that might have its output piped, this is less than ideal.

+1 for a fix!

@cowboy
Copy link

cowboy commented Feb 18, 2012

Is this still an issue? I just tried @ichernev's example in v0.6.9 and don't see any issues.

@ichernev
Copy link

Yep, v0.6.8 works for me too.

@mpareja
Copy link

mpareja commented Oct 12, 2012

This is still an issue in v0.8.12 for Windows 7 64-bit.

I'm working around this by exiting on the 'drain' event and writing empty string to the stream. I write the empty string to the stream in case there is nothing more to 'drain' by the time I'm registering for the event:

process.stdout.once('drain', function () { process.exit(0); });
process.stdout.write('');

@bnoordhuis
Copy link
Member

@mpareja Can you open a new issue for that? Please include a test case. Thanks.

@ghost
Copy link

ghost commented Oct 31, 2012

Same error with mocha called from webstorm console. I thought the problem is with webstorm because from git bash I got the whole error stack in the console. Strange behavior... :S
Here is how to reproduce it:
http://yieldthedog.github.com/blog/2012/03/12/configure-webstorm-to-run-and-debug-mocha-tests/
In webstorm console with stdout always the first line appears, with stderr about 65% the first line appears, about 35% the whole err.stack with a custom reporter.

I got this in webstorm console:
"Process finished with exit code 59"
which means
"An unexpected network error occurred."

Hmm it depends only on how long I have to wait for the response. So I think it could be a timeout problem, not necessarily this bug.

@matthiasg
Copy link

this is a real problem. mocha uses console.log to log errors. but error stack traces on windows are reproducible not redirected to a file or other process while the actual errors are.

node.js should make sure console.log output is flushed before the process is exited.

@bnoordhuis
Copy link
Member

Added a 'windows' label. This shouldn't be an issue any longer on Unices.

@matthiasg
Copy link

should the ticket not be reopened then ?

@mpareja
Copy link

mpareja commented Nov 30, 2012

I should have created a new issue. Sorry folks, life's been crazy and I haven't done this. I'll open a ticket with a repro sample at some point soon and reference it from this issue.

@matthiasg
Copy link

is this reason for gruntjs/grunt#504

@matthiasg
Copy link

ah the issue is #3584

marijnh pushed a commit to acornjs/acorn that referenced this issue Jul 29, 2014
juj added a commit to emscripten-core/emscripten that referenced this issue Jul 31, 2014
… fix other.test_warn_undefined on Windows. The end of the output buffer would not get printed during the test, which would cause the test to fail. See also #2582
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants