erlanggen-event

Notify and stop gen_event manager atomically


When a particular message is received by my gen_event manager process, I want it to stop after all handlers have handled it and before they get and handle any other events. The only way I could find is this:

-module(manager).

...

stop(Reason) ->
    gen_event:sync_notify(manager, {stop, Reason}),
    gen_event:stop(manager).

But this requires all handlers to return remove_handler from handle_event({stop, Reason}, State), otherwise they could handle an event sent from a different process after sync_notify and before stop. I would prefer to have an approach that imposes no requirements on handlers.


Solution

  • As far as I know, there is no other way to do it than the one you're using for handling in a way that is truly limited to one call, outside of just plainly killing the event manager with exit(Pid, Reason) or ordering it to be shut down by its own supervisor.