anchor.diff:                      receive
                           {From, continue} -> 
                               %% normal case
  -                            attachment_loop(ReqId);
  +                            attachment_loop(ReqId, Conn);
                           {From, fail} ->
                               %% error, failure code
                               ?LOG_ERROR(
                                   "streaming attachment failed with status ~p",
                                   [Status]),
  +                            catch ibrowse:stop_worker_process(Conn),
                               exit(attachment_request_failed);
                           {From, stop_ok} ->
                               %% stop looping, controller will start a new loop
  +                            catch ibrowse:stop_worker_process(Conn),
                               stop_ok
                       end
               end,
  -            attachment_loop(ReqId);
  +            attachment_loop(ReqId, Conn);
           {ibrowse_async_response, ReqId, {chunk_start,_}} ->
  -            attachment_loop(ReqId);
  +            attachment_loop(ReqId, Conn);
           {ibrowse_async_response, ReqId, chunk_end} ->
  -            attachment_loop(ReqId);
  +            attachment_loop(ReqId, Conn);
           {ibrowse_async_response, ReqId, {error, Err}} ->
               ?LOG_ERROR("streaming attachment failed with ~p", [Err]),
  +            catch ibrowse:stop_worker_process(Conn),
               exit(attachment_request_failed);
           {ibrowse_async_response, ReqId, Data} -> 
               receive {From, gimme_data} -> From ! {self(), Data} end,
  -            attachment_loop(ReqId);
  -        {ibrowse_async_response_end, ReqId} -> ok
  +            attachment_loop(ReqId, Conn);
  +        {ibrowse_async_response_end, ReqId} ->
  +            catch ibrowse:stop_worker_process(Conn),
  +            exit(normal)
       end.
   
   attachment_stub_converter(DbS, Id, Rev, {Name, {stub, Type, Length}}) ->
       #http_db{uri=DbUrl, headers=Headers} = DbS,
       {Pos, [RevId|_]} = Rev,
       Url = lists:flatten([DbUrl, url_encode(Id), "/", url_encode(?b2l(Name)),
  -        "?rev=", couch_doc:rev_to_str({Pos,RevId})]),
  +        "?rev=", ?b2l(couch_doc:rev_to_str({Pos,RevId}))]),
       ?LOG_DEBUG("Attachment URL ~p", [Url]),
       {ok, RcvFun} = make_attachment_stub_receiver(Url, Headers, Name, 
           Type, Length),
  
positive.code:     ets:update_counter(State#state.stats, total_revs, RevsCount),
      case State#state.listeners of
      [] ->
          % still waiting for the first listener to sen a request
          {noreply, State#state{current_seq=LastSeq,done=true}};
      _ ->
          {stop, normal, ok, State#state{current_seq=LastSeq}}
  
negative.code:     ets:update_counter(Stats, docs_read, length(Docs)),
      
      %% save them (maybe in a buffer)
      {NewBuffer, NewContext} = 
      case should_flush(lists:flatlength([Docs|Buffer])) of
          true ->
              Docs2 = lists:flatten([Docs|Buffer]),
              {ok, Errors} = update_docs(Target, Docs2, [], replicated_changes),
  