anchor.diff:      end.
   
   loop_receive_result(
  -  Parent, Debug, #{state := State} = S, Hibernate, Event) ->
  +  Parent, Debug,
  +  #{state := State,
  +    timer_types := TimerTypes, cancel_timers := CancelTimers} = S,
  +  Hibernate, Event) ->
       %% From now the 'hibernate' field in S is invalid
       %% and will be restored when looping back
       %% in loop_event_result/11
       NewDebug = sys_debug(Debug, S, State, {in,Event}),
       %% Here is the queue of not yet handled events created
       Events = [],
  -    loop_event(Parent, NewDebug, S, Events, Event, Hibernate).
  +    %% Cancel any running event timer
  +    case
  +	cancel_timer_by_type(timeout, TimerTypes, CancelTimers)
  +    of
  +	{_,CancelTimers} ->
  +	    %% No timer cancelled
  +	    loop_event(Parent, NewDebug, S, Events, Event, Hibernate);
  +	{NewTimerTypes,NewCancelTimers} ->
  +	    %% The timer is removed from NewTimerTypes but
  +	    %% remains in TimerRefs until we get
  +	    %% the cancel_timer msg
  +	    NewS =
  +		S#{
  +		  timer_types := NewTimerTypes,
  +		  cancel_timers := NewCancelTimers},
  +	    loop_event(Parent, NewDebug, NewS, Events, Event, Hibernate)
  +    end.
   
   %% Entry point for handling an event, received or enqueued
   loop_event(
     Parent, Debug,
  -  #{state := State, data := Data, timer_types := TimerTypes,
  -    cancel_timers := CancelTimers} = S_0,
  +  #{state := State, data := Data} = S,
     Events, {Type,Content} = Event, Hibernate) ->
       %%
       %% If (this old) Hibernate is true here it can only be
  
positive.code: 	     {bad_action_from_state_function,Action},
  	     ?STACKTRACE()};
  	{timeout,infinity,_} ->
  	    %% Ignore - timeout will never happen and already cancelled
  	    parse_actions(
  	      Debug, S, State, Actions,
  	      Hibernate, TimeoutsR, Postpone, NextEventsR);
  
negative.code: 	#{} ->
  	    %% Unseen type - handle
  	    NewSeen = Seen#{TimerType => true},
  	    if
  		TimerType =:= timeout ->
  		    %% Handle event timer later
  		    parse_timers(
  		      TimerRefs, TimerTypes, CancelTimers, TimeoutsR,
  		      NewSeen, [Timeout|TimeoutEvents]);
  		Time =:= infinity ->
  		    %% Cancel any running timer
  		    {NewTimerTypes,NewCancelTimers} =
  			cancel_timer_by_type(
  			  TimerType, TimerTypes, CancelTimers),
  		    parse_timers(
  		      TimerRefs, NewTimerTypes, NewCancelTimers, TimeoutsR,
  		      NewSeen, TimeoutEvents);
  		Time =:= 0 ->
  		    %% Cancel any running timer
  		    {NewTimerTypes,NewCancelTimers} =
  			cancel_timer_by_type(
  			  TimerType, TimerTypes, CancelTimers),
  		    %% Handle zero time timeouts later
  		    TimeoutEvent = {TimerType,TimerMsg},
  		    parse_timers(
  		      TimerRefs, NewTimerTypes, NewCancelTimers, TimeoutsR,
  		      NewSeen, [TimeoutEvent|TimeoutEvents]);
  		true ->
  		    %% (Re)start the timer
  		    TimerRef =
  			erlang:start_timer(Time, self(), TimerMsg),
  		    case TimerTypes of
  			#{TimerType := OldTimerRef} ->
  			    %% Cancel the running timer
  			    cancel_timer(OldTimerRef),
  			    %% Insert the new timer into
  			    %% both TimerRefs and TimerTypes
  			    parse_timers(
  			      TimerRefs#{TimerRef => TimerType},
  			      TimerTypes#{TimerType => TimerRef},
  			      CancelTimers + 1,
  			      TimeoutsR, NewSeen, TimeoutEvents);
  			#{} ->
  			    parse_timers(
  			      TimerRefs#{TimerRef => TimerType},
  			      TimerTypes#{TimerType => TimerRef},
  			      CancelTimers,
  			      TimeoutsR, NewSeen, TimeoutEvents)
  		    end
  	    end
      end.
  
  