anchor.diff:      case should_flush(lists:flatlength([Docs|Buffer])) of
           true ->
               Docs2 = lists:flatten([Docs|Buffer]),
  -            {ok, Errors} = update_docs(Target, Docs2, [], replicated_changes),
  -            dump_update_errors(Errors),
  -            ets:update_counter(Stats, doc_write_failures, length(Errors)),
  -            ets:update_counter(Stats, docs_written, length(Docs2) -
  -                    length(Errors)),
  -            {ok, _, Ctxt} = do_checkpoint(Source, Target, Context, Seq, Stats),
  -            {[], Ctxt};
  +            try update_docs(Target, Docs2, [], replicated_changes) of
  +            {ok, Errors} ->
  +                dump_update_errors(Errors),
  +                ets:update_counter(Stats, doc_write_failures, length(Errors)),
  +                ets:update_counter(Stats, docs_written, length(Docs2) -
  +                        length(Errors)),
  +                {ok, _, Ctxt} = do_checkpoint(Source, Target, Context, Seq, Stats),
  +                {[], Ctxt}
  +            catch
  +            throw:attachment_write_failed ->
  +                ?LOG_ERROR("attachment request failed during write to disk", []),
  +                exit({internal_server_error, replication_link_failure})
  +            end;
           false ->
               {[Docs | Buffer], Context}
       end,
  
positive.code:             %% be the one to actually receive the ibrowse data.
              {ok, fun() -> 
                  Pid ! {self(), gimme_data}, 
                  receive {Pid, Data} -> Data end
              end};
          ResponseCode >= 300, ResponseCode < 400 ->
              % follow the redirect
  
negative.code:         stats = Stats
      } = State,
      
      try update_docs(Target, lists:flatten(Buffer), [], replicated_changes) of
      {ok, Errors} ->
          dump_update_errors(Errors),
          ets:update_counter(Stats, doc_write_failures, length(Errors)),
          ets:update_counter(Stats, docs_written, lists:flatlength(Buffer) -
                  length(Errors))
      catch
      throw:attachment_write_failed ->
          ?LOG_ERROR("attachment request failed during final write", []),
          exit({internal_server_error, replication_link_failure})
      end,
      
      couch_task_status:update("Finishing"),
      
  