couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikewall...@apache.org
Subject couch commit: updated refs/heads/master to f02a101
Date Wed, 29 Oct 2014 23:21:27 GMT
Repository: couchdb-couch
Updated Branches:
  refs/heads/master 40c5c85d3 -> f02a1013d


Make couch_stream monitor the stream opener

This commit fixes a process leak which would happen when a process
opened a couch_stream and exited with reason normal before closing
the stream.

This patch makes the couch_stream gen_server monitor the process
which opens the stream. When that monitor sends an exit signal
the couch_stream gen_server is stopped and the process dies.

Closes COUCHDB-2365


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/f02a1013
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/f02a1013
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/f02a1013

Branch: refs/heads/master
Commit: f02a1013dc61f011ccb58f1fddb689a93795af98
Parents: 40c5c85
Author: Mike Wallace <mikewallace@apache.org>
Authored: Tue Oct 7 23:45:11 2014 +0100
Committer: Mike Wallace <mikewallace@apache.org>
Committed: Wed Oct 29 23:12:27 2014 +0000

----------------------------------------------------------------------
 src/couch_stream.erl        | 10 ++++++++--
 test/couch_stream_tests.erl | 18 +++++++++++++++++-
 2 files changed, 25 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/f02a1013/src/couch_stream.erl
----------------------------------------------------------------------
diff --git a/src/couch_stream.erl b/src/couch_stream.erl
index e71542c..6877f0f 100644
--- a/src/couch_stream.erl
+++ b/src/couch_stream.erl
@@ -29,6 +29,7 @@
 
 -record(stream,
     {fd = 0,
+    opener_monitor,
     written_pointers=[],
     buffer_list = [],
     buffer_len = 0,
@@ -50,7 +51,7 @@ open(Fd) ->
     open(Fd, []).
 
 open(Fd, Options) ->
-    gen_server:start_link(couch_stream, {Fd, Options}, []).
+    gen_server:start_link(couch_stream, {Fd, self(), Options}, []).
 
 close(Pid) ->
     gen_server:call(Pid, close, infinity).
@@ -197,7 +198,7 @@ write(Pid, Bin) ->
     gen_server:call(Pid, {write, Bin}, infinity).
 
 
-init({Fd, Options}) ->
+init({Fd, OpenerPid, Options}) ->
     {EncodingFun, EndEncodingFun} =
     case couch_util:get_value(encoding, Options, identity) of
     identity ->
@@ -207,6 +208,7 @@ init({Fd, Options}) ->
     end,
     {ok, #stream{
             fd=Fd,
+            opener_monitor=erlang:monitor(process, OpenerPid),
             md5=couch_util:md5_init(),
             identity_md5=couch_util:md5_init(),
             encoding_fun=EncodingFun,
@@ -266,6 +268,7 @@ handle_call({write, Bin}, _From, Stream) ->
 handle_call(close, _From, Stream) ->
     #stream{
         fd = Fd,
+        opener_monitor = MonRef,
         written_len = WrittenLen,
         written_pointers = Written,
         buffer_list = Buffer,
@@ -288,6 +291,7 @@ handle_call(close, _From, Stream) ->
         StreamLen = WrittenLen + iolist_size(WriteBin2),
         {StreamInfo, StreamLen, IdenLen, Md5Final, IdenMd5Final}
     end,
+    erlang:demonitor(MonRef),
     {stop, normal, Result, Stream}.
 
 handle_cast(_Msg, State) ->
@@ -296,5 +300,7 @@ handle_cast(_Msg, State) ->
 code_change(_OldVsn, State, _Extra) ->
     {ok, State}.
 
+handle_info({'DOWN', Ref, _, _, _}, #stream{opener_monitor=Ref} = State) ->
+    {stop, normal, State};
 handle_info(_Info, State) ->
     {noreply, State}.

http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/f02a1013/test/couch_stream_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_stream_tests.erl b/test/couch_stream_tests.erl
index 3e84ca0..e12e714 100644
--- a/test/couch_stream_tests.erl
+++ b/test/couch_stream_tests.erl
@@ -38,7 +38,8 @@ stream_test_() ->
                 fun should_return_stream_size_on_close/1,
                 fun should_return_valid_pointers/1,
                 fun should_recall_last_pointer_position/1,
-                fun should_stream_more_with_4K_chunk_size/1
+                fun should_stream_more_with_4K_chunk_size/1,
+                fun should_stop_on_normal_exit_of_stream_opener/1
             ]
         }
     }.
@@ -94,6 +95,21 @@ should_stream_more_with_4K_chunk_size({Fd, _}) ->
     ?_assertMatch({[{0, 4100}, {4106, 1020}], 5120, _, _, _},
                   couch_stream:close(Stream)).
 
+should_stop_on_normal_exit_of_stream_opener({Fd, _}) ->
+    RunnerPid = self(),
+    OpenerPid = spawn(
+        fun() ->
+            {ok, StreamPid} = couch_stream:open(Fd),
+            RunnerPid ! {pid, StreamPid}
+        end),
+    StreamPid = receive
+        {pid, StreamPid0} -> StreamPid0
+    end,
+    % Confirm the validity of the test by verifying the stream opener has died
+    ?_assertNot(is_process_alive(OpenerPid)),
+    % Verify the stream itself has also died
+    ?_assertNot(is_process_alive(StreamPid)).
+
 
 read_all(Fd, PosList) ->
     Data = couch_stream:foldl(Fd, PosList, fun(Bin, Acc) -> [Bin, Acc] end, []),


Mime
View raw message