Problem with trap_exit

Ulf Wiger ulf@REDACTED
Fri Aug 18 10:01:55 CEST 2006


> Hi all.
> I'm having some problem trying to trap an exit in a gen_fsm.
> I've made this small example of the problem attached to this mail. It's
> a gen_server (test_server.erl) that starts a gen_fsm (test_fsm.erl)
> using gen_fsm:start_link(...)
> When I by purpose crash the server I expect that the
> handle_info-function in the state-machine to handle the {'EXIT',
> ...}-signal since I've turned on trap_exit.
> Instead the terminte is called with the reason for the crash.
> Why is the signal not traped?
> Thanks,
> // Alex

It _is_ trapped - otherwise, terminate() wouldn't be called.
Most likely, the main loop in gen_fsm is trapping the exit,
which it does if the 'EXIT' message comes from the parent
process. The thinking is that the parent process is most
likely a supervisor, and the gen_fsm process a worker process.
It then makes sure that it's not 'orphaned'.

If you test your program from the shell, try unlinking the
gen_fsm process, then killing the shell process (call
exit(foo) at the shell prompt), and then link to the fsm
process from the new shell process.

BR,
Ulf W





More information about the erlang-questions mailing list