anchor.diff:      modify_handler_pid :: undefined | pid(),
       % a pid of the commit handler process
       commit_handler_pid :: undefined | pid(),
  -    % a reference to the message expected to trigger commit
  +    % a reference to a timer expected to trigger terminate
  +    terminate_timer_ref :: undefined | reference(),
  +    % a reference to a message expected to trigger commit
       commit_msg_ref :: undefined | reference(),
  -    % a reference to the message expected to trigger terminate
  +    % a reference to a message expected to trigger terminate
       terminate_msg_ref :: undefined | reference(),
       commit_delay :: timeout(),
       idle_timeout :: timeout()
  
positive.code: schedule_commit(_Delay, #state{changes = undefined} = State) ->
      State;
  schedule_commit(Delay, #state{commit_msg_ref = undefined} = State) ->
      State#state{commit_msg_ref = schedule_msg(Delay, {commit, Delay})};
  schedule_commit(_Delay, #state{} = State) ->
      State.
  
  %%--------------------------------------------------------------------
  %% @private
  %% @doc
  %% Schedules terminate operation if there are no pending requests, uncommitted
  %% changes and modify/commit handlers running.
  %% @end
  %%--------------------------------------------------------------------
  -spec schedule_terminate(state()) -> state().
  schedule_terminate(#state{
      requests = [],
      changes = undefined,
      modify_handler_pid = undefined,
      commit_handler_pid = undefined,
      idle_timeout = IdleTimeout
  } = State) ->
      State#state{terminate_msg_ref = schedule_msg(IdleTimeout, terminate)};
  schedule_terminate(#state{} = State) ->
      State.
  
  %%--------------------------------------------------------------------
  %% @private
  %% @doc
  %% Wraps message with a reference tag and sends after delay to the calling
  %% process. Returns reference tag.
  %% @end
  %%--------------------------------------------------------------------
  -spec schedule_msg(timeout(), any()) -> undefined | reference().
  schedule_msg(infinity, _Msg) ->
      undefined;
  schedule_msg(Delay, Msg) ->
      Ref = make_ref(),
      erlang:send_after(Delay, self(), {Ref, Msg}),
      Ref.
  
  %%--------------------------------------------------------------------
  %% @private
  
negative.code: handle_modified(Exit, #state{commit_delay = Delay} = State) ->
      ?error("Modify handler of a transaction process terminated abnormally: ~p",
          [Exit]),
      schedule_terminate(schedule_commit(Delay, modify_async(State#state{
          modify_handler_pid = undefined
      }))).
  
  %%--------------------------------------------------------------------
  %% @private
  