gen_server cleanup handling

Bernard Duggan bernie@REDACTED
Tue May 11 03:58:19 CEST 2010


Hi list,
     This is something of a followup to my previous question about 
supervision trees.  Basically we have a gen_server that, in the case of 
a standard app shutdown, needs to do some cleanup.  A standard app 
shutdown seems to take the form of supervisors sending 'shutdown' exit 
messages to their children.  Fine so far.  The catch is that in order 
for the terminate() handler to be invoked on our server, we have to turn 
on trap_exit in every gen_server that needs to do cleanup.  This seems 
non-ideal for a few reasons:

* Elsewhere in the docs, we're cautioned against using trap_exit except 
when unavoidable (though I'm happy to accept it if this is one such case).

* It means that we don't get automatic propagation of crashes from any 
worker processes we might spawn.  Instead we have to write a 
handle_info({'EXIT'...) in every gen_server that might ever link to 
another process to ensure those crashes are propagated properly.  This 
seems like it could be solved by instead spawning any worker processes 
as a child of our supervisor (which is what Garrett suggested we should 
do anyway) - if that's a good reason to set up things that way, I'm 
likewise happy to accept it and rearrange our code accordingly.

* Most concerning, though, is the possibility of some library call 
creating a link to a temporary worker process (or any process for that 
matter) whose crash should propagate through us - in this case we'd 
still have to have the handle_info({'EXIT'...) setup as a catchall which 
seems like a fiddly, repetitive bit of code we'd rather avoid if possible.

So what's the thinking about this?  Am I missing something obvious?  
Should I just turn on trap_exit willy-nilly wherever I need shutdown 
cleanup?  Should I just suck it up and write the 'EXIT' message handlers 
in all such gen_servers?

Cheers,

Bernard


More information about the erlang-questions mailing list