Erlang的gen_server的terminate()/2未执行

简介: 官方资料参考: Module:terminate(Reason, State) Types: Reason = normal | shutdown | {shutdown,term()} | term() State = term() This function is ...

官方资料参考:

Module:terminate(Reason, State)

Types:

Reason = normal | shutdown | {shutdown,term()} | term()
State = term()

This function is called by a gen_server when it is about to terminate. It should be the opposite of Module:init/1 and do any necessary cleaning up. When it returns, the gen_server terminates with Reason. The return value is ignored.

Reason is a term denoting the stop reason and State is the internal state of the gen_server.

Reason depends on why the gen_server is terminating.

If it is because another callback function has returned a stop tuple {stop,..}, Reason will have the value specified in that tuple.

If it is due to a failure, Reason is the error reason.

If the gen_server is part of a supervision tree and is ordered by its supervisor to terminate, this function will be called with Reason=shutdown if the following conditions apply:

  • the gen_server has been set to trap exit signals, and
  • the shutdown strategy as defined in the supervisor's child specification is an integer timeout value, not brutal_kill.

Even if the gen_server is not part of a supervision tree, this function will be called if it receives an 'EXIT' message from its parent. Reason will be the same as in the'EXIT' message.

Otherwise, the gen_server will be immediately terminated.

 

 

实际场景:

gen_server的三个进程,A、B、C,其中A是B、C的父进程,即A进程调用的 B、C的  gen_server:start_link.

A、B、C三个进程全都未捕获 trap_exit,在C进程退出的时候  A、B进程的terminate()/2函数未执行。

 

原因分析:

参考官方资料可以知,如果一个gen_server进程不是监控树的一部分,gen_sever进程在收到父进程'EXIT'信息时将会调用terminate()/2函数。

(注:不是只有收到父进程的 'EXIT'信息时才会调用,别理解歧义)

需要设置 process_flag(trap_exit, true),link的进程死掉时,当前gen_server进程的handle_info()/2将会收到 :{'EXIT',Pid,Reason}

 

代码修改:

在A、B进程对应的模块中添加 handle_info()/2 对 {'EXIT',Pid,Reason} 的匹配,并 返回  {stop,Reason,NewState}

 

 

参考:

http://blog.csdn.net/zcc_0015/article/details/18054889

http://blog.sina.com.cn/s/blog_96b8a1540101314t.html

相关文章
|
4月前
|
SQL
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
55 0
|
11月前
|
关系型数据库 MySQL Devops
docker容器刚启动就停止 — 运行mysql 报错 mysqld: [ERROR] Fatal error in defaults handling. Program aborted!
docker容器刚启动就停止 — 运行mysql 报错 mysqld: [ERROR] Fatal error in defaults handling. Program aborted!
627 0
|
4月前
|
Java 中间件 Serverless
Serverless 应用引擎操作报错合集之在阿里函数计算中,云函数怎么一直报错Function instance exited unexpectedly(code 1, message:operation not permitted) with start command 'php server.php '.如何解决
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
106 2
|
4月前
|
SQL
SQL Server2008 安装报错Restart computer failed的解决办法
SQL Server2008 安装报错Restart computer failed的解决办法
179 0
|
4月前
|
安全 Ubuntu 应用服务中间件
【报错】ubutun下启动nginx失败:500 internal server error
【报错】ubutun下启动nginx失败:500 internal server error
493 0
|
Linux 流计算
Flink - 本地执行 Failed to start the Queryable State Data Server
Flink 本地执行任务报错 Failed to start the Queryable State Data Server 以及 Unable to start Queryable State Server. All ports in provided range are occupied. 根据报错分析是因为本地端口被占用,没有足够端口供 Flink 本地客户端启动,所以解决方法就是处理被占用的端口。...
141 0
Flink - 本地执行 Failed to start the Queryable State Data Server
|
安全 程序员 C++
《gen_server.erl源码》
erlang程序员研究OTP,如同C++程序员研究STL一样重要。
《gen_server.erl源码》
|
Oracle 关系型数据库 Linux
oom_kill_process造成数据库挂起并出现found dead shared server
这篇博客是上一篇博客Oracle shutdown immediate遭遇ORA-24324 ORA-24323 ORA-01089的延伸(数据库挂起hang时,才去重启的),其实这是我们海外一工厂的遇到的案例,把内容拆开是因为这个case分开讲述显得主题明确一些。
1297 0