GJain
GJain

Reputation: 5093

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

How serious is this warning? Can I use this module with this warning? What the side-effects of this warning.

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')**

Upvotes: 1

Views: 264

Answers (2)

legoscia
legoscia

Reputation: 41668

In Erlang/OTP 19.3 and later, as a result of 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.


So let's figure out what the consequences are of not including this function in earlier versions. According to the documentation, the terminate function would be called in three different cases:

  • another callback function has returned a stop tuple {stop,Reason} in Actions
  • the gen_statem is part of a supervision tree and is ordered by its supervisor to terminate (only if the gen_statem has been set to trap exit signals, and the shutdown strategy is not brutal_kill)
  • the process receives an 'EXIT' message from its parent

It's certainly imaginable that none of those situations would arise for a particular gen_statem, and thus the lack of a terminate function would have no negative consequences. In the worst case, the terminate function would be called when the process was already about to exit, thus terminating the process with a different exit reason than the original one, hiding the cause of the original problem.

Upvotes: 1

7stud
7stud

Reputation: 48659

How serious is this warning?

A function that is supposed to be defined is not there.

Can I use this module with this warning?

Supervisors use the terminate() function to shut down a child process when necessary. If the child process never needs shutting down, then you are good.

You could add a do nothing terminate function to the source code:

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

But, a better option might be to report the error to the github repository and hopefully they will make a quick fix to the code.

Or, you could try upgrading your erlang version. In newer erlang versions, the terminate() function is automatically defined for you.

What the side-effects of this warning.

Your program crashes at some point.

Upvotes: 2

Related Questions