Search code examples
node.jsstreamstdoutwebstormwinston

stdout collisions on node


If multiple processes are simultaneously writing to stdout then there is nothing to stop the streams from interleaving. This is what I mean by a collision. enter image description here According to the comments in the node source, it should be possible to avoid collisions in process.stdout. I tried this and it helps but I still get collisions. Without the writing flag, I get collisions every time, with the flag it drops to about 40%. Still very significant.

page.on('onConsoleMessage', function log(message) {
  var writing = process.stdout._writableState.writing
    || process.stdout._writableState.bufferProcessing
    || process.stdout.bufferSize ;
  if(writing)
    process.nextTick(message => log(message));
  else
    process.stdout.write('-> ' + message + '\n')
});

What is the best way to avoid collisions on process.stdout?

The above routine is competing with Winston for stdout.

node v5.12.0 Windows 10

This problem only happens when using the Run console in webstorm, the output is not mixed up when running node in powershell or from cmd. I raised a ticket for this at jetbrains.


Solution

  • If you have multiple writers to the same stream, I don't think that you'll get interleaving. Even if a single writer is logging multiple lines in succession, those lines will be buffered (if there is buffering going on) in the correct order, and when another writer is logging its lines, they will be appended to the buffer, behind the previously logged lines.

    Interleaving can occur when you have writers writing to a different stream, like one writing to stdout and the other to stderr. At some point, when the output buffer of one stream fills up, it gets flushed to the console, regardless of any other streams that may also be writing to console.