不论是新手还是熟手,写gen_server时常会遇到terminate/2,有时执行,有时却不执行的困惑. 比如stackoverflow中的Handling the cleanup of the gen_server state,因为terminate的文档写得比较模糊,并没有给出如何让terminate/2一定会被执行的方案. 为了理顺各种情形,做了个小实验,结论如下: 让进程退出的来源有二种: 内部原因,自己运行完退出或发生异常crash退出. 外部原因,使用erlang:exit/2强制…