occasional epipe exits from port_command

Ken Ellis kaellis@REDACTED
Thu Feb 17 23:10:21 CET 2011

Hi all,

I get some occasional exits when working with open_port and
port_command.  Reproducing with a simple case below.  Apologies, new
to using these.  This code will occasionally exit with an epipe (try
running repeatedly if you have trouble reproducing):

do_work() ->
    Port = erlang:open_port({spawn,"echo i run quickly"},[exit_status,
stream, use_stdio, stderr_to_stdout]),
    port_command(Port,"dummy data"),

=ERROR REPORT==== 17-Feb-2011::16:55:30 ===
Error in process <0.33.0> with exit value:

Whereas this code, which waits for the shell command to exit by
sleeping briefly, will always throw a bad argument exception:

do_work() ->
    Port = erlang:open_port({spawn,"/bin/fjaldkg"},[exit_status,
stream, use_stdio, stderr_to_stdout]),
    port_command(Port,"dummy data"),

** exception error: bad argument
     in function  port_command/2
        called as port_command(#Port<0.585>,"dummy data")
     in call from port_test:do_work/0

And this one will always work fine, writes to the port while it is still open:
do_work() ->
    Port = erlang:open_port({spawn,"sleep 1"},[exit_status, stream,
use_stdio, stderr_to_stdout]),
    port_command(Port,"dummy data"),

I would think that the port_command would either work, or throw the
bad argument exception.  The relatively rare epipe I don't know how to
handle.  Is it intended behavior?  Or to I have to plan on any
port_command potentially exiting with an epipe?


More information about the erlang-questions mailing list