[erlang-questions] Erlang and Akka, The Sequel

Fred Hebert mononcqc@REDACTED
Fri Apr 3 20:30:43 CEST 2015


On 04/03, ayodele abejide wrote:
>There is an  implementation of Future/Promise in Elixir its called Task[1]

>Reading the source[2] Task is a wrapper around proc_lib that monitors 
>the
>calling process for error handling and uses patterns described by Joe in
>its implementation. Understanding the primitives would make implementing
>something similar in Erlang trivial
>
>https://github.com/elixir-lang/elixir/blob/v1.0.3/lib/elixir/lib/task.ex#L146-L209

Interestingly enough, this shows these patterns are harder to implement 
correctly than it seems. Looking at the code, if the caller process is 
trapping exits (process_flag(trap_exit, true)), then the caller won't 
only receive the 'DOWN' message sent by the monitor, but also an 'EXIT' 
tuple. If the `await` function is wrapped in a `catch`, then the message 
won't be handled properly.

The Elixir solution conveniently ignores that possibility in await() and 
therefore risks leaking messages (that will remain stuck in the process 
mailbox).

The Erlang RPC mechanism handles that one properly by spawning [1] a 
middle-man process to handle these failures [2].

[1]: 
https://github.com/erlang/otp/blob/maint/lib/kernel/src/rpc.erl#L629-L649
[2]: 
https://github.com/erlang/otp/blob/maint/lib/kernel/src/rpc.erl#L328-L361

The basic pattern is simple enough, but fault-tolerance is often 
trickier than one expects it to be.

Regards,
Fred.



More information about the erlang-questions mailing list