[erlang-questions] supervisors, gen_server:terminate/2, and trap_exit

Fred Hebert <>
Wed Mar 25 06:33:42 CET 2015


On 03/24, Youngkin, Rich wrote:
>> "The supervisor process sends an exit signal to the child process and
>> waits for acknowledgment. The child process should handle the
>> {'EXIT',ParentPid,shutdown} message and terminate with reason shutdown
>> if it traps exit signals."
>>
>
>In my tests I didn't have to handle(_info) {'EXIT', ParentPid, shutdown).
>terminate/2 just gets called as a result of trapping exits when the
>supervisor directs the child to terminate (i.e., when the application is
>shutting down).

Yes, that is wht I meant. Sorry I was possibly not clear. The child 
process should handle that *automatically*. You won't see the signal 
yourself because the behaviours handles it. You will see other signals 
(from processes that are not your parent) in handle_info/2.

>So terminate/2 gets called, with reason "shutdown", not
>handle_info({'EXIT', ParentPid, shutdown}). But maybe... {'EXIT',
>ParentPid, shutdown} would be sent if the supervisor failed??? I didn't
>test that, but supervisors don't fail, right?
>

The message gets sent, but is caught by OTP for that specific one.  
Seehttps://github.com/erlang/otp/blob/maint/lib/stdlib/src/gen_server.erl#L380-L381 
This however can only work when trapping exits.

>
>WRT to trap exit, yes.  WRT "The child process should handle the {'EXIT',
>ParentPid, shutdown} message...", no.
>

Yes. But it's just that the child process sees it handled by OTP. You, 
as an implementer of a gen_server or gen_fsm do not see it, but the 
child process *does* handle it. Sorry for the confusion.



More information about the erlang-questions mailing list