Handling event handler crashes

How does one keep handlers in the event managers after the handler crashes? After the crash they are removed from it by default. I’ve seen gen_event:add_sup_handler/3, but the problem is that it creates a link to the calling process so I’m not really sure which process should call it.

Naive solution would be to wrap all callbacks in try-catch, but I’m sure that there is a better solution. How can I “monitor” and act upon handler crash?

I investigated how lager does (did) it. Wrote it up here: Supervised event handlers in Erlang | Roger's Blog

It’s a bit confusing, but I can give you a working example. In this project we have the gen_event behaviour module cgf_event. A supervisor (cgf_event_sup) starts a gen_event worker behaving to this as well as a gen_server behaviour worker, implemented in [cgf_event_server](https://, which “supervises” the event handler. It is responsible for installing and reinstalling the handler.

1 Like

Great post indeed, but I was wondering if I can have handler in the event manager all the time, without having to re-add them.

I can’t open the links. Does this mean having a separate supervisor for every handler?

Not really, no. If they die, they need to be re-added. There’s no strictly automatic way to do that. You could, I guess, write a wrapper handler that swallows exceptions.

Terminology nitpick (the docs, not you): It’s not strictly a “supervisor” (as in: it’s not a supervisor) for the handler. This, I think, is why lager (if I’m remembering correctly) calls it a guard.

I think (but, as you saw, it’s 10 years since I wrote the post) that you can have a single “supervisor” for all of the handlers, but that it’s potentially easier to reason about it if it’s one-for-one.

Sorry I fixed the links. You just need another process to react to the handler exit, that process could handle any number of handlers.