anchor.diff:          stats = Stats
       } = State,
       
  -    {ok, Errors} = update_docs(Target, lists:flatten(Buffer), [], replicated_changes),
  -    dump_update_errors(Errors),
  -    ets:update_counter(Stats, doc_write_failures, length(Errors)),
  -    ets:update_counter(Stats, docs_written, lists:flatlength(Buffer) -
  -            length(Errors)),
  +    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"),
       
  
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:     %% wait for headers to ensure that we have a 200 status code
      %% this is where we follow redirects etc
      Pid ! {self(), gimme_status}, 
      receive
      {'EXIT', Pid, attachment_request_failed} ->
          make_attachment_stub_receiver(Url, Headers, Name, Type, Length, Retries-1);
      {Pid, {status, StreamStatus, StreamHeaders}} -> 
          ?LOG_DEBUG("streaming attachment Status ~p Headers ~p",
              [StreamStatus, StreamHeaders]),
          
  