[erlang-questions] Programming question

Sean Hinde sean.hinde@REDACTED
Wed Jan 24 16:48:30 CET 2007


On 24 Jan 2007, at 14:53, Richard Carlsson wrote:

> Sean Hinde wrote:
>> Hi,
>> A quick question for Erlang gurus out there.
>> I have two processes A and B. B spends most of its life in a  
>> series  of gen_server calls towards A:
>> loopB(PidA, S) ->
>>       Res = gen_server:call(PidA, {op, stuff}),
>>       S1 = process(Res, S),
>>       loopB(PidA, S1).
>> The question is how do I set things up so that if A dies B is killed?
>
> Assuming that normal linking of B to A does not work (e.g. if B is
> trapping exits, does not accept an exit message in the waiting state,
> and you can't change the code of A and B), you could spawn a small
> simple watchdog process, and link it to both A and B. It should wait
> for the termination of either of them. If A dies, the watchdog kills
> B by brute force, i.e., exit(B, kill). If B dies, the watchdog dies
> with it, quietly.

Apologies to all!

There was an error in my example. gen_server does indeed do the right  
thing in this case. The problem occurs in this code:

loopB(PidA, S) ->
       Res = (catch gen_server:call(PidA, {op, stuff})),
       S1 = process(Res, S),
       loopB(PidA, S1).

Here the gen_server:call catches the {'EXIT', Reason} signal from the  
dying B process and turns it into a call to exit(Reason).

process B just gets an {'EXIT', Reason} result to the call, but it  
doesn't know if Process A is still alive (there can be several  
reasons for the {'EXIT', Reason}.

One fix might be for gen:call() to issue another exit signal towards  
the calling process after returning the result.

Sean






More information about the erlang-questions mailing list