zoukankan      html  css  js  c++  java
  • Erlang的gen_server的terminate()/2未执行

    官方资料参考:

    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

  • 相关阅读:
    Pandas数据结构简要概述
    pip install PyQt5和pip install PyQt5tools安装后,找不到designer.exe路径
    解决Qt5中利用designer建立二级菜单无法输入中文的处理技巧
    事务
    大家一起讨论一下朋友网的人脉关系算法是怎么实现的
    .net程序员必须学习的10项技术
    KENDOUI控件 GRID中TEMPLATE中条件判断的使用
    Kendo Template 模板中使用KendoUI组件
    Kendo.Grid 使用POPUP时分别定义Create和Edit模板
    Kendo UI 遮罩层组件
  • 原文地址:https://www.cnblogs.com/ribavnu/p/4988543.html
Copyright © 2011-2022 走看看