[erlang-questions] gen_server:terminate/2 question

Bernard Duggan <>
Wed Nov 11 06:57:55 CET 2009


Yogish Baliga wrote:
> I have a gen_server process listening for events on message bus. These messages are placed in its message box.  The processing of these messages is  slower than the rate of incoming message. 
>
> When I stop the application (supervisor of which is a parent of the above mentioned gen-server), Will this gen_server get the terminate/2 call immediately or only after all the messages are processed OR should I have a wait logic in the terminate function to wait for the message box to be cleared?
>   
It depends on the shutdown method you've specified in the supervisor. 
If you've nominated 'brutal_kill', I'm pretty sure even terminate/2
doesn't get called.  If you've specified a timeout or 'infinity', I
think the request to shutdown will be placed in the queue after all the
other messages.  If, of course, you reach the timeout before the message
queue is cleared and the shutdown is processed, the supervisor will fall
back to a kill which will bypass the queue (and, as above, will probably
also mean terminate/2 doesn't get called).

(Anyone please feel free to correct me on the above - I've only been
using the gen_server and supervisor stuff for a few months and so could
easily be under some misapprehensions).

Remember also that if you want to ensure all the messages ever sent to
the server are handled, you need to make sure the things sending them
stop before you make the shutdown request.

Cheers,

Bernard


More information about the erlang-questions mailing list