Supervisor也崩溃了??

2009-04-11
今天测试程序的时候出现了一个怪问题:我用一个supervisor(sup)去监督一个gen_server(test)的工作,test崩溃后被重启了,但随即sup也跟着崩溃了,导致整棵监督树倒了...郁闷,检查错误报告(见下)发现test仅仅被重启了一次,不是因为重启次数过多而使得sup崩溃~~
=ERROR REPORT==== 11-Apr-2009::01:51:47 ===
** Generic server test terminating
** Last message in was error
** When Server state == []
** Reason for termination ==
** error
gen_server test is started (test重启时所输出的信息)
** exception exit: {error,{gen_server,call,[test,error]}}
in function gen_server:call/2
13>
=ERROR REPORT==== 11-Apr-2009::01:51:47 ===
** Generic server sup terminating
** Last message in was {'EXIT',<0.54.0>,
{{error,{gen_server,call,[test,error]}},

可见sup系因为收到‘EXIT’信息才崩溃的,大白天谁无缘无故发个‘EXIT’信息出来?Google一番后在Google Groups里找到解答如下:

在erlshell里测试,需要unlink supervisor,这是因为在shell里运行时,进程层次状况如下:
shell
eval_loop
sup

这几个进程是link起来的,如不unlink,在出错时eval_loop会退出重启,导致sup收到{'EXIT', Pid, _}而发生退出。

OK,据此稍为修改监督者的启动函数即可:
{ok, Pid} = supervisor:start_link({local, sup}, sup, []),
unlink(Pid).

M-OSCAR | Powered by Blogger | Entries (RSS) | Comments (RSS) | Designed by MB Web Design | XML Coded By Cahayabiru.com