gen_event feature request

Serge Aleynikov serge@REDACTED
Sat Sep 24 18:39:50 CEST 2005

Current implementation of gen_event doesn't have an option to atomically 
check for an installed duplicate event handler before adding a new event 
handler.  The common practice seems to be:

case lists:member(NewHandler, gen_event:which_handlers(EventMgr)) of
true ->
false ->
     gen_event:add_handler(EventMgr, NewHandler)

Apparently this approach has a race condition.  Secondly, when an 
existing event handler is being replaced by a new one using 
gen_event:swap_handler/3 this option also does not check if the 
NewHandler is already installed, which results in a duplicate handler 
added.  As example of this can be found in the error_logger module:

swap_handler(tty) ->
     gen_event:swap_handler(error_logger, {error_logger, swap},
			   {error_logger_tty_h, []}),

When you call error_logger:swap_handler(tty) multiple times you'll find 
that multiple copies of error_logger_tty_h get installed (which is not 

In order to overcome these issues I would recommend to add new features:

1. gen_event:add_handler_unique(EventMgrRef, NewHandler, Args) -> Result
         Result = ok | already_installed | {'EXIT',Reason} | term()

2. gen_event:swap_handler_unique(
      EventMgrRef, {OldHandler,Args1}, {NewHandler,Args2}) -> Result
         Result = ok | already_installed | {error, Reason} | term()

'already_installed' is returned if the NewHandler is already installed.



More information about the erlang-questions mailing list