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
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, 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 @@
     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 @@
     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(close, _From, Stream) ->
     #stream{
         fd = Fd,
+        opener_monitor = MonRef,
         written_len = WrittenLen,
         written_pointers = Written,
         buffer_list = Buffer,
@@ -288,6 +291,7 @@
         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 @@
 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}.
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 @@
                 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 @@
     ?_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, []),