why catch gen_server; MOD:handle_call?

Martin J. Logan <>
Thu Jul 8 16:20:04 CEST 2004


-snip-
> 
> Moving the cleanup to a supervisor would discourage that type of
> mistake, but the canned supervisors don't have any mechanism for a
> process-specific cleanup. I'm not convinced they should,
> either. Putting the cleanup code in the same module as the server
> doesn't seem horrible, as long as you remember that you can't always
> rely on the State variable being at true reflection of system state.
> If you write code like this you're fine:
> 
>   terminate(normal, State) ->
>         % do anything you want, the State is consistent with the world
>         ok;
> 
>   terminate(shutdown, State) ->
>         % do anything you want, the State is consistent with the world
>         ok;
> 
>   terminate(Reason, Possibly_old_state) ->
>         % keep in mind that the State is not necessarily consistent
>         % with what has happened; the gen_server may have terminated
>         % while partway through a callback.
>         ok
> 

I agree that this would be a solution. This is how I will write code in
the future. I am now mindful of the issue. The thing is I don't remember
seeing any cautionary in the docs and the problem is a rather subtle
one. Many new coders and experienced ones alike could be bitten by this.
Perhaps the otp guys could advertise this potential stumbling block to
avoid users making a mistake with terminate.  


> I don't remember if they teach that in the advanced OTP course
> (Francesco? Lennart?). Changing the emacs skeleton would be a
> nudge in the right direction.
> 
> Matthias




More information about the erlang-questions mailing list