diff --git a/src/couch_replicator_httpc.erl b/src/couch_replicator_httpc.erl index 668b218..266b922 100644 --- a/src/couch_replicator_httpc.erl +++ b/src/couch_replicator_httpc.erl @@ -128,7 +128,7 @@ process_response({error, connection_closing}, Worker, HttpDb, Params, _Cb) -> stop_and_release_worker(HttpDb#httpdb.httpc_pool, Worker), throw({retry, HttpDb, Params}); -process_response({error, {'EXIT',{normal,_}}}, _Worker, HttpDb, Params, _Cb) -> +process_response({error, req_timedout}, _Worker, HttpDb, Params, _Cb) -> % ibrowse worker terminated because remote peer closed the socket % -> not an error throw({retry, HttpDb, Params}); @@ -222,16 +222,12 @@ clean_mailbox(_ReqId, 0) -> clean_mailbox({ibrowse_req_id, ReqId}, Count) when Count > 0 -> case get(?STREAM_STATUS) of {streaming, Worker} -> - ibrowse:stream_next(ReqId), - receive - {ibrowse_async_response, ReqId, _} -> - clean_mailbox({ibrowse_req_id, ReqId}, Count - 1); - {ibrowse_async_response_end, ReqId} -> + case is_process_alive(Worker) of + true -> + discard_message(ReqId, Worker, Count); + false -> put(?STREAM_STATUS, ended), ok - after 30000 -> - exit(Worker, {timeout, ibrowse_stream_cleanup}), - exit({timeout, ibrowse_stream_cleanup}) end; Status when Status == init; Status == ended -> receive @@ -248,6 +244,20 @@ clean_mailbox(_, Count) when Count > 0 -> ok. +discard_message(ReqId, Worker, Count) -> + ibrowse:stream_next(ReqId), + receive + {ibrowse_async_response, ReqId, _} -> + clean_mailbox({ibrowse_req_id, ReqId}, Count - 1); + {ibrowse_async_response_end, ReqId} -> + put(?STREAM_STATUS, ended), + ok + after 30000 -> + exit(Worker, {timeout, ibrowse_stream_cleanup}), + exit({timeout, ibrowse_stream_cleanup}) + end. + + maybe_retry(Error, Worker, #httpdb{retries = 0} = HttpDb, Params) -> report_error(Worker, HttpDb, Params, {error, Error});