Is there a race consuming stdout from child_process.spawn in node.js?

I'm using node.js to spawn a child process and consume its output, using the child.stdout.on('data') hook.

child.stdout is itself a stream, and over on the streams page I notice a fat warning informing me that if no handler is registered when a data event arrives, then the data is dropped on the floor.

There is a brief moment between the spawn of my child process, and the registration of my stdout handler. Is there a race condition here? I don't want my first read to be lost.

It is reasonable to guess that the pipe between parent and child would buffer, but does node guarantee it?

You have to call child.stdout.on('data', [...]) latter in the same turn (a series of serial statement) as the child_process.spawn([...]) call to ensure that the listener is there when the IO operations are next handled. Or you could pause the stream child.stdout.pause() (in the same turn) and resume it in a latter turn after adding the listener if it is necessary to install the listener in a latter turn.