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

相关文章
|
6月前
|
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根本解决方案
62 0
|
5月前
|
监控 Serverless Docker
函数计算操作报错合集之遇到报错:Python Jedi client: couldn't create connection to server.是什么原因
在使用函数计算服务(如阿里云函数计算)时,用户可能会遇到多种错误场景。以下是一些常见的操作报错及其可能的原因和解决方法,包括但不限于:1. 函数部署失败、2. 函数执行超时、3. 资源不足错误、4. 权限与访问错误、5. 依赖问题、6. 网络配置错误、7. 触发器配置错误、8. 日志与监控问题。
244 0
|
2月前
|
监控
{"level":"warn","ts":"2023-11-07T00:35:53.400+0800","caller":"etcdserver/server.go:2048",&
{"level":"warn","ts":"2023-11-07T00:35:53.400+0800","caller":"etcdserver/server.go:2048",&
emqtt 启动报错 Erlang closed the connection 查看状态报错
emqtt 启动报错 Erlang closed the connection 查看状态报错 Node 'emq@192.168.*.*' not responding to pings.
259 0
|
6月前
|
SQL 安全 网络安全
解决:provider:Named Pipes Provider error:40无法打开SQL Server的连接 的3种方式
解决:provider:Named Pipes Provider error:40无法打开SQL Server的连接 的3种方式
1073 0
emqtt 启动报错 Erlang closed the connection 查看状态报错
emqtt 启动报错 Erlang closed the connection 查看状态报错 Node 'emq@192.168.*.*' not responding to pings.
254 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 本地客户端启动,所以解决方法就是处理被占用的端口。...
150 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分开讲述显得主题明确一些。
1303 0