joyent/node — Issue #2675: allow console.log to completely drain before node exits normally
Author: mikedeboer
Created: 2012-02-02 09:40:59+00:00
State: open

this issue has the same name as the pull req that was closed half a year ago: https://github.com/joyent/node/pull/1312.

I ran into this problem when spawning NodeJS programs as child processes and to behave like CGI scripts/ processes. Usually these processes output to stdout, which we do in NodeJS with `console.log`. Therefore, using the unbuffered writing to stderr is not a real option, as it would be a hack around the real problem: when NodeJS exits, the write buffer to the stdout stream may not be drained yet.

Setting a listener to the 'drain' event on `process.stdout` is also not a valid workaround, because it does not emit when the stream buffer is in fact empty already. On top of that, it would be a workaround/ hack which would need to be applied to each and every NodeJS CGI script, which is unmaintainable.

In the case of running NodeJS programs as CGI, this behavior results in incomplete output/ HTML pages. In other words, it is impossible to use NodeJS for this purpose at this point.