[erlang-questions] Gracefully Exit from state_timeout gen statem

Raimo Niskanen raimo+erlang-questions@REDACTED
Fri Aug 17 14:31:40 CEST 2018


On Wed, Aug 15, 2018 at 04:31:21PM -0400, Code Wiget wrote:
> Thanks for the reply. I’m having an issue getting the full report because lager is truncating it. Will the stack trace help?
> 
> Stacktrace: [{gen_statem,parse_event_result,8,[{file,"gen_statem.erl"},{line,1267}]},{gen_statem,loop_event,6,[{file,"gen_statem.erl"},{line,1015}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,247}]}]
> 
> But the process is exiting on this line:
> > handle_event(state_timeout, _Msg, StateName, State) ->
> >     ?LOG(warning, "STATE TIMEOUT(State:~p)", [StateName], State),
> >     {stop, timeout};    <- This line

This is consistent with e.g OTP-20.0 and returning {stop, timeout} from the
event handler.  The process should terminate with exception class 'exit',
reason 'timeout' and that stacktrace.

Before terminating it should print the termination reason through
error_logger:format/2, and I guess that is where Lager gets involved.

The term [smdpp_proxy,{state_timeout,timeout}, [{data,[{"State",{awaiting_
that is involved in the 'badmatch' exception is argument 2 to
error_logger:format/2 (the name of the gen_statem server is smdpp_proxy,
right?).  The first argument is a format string.

So it seems Lager's event handler error_logger_lager_h exits with a
'badmatch' involving the term that is the arguments to the format string.
Smells like an internal Lager error to me.  Maybe.  Probably...

/ Raimo


> 
> 
> On Aug 15, 2018, 9:45 AM -0400, Raimo Niskanen <raimo+erlang-questions@REDACTED>, wrote:
> > On Tue, Aug 07, 2018 at 03:05:08PM -0400, Code Wiget wrote:
> > > Would like to update this to say that I’m using {state_timeout, 2000, timeout}, rather than {state_timeout, 2000}
> > >
> > > On Aug 7, 2018, 2:29 PM -0400, Code Wiget <codewiget95@REDACTED>, wrote:
> > > > Hi everyone,
> > > >
> > > > How do you gracefully exit from a state_timeout in a gen_statem? Generally, for a gen_statem, I can return “stop” or {stop, Reason} from a state function and the statem will terminate. However, if I use a state timeout method, returned from init:
> > > >
> > > > > {ok, new_state, State, {state_timeout, 2000}}
> > > >
> > > > The new_state(state_timeout, Info, State) method is called after 2000. Returning “stop” from this given an error…:
> > > >
> > > > > 18:22:33.039 [error] <0.576.0> :  Lager event handler error_logger_lager_h exited with reason {'EXIT',{{badmatch,[smdpp_proxy,{state_timeout,timeout},[{data,[{"State",{awaiting_
> >
> > Can you post the whole printout?
> > The truncation hides where the process has crashed.
> >
> > > > >
> > > > What can I return from this function to gracefully exit? Can this be done with state_timeout? If this is an obvious question, please point me in the right direction. The documentation here:http://erlang.org/doc/man/gen_statem.html doesn’t seem to mention any return from the state_timeout.
> > > >
> > > > Thanks!
> > > >
> > > >
> >
> > --
> >
> > / Raimo Niskanen, Erlang/OTP, Ericsson AB


-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list