anchor.diff:          couch_log:notice("Delaying replication `~s` start by ~p seconds.",
               [pp_rep_id(RepId), DelaySecs]),
           Pid = spawn_link(?MODULE, start_replication, [Rep, DelaySecs]),
  -        State#state{rep_start_pids = [Pid | State#state.rep_start_pids]};
  +        State#state{
  +            rep_start_pids = [{rep_start, Pid} | State#state.rep_start_pids]
  +        };
       #rep_state{rep = #rep{doc_id = DocId}} ->
           State;
       #rep_state{starting = false, rep = #rep{db_name = DbName, doc_id = OtherDocId}} ->
  
positive.code:     } = State,
      stop_all_replications(),
      lists:foreach(
          fun(Pid) ->
              catch unlink(Pid),
              catch exit(Pid, stop)
          end,
          [ScanPid | StartPids]),
      true = ets:delete(?REP_TO_STATE),
      true = ets:delete(?DOC_TO_REP),
      true = ets:delete(?DB_TO_SEQ),
  
negative.code:     {stop, {error, {unexpected_call, Msg}}, State}.
  
  handle_cast({resume_scan, DbName}, State) ->
      Pids = State#state.rep_start_pids,
      NewPids = case lists:keyfind(DbName, 1, Pids) of
          {DbName, _Pid} ->
              Entry = case ets:lookup(?DB_TO_SEQ, DbName) of
                  [] ->
                      {DbName, 0, true};
                  [{DbName, EndSeq, _Rescan}] ->
                      {DbName, EndSeq, true}
              end,
              true = ets:insert(?DB_TO_SEQ, Entry),
              Pids;
          false ->
              Since = case ets:lookup(?DB_TO_SEQ, DbName) of
                  [] -> 0;
                  [{DbName, EndSeq, _Rescan}] -> EndSeq
              end,
              true = ets:insert(?DB_TO_SEQ, {DbName, Since, false}),
              ensure_rep_ddoc_exists(DbName),
              Pid = start_changes_reader(DbName, Since),
              couch_log:debug("Scanning ~s from update_seq ~p", [DbName, Since]),
              [{DbName, Pid} | Pids]
      end,
      {noreply, State#state{rep_start_pids = NewPids}};
  
  handle_cast({set_max_retries, MaxRetries}, State) ->
      {noreply, State#state{max_retries = MaxRetries}};
  