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

  • 相关阅读:
    centos6.x 配置bond
    Js学习(2)
    Js学习(1)
    Java源码阅读计划(1) String<II>
    【461】汉明距离
    【617】合并二叉树
    Java源码阅读计划(1) String<I>
    Dubbo的高可用性
    Dubbo SpringBoot配置方法
    Dubbo基本配置属性
  • 原文地址:https://www.cnblogs.com/ribavnu/p/4988543.html
Copyright © 2011-2022 走看看