[erlang-questions] Issues with stdin on ports

Per Hedeland per@REDACTED
Wed Jul 31 23:11:00 CEST 2013


Per Hedeland <per@REDACTED> wrote:
>
>Max Lapshin <max.lapshin@REDACTED> wrote:
>>
>>Your solution is less obtrusive but less stable. What will happen with
>>your system if you leave some processes because your code failed to do
>>os:cmd? Or what will happen if your erlang is killed?
>
>Good points - your solution is better at handling those cases.

Actually, that's quite an understatement. Let me try again (as a general
statement):

The open_port(spawn) design expects an external process that terminates
when it sees EOF on stdin. This is the only way to ensure proper
termination in all failure cases. In the case of a program that doesn't
have this behaviour by default, it can be achieved by modifying the
program as described by Max, or by using some kind of wrapper.

If this isn't feasible, the Unix pid of the external process can be
obtained with erlang:port_info(Port, os_pid), and used to terminate the
process by sending it a signal with os:cmd/1. However this is a
primitive workaround, and in particular if the Erlang VM is terminated,
it doesn't apply, and the external process will continue to run
indefinitely.

--Per



More information about the erlang-questions mailing list