restarting child processes

Lennart Ohman <>
Thu Oct 20 00:06:35 CEST 2005


I guess from your code that you hope the child process will
stop on its own!?
The problem then is that the so called server side timeout
(what when I teach OTP call the gen_server gets bored :-)
is controlled by having for instance init return {ok,LoopData,TimeOut}.

Changing your code would then be:

start_link() ->
    io:format("Child starting~n"),
    gen_server:start_link(swf_kid, [{timeout, 750}], []).

init([{timeout,T}]) ->
    process_flag(trap_exit, true),
    {ok, [],T}.

Since you seem to want to forward the timeout as a parameter
from the supervisor to the newly created gen_server process.

/Lennart


-------------------------------------------------------------
Lennart Ohman                   office  : +46-8-587 623 27
Sjoland & Thyselius Telecom AB  cellular: +46-70-552 67 35
Sehlstedtsgatan 6               fax     : +46-8-667 82 30
SE-115 28, STOCKHOLM, SWEDEN    email   : 

-----Original Message-----
From: Chris Campbell [mailto:] 
Sent: Wednesday, October 19, 2005 11:59 PM
To: Lennart Ohman
Cc: 
Subject: Re: restarting child processes

On 19/10/05, Lennart Ohman <> wrote:
> Hi Chris,
> the childprocess (or worker) crashes in its init-phase. You
> can se this by examining the Context field in the supervisor
> report (start_error). The supervisor will then consider itself
> to be a failure and crash in its own initphase.

Ok.  Thanks.

> As a note, your child is actually not a proper OTP process either,
> or to put it in a more polite way :-) one can say that you have
> choose to role-your-own by not using for instance the gen_server
> behaviour.

[snip]

Ok, I rewrote it so it uses gen_server however it never times out.

% swf_kid.erl
-module(swf_kid).
-behaviour(gen_server).

-export([start_link/0, terminate/2]).
-export([init/1, handle_cast/2, handle_info/2, handle_call/3]).

start_link() ->
    io:format("Child starting~n"),
    gen_server:start_link(swf_kid, [{timeout, 750}], []).

init(_A) ->
    process_flag(trap_exit, true),
    {ok, []}.

handle_cast(_A, S) ->
    {noreply, S}.

handle_call(_R, _F, S) ->
    {reply, dont_care, S}.

handle_info(timeout, S) ->
    io:format("Child exiting!~n"),
    {stop, arghh, S}.

terminate(shutdown, _S) ->
    ok.

% swf_supervisor.erl
-module(swf_supervisor).
-behaviour(supervisor).
-export([start_link/0, init/1]).


start_link() ->
    supervisor:start_link(swf_supervisor, []).

init(_X) ->
    {ok, {{one_for_one, 50, 1},
          [{kid, {swf_kid, start_link, []},
            permanent, brutal_kill, worker, [swf_kid]}]}}.





More information about the erlang-questions mailing list