警告:未定义的回调函数 terminate/3(行为 'gen_statem')**

Warning: undefined callback function terminate/3 (behaviour 'gen_statem')**

这个警告有多严重?我可以在出现此警告时使用此模块吗?此警告的副作用是什么。

Warning: undefined callback function terminate/3 (behaviour 'gen_statem')**

Erlang/OTP: 19.0.7

 
git clone https://github.com/inaka/apns4erl.git
cd apns4erl/
wget https://s3.amazonaws.com/rebar3/rebar3 && chmod +x rebar3
./rebar3 compile
===> Fetching coveralls v2.2.0
===> Fetching jsx v2.10.0
===> Analyzing applications...
===> Compiling jsx
===> Compiling coveralls
===> Verifying dependencies...
===> Fetching base64url v1.0.1
===> Fetching gun v1.3.3
===> Fetching jsx v3.0.0
===> Fetching cowlib v2.7.3
===> Analyzing applications...
===> Compiling cowlib
===> Compiling base64url
===> Compiling gun
===> Compiling jsx
===> Analyzing applications...
===> Compiling apns
**src/apns_connection.erl:22: Warning: undefined callback function terminate/3 (behaviour 'gen_statem')**

How serious is this warning?

应该定义的函数不存在。

Can I use this module with this warning?

主管在必要时使用 terminate() 函数关闭子进程。如果子进程永远不需要关闭,那你就很好。

您可以在源代码中添加一个什么都不做的终止函数:

terminate(_Reason, _State, _Data) -> ok.

但是,更好的选择可能是向 github 存储库报告错误,希望他们能快速修复代码。

或者,您可以尝试升级您的 erlang 版本。在较新的 erlang 版本中,会自动为您定义 terminate() 函数。

What the side-effects of this warning.

您的程序有时会崩溃。

在 Erlang/OTP 19.3 及更高版本中,由于 this change, the terminate/3 callback is optional for gen_statem. As stated in the documentation:

This callback is optional, so callback modules need not export it. The gen_statem module provides a default implementation without cleanup.


所以让我们看看在早期版本中不包含此功能的后果是什么。根据文档,terminate 函数将在三种不同的情况下被调用:

  • 另一个回调函数在 Actions
  • 中返回了一个停止元组 {stop,Reason}
  • gen_statem 是监督树的一部分,由其主管命令终止(仅当 gen_statem 已设置为捕获退出信号,并且关闭策略不是 brutal_kill)
  • 进程收到来自其父进程
  • 'EXIT'消息

当然可以想象 none 会出现特定的 gen_statem,因此缺少 terminate 功能不会产生负面影响。在最坏的情况下,terminate 函数将在进程即将退出时被调用,从而以与原始进程不同的退出原因终止进程,隐藏原始问题的原因。