[erlang-questions] Question about gen_event

Martin Bjorklund <>
Thu Oct 2 16:38:11 CEST 2008


Kostis Sagonas <> wrote:
> I've been staring at the code of 'gen_event' and I cannot make much 
> sense of how the code for swapping handlers works.  In fact, I am quite 
> convinced there is a bug in there but it is being silenced by the use of 
> a catch.  Can somebody please confirm that this is a bug or enlighten me 
> on what's happening?
> The code of gen_event:do_swap/7 reads:
> do_swap(Mod1,Handler1,Args1,State1,Handler2,Args2,SName) ->
>      %% finalise the existing handler
>      State2 = do_terminate(...),
>      {Mod2,Handler} = new_handler(Handler2, Handler1),     %% <---

This is correct; the module is extracted from Handler2, and the
#handler{} is updated.

>      case catch Mod2:init({Args2, State2}) of
>          {ok, State2a} ->
>              {ok, Handler#handler{state = State2a}};
>          Other ->
>              report_terminate(Handler2, crash, ...),       %% <---

But this is wrong; Handler2 should be Handler here.  Handler2 is
either a module name (atom()) or a tuple {ModuleName, Id} ({atom(),
term()}).  But report_terminate wants a #handler{}.

The variable names are pretty confusing... I suspect that once upon a
time, Handler was just a modulename or the 2-tuple, and it has evolved
into a record...


More information about the erlang-questions mailing list