官方资料参考:

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

Erlang的gen_server的terminate()/2未执行的更多相关文章

  1. Erlang模块gen_server翻译

    gen_server 概要: 通用服务器行为描述: 行为模块实现服务器的客户端-服务器关系.一个通用的服务器进程使用这个模块将实现一组标准的接口功能,包括跟踪和错误报告功能.它也符合OTP进程监控树. ...

  2. jsp中一个标签两种方式绑定两个click事件导致未执行的问题

    近日,在开发过程中,写了一个标签 <li id="a1" onclick="doSomething()">...</li> 在js页面中 ...

  3. Oracle 11g 安装过程中“检查网络配置要求 未执行”解决方法

    正在检查网络配置要求... 检查完成.此次检查的总体结果为: 未执行 网上查了一下,很多朋友都遇到这个问题而无从下手,其实解决起来很容易的. 只需要在 Windows XP 中安装 Microsoft ...

  4. flock防止crontab脚本周期内未执行完重复执行(转)

    如果某脚本要运行30分钟,可以在Crontab里把脚本间隔设为至少一小时来避免冲突.而比较糟的情况是可能该脚本在执行周期内没有完成,接着第二个脚本又开始运行了.如何确保只有一个脚本实例运行呢?一个好用 ...

  5. 关闭页面,window.onunload事件未执行的原因

    1.问题描述: JS中定义widow.onunload= function(),页面关闭时,logout()函数未执行. window.onunload = function() { logout() ...

  6. 记一次Laravel 定时任务schedul:run未执行的处理

    关于Laravel的任务调度(定时任务)的配置在此不做赘述,跟着官方文档一步一步的操作是不会导致定时任务不能正常工作的. 为保证能及时捕获定时任务执行出现异常的原因,只需在配置系统crontab时指定 ...

  7. [erlang 002]gen_server中何时会跑到terminate函数

    一.从start方法产出的独立gen_server进程 实验代码: %%%-------------------------------------- %%% @Module  : %%% @Auth ...

  8. erlang OTP gen_server 图解分析

    http://www.hoterran.info/otp-gen_server-sourcecode 在阅读erlang的otp源码gen_server.erl的时候,一直想写点什么,用一种最好的方式 ...

  9. quartz 防止上一任务未执行完毕,下一时间点重复执行

    /** * 订单监控类 * 定时扫描所有待付款订单,超时自动取消 * Created by huangbaidong * 2017/3/29. */ @Component public class O ...

随机推荐

  1. 洛谷P1395 会议(CODEVS.3029.设置位置)(求树的重心)

    To 洛谷.1395 会议 To CODEVS.3029 设置位置 题目描述 有一个村庄居住着n个村民,有n-1条路径使得这n个村民的家联通,每条路径的长度都为1.现在村长希望在某个村民家中召开一场会 ...

  2. php 解析HTTP协议六种请求方法,get,head,put,delete,post有什么区别

    GET: 请求指定的页面信息,并返回实体主体.HEAD: 只请求页面的首部.POST: 请求服务器接受所指定的文档作为对所标识的URI的新的从属实体.PUT: 从客户端向服务器传送的数据取代指定的文档 ...

  3. [HihoCoder1398]网络流五·最大权闭合子图

    题目大意:有$N$项活动$M$个人,每个活动$act_i$有一个正的权值$a_i$,每个人$stu_i$有一个负的权值$b_i$.每项活动能够被完成当且仅当该项活动所需的所有人到场.如何选择活动使最终 ...

  4. 20172302 《Java软件结构与数据结构》第八周学习总结

    2018年学习总结博客总目录:第一周 第二周 第三周 第四周 第五周 第六周 第七周 第八周 教材学习内容总结 第十二章 优先队列与堆 1.堆(heap)是具有两个附加属性的一棵二叉树: (1)它是一 ...

  5. 安装 jenkins

    1. 将jenkins.war包放在 tomcat  的 webapps 目录下即可 2 重启 tomcat 3. 通过浏览器访问 IP:8080/jenkins

  6. nginx image_filter 配置记录

    nginx_image_filter http_image_filter_module 配置 ---------------------------------- 第一种: //官方配置 locati ...

  7. Objective-C学习笔记(三)——用Objective-C编写第一个程序:Hello,World!

    不管是哪一个程序猿,或者是学习哪一门计算机语言.写的第一个程序基本上就是Hello World. 今天我们用OC来实现第一个程序:Hello World. 在Xcode中选择新建一个项目,在对话框中选 ...

  8. 为Qemu aarch32开发板添加sd卡

    Qemu: 2.8.0 开发板: vexpress 1.制作sd卡镜像 2.分区 sudo losetup /dev/loop0 fs_vexpress_1G.img sudo fdisk /dev/ ...

  9. Unity Shader-后处理:Bloom全屏泛光

    一.简介   今天来学习一下全屏Bloom效果,有时候也叫Glow效果,中文一般叫做“全屏泛光”,这是一种可以模拟出HDR的全屏后处理效果,但是实现原理与HDR相差很远,效果比HDR差一些,但是比HD ...

  10. linux-提示用户不在 sudoers文件中,此事将被报告。

    在安装oracle创建用户后使用su – oracle命令进入执行相关修改信息(vi file命令)时提示无权限修改 “Can't open file for writing”或“operation ...