occasional epipe exits from port_command

Ken Ellis <>
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"),
    port_close(Port).

=ERROR REPORT==== 17-Feb-2011::16:55:30 ===
Error in process <0.33.0> with exit value:
{epipe,[{erlang,port_command,[#Port<0.722>,"dummy
data"]},{port_test,do_work,0},{erl_eval,do_apply,5},{erl_eval,eval_lc1,6},{erl_eval,eval_generate,7},{erl_eval,eval_lc,6},{shell,exprs,7},{shell,eval_exprs,7}]}


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]),
    timer:sleep(500),
    port_command(Port,"dummy data"),
    port_close(Port).

** 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"),
    port_close(Port).


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?

Thanks,
Ken


More information about the erlang-questions mailing list