timer cleanup in case gen_server crashes

Reto Kramer <>
Fri Sep 3 03:12:33 CEST 2004


If I start an interval timer in the init function of a gen_server like 
this:

init([]) ->
     process_flag(trap_exit, true),
     {ok, TRef} = timer:apply_interval(get_period(),
				      ?MODULE, trigger_heartbeat, []),
     {ok, #state{..., send_timer=TRef, ...}}.

How can I then make sure that the timer is canceled in case the 
gen_server itself crashes?

Will this happen automatically because the gen_server process is the 
parent of whatever the timer module spawns?

I don't want to have the supervisor that starts my gen_server to know 
about the timer, I view the timer as an implementation detail of what 
the gen_server does. Would erlang gurus say this is reasonable?

Thanks,
- Reto




More information about the erlang-questions mailing list