[erlang-questions] gen_statem confusion

Kenneth Lakin kennethlakin@REDACTED
Wed Jan 18 21:37:50 CET 2017

On 01/18/2017 11:20 AM, Fred Hebert wrote:
> So if you want the next event run internally to be 'next_state' (which I
> assume means "progress to the next state" in your state machine), you'd
> need to return:
> handle_event(timeout, _TimeoutMsg, StateName, Data) ->
>    ...
>    {next_state, StateName, Data, [{next_event, internal, next_state}]}.
> which will enqueue 'next_state' as an internally-generated message
> to be handled.

You need to be a little careful with this. next_event is a _stack_, not
a queue. If you return

{next_state, state, Data, [{next_event, internal, a}, {next_event,
internal, b}]}

and then in state(internal, a, Data) you return

{next_state, state, Data, [{next_event, internal, c}, {next_event,
internal, d}]}

The next three calls (assuming nothing else is pushed on the next_event
stack) will be
state(internal, c, Data)
state(internal, d, Data)
state(internal, b, Data)

It's really easy to overlook this. This subtle quirk was the cause of at
least one bug in a _widely_ used library.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170118/abe6a60f/attachment.bin>

More information about the erlang-questions mailing list