1
votes

I'm trying to make a mp3 webradio in node.js using the child_process module and the mpg123 player on a raspberry pi.

Here's the problem: When I try to write to a stream, I always get an Error:

Error: write EPIPE
at exports._errnoException (util.js:1026:11)
at WriteWrap.afterWrite (net.js:795:14)

Here's what I tried to test it:

//create a subprocess
var test = childProcess.exec('ls /home/pi/music');

//pipe all the output to the process output
test.stdout.pipe(process.stdout);
test.stderr.pipe(process.stderr);

test.stdin.setEncoding('utf-8');

//here I just want to write a key to the subprocess
test.stdin.write('q');
test.stdin.end()

Anyone know, what to do, that the created write stream will not be closed until everything is done?

And of course I still searched on google for this:

Child_process throw an Error: write EPIPE

https://github.com/nodejs/node/issues/2985

https://nodejs.org/api/child_process.html#child_process_subprocess_stdin

https://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback

Please help!

1
the child process for ''ls /home/pi/music' will not exit before you try to write something else on its output ? - dpetrini
I think the process will exit and this is the reason for the error. But I don't know how to stop this :c. If it wouldn't stop, I think it wouldn't produce an error ... - J.Doe
Isn't it because you .end() the stind stream while it has already been ended on the other side ? - n00dl3
What is your intention, If you want to test you could generate a bigger output to test the behavior (let's say 'dmesg'). You want to play mp3 files, why don“t configure the player to output to stdout? - dpetrini

1 Answers

0
votes

The EPIPE write error means someone is writing into a pipe that no one will ever read from.

Additionally, because this error is being raised in a context where no one handles it, the error is fatal for your process. Sadly, node doesn't do a good job of identifying which stream, but we can guess that its in your code, and there is only one stream being written to by this code ...

You can verify that the error is coming from the "stdin" stream if you add an error handler: test.stdin.on('error', (...args) => { console.log('stdin err', args); });

Now the error will be "handled" (poorly), and the process will continue, so you at least know where its coming from.

Specifically this test.stdin.write('q') is writing a q to the stdin of your process. But your child process is ls. It doesn't accept anything on stdin, so it closes stdin. So the OS sends an EPIPE error back when the parent tries to write to the now-closed pipe. So, stop doing that.

You probably expect ls to behave the same as when you type ls interactively, but it probably does not (I suspect your interactive ls is going through a pager, and you're expecting that you need to quit that pager?) This child process is more equivalent to running /bin/ls interactively.