[weird] console.log is not sync anymore when I use a stream, output is truncated when it s followed by process.exit

35 views
Skip to first unread message

mh-cbon

unread,
May 23, 2016, 10:57:29 AM5/23/16
to nodejs
Hi,

I m facing a badly weird issue which i can t tell if it s a bug, or an error from my side.

Basically, i m doing

console.log('some long data...');
process
.exit(0)

The problem is that the console.log won t display the whole data.
It will stop somewhere in the middle and exit.

I noticed this happens only when i involve a stream parser made on top of `mississippi`

A simple and regular code like this works fine,

var data = require('fs').readFileSync('fixtures/user')

console
.log(JSON.stringify({d:data}, null, 2));
process
.exit(0)

this enhanced version which parse the file content via a stream will not work correctly

var spDs = require('./sp-dscacheutil.js');

var data;
var stream = spDs()
.on('error', function () {
  console
.log('error')
})
.on('end', function () {
  console
.error('end');
  console
.log("%j", data);
  process
.exit(0);
}).on('data', function (d) {
  data
= d;
});

require('fs').createReadStream('fixtures/user').pipe(stream)


The stream parser is available here

https://github.com/mh-cbon/dscacheutil/blob/master/sp-dscacheutil.js

I reproduced the problem on
node 6.1/6.2
systems fedora23/yosemite

Looking for help !

Thanks !

Zlatko

unread,
May 25, 2016, 12:14:13 AM5/25/16
to nodejs
On Monday, May 23, 2016 at 4:57:29 PM UTC+2, mh-cbon wrote:
Hi,

I m facing a badly weird issue which i can t tell if it s a bug, or an error from my side.

 
It's not a bug, it's a feature :)

Seriously, console.log is asynchronous, because it can involve disk I/O (e.g you run a program but redirect the log to a file). In order not to block with such processes, console.log is made async.

IIRC console.error is sync - so in this particular case you can use that. But you can also write to process.stdout and then make sure your stuff is written before you end the program.

Ryan Graham

unread,
May 25, 2016, 10:52:38 AM5/25/16
to nodejs
I wish it were that simple! Both log and error were supposed to be sync, unless they were redirected to files.

The weirdness the original post was about is quite possibly related to the the "fun" described in https://github.com/nodejs/node/issues/6456

~Ryan
--

~Ryan

mh-cbon

unread,
May 25, 2016, 6:41:01 PM5/25/16
to nodejs
Really useful comment, thanks !

To summarize,
i m wrong to call process.exit(),
When i need to set the process exit code,
then i should set `process.exitCode` property (asat).

see https://nodejs.org/api/process.html#process_process_exitcode
Reply all
Reply to author
Forward
0 new messages