summaryrefslogtreecommitdiff
path: root/src/couchdb/couch_file.erl
diff options
context:
space:
mode:
authorDamien F. Katz <damien@apache.org>2010-04-02 20:01:11 +0000
committerDamien F. Katz <damien@apache.org>2010-04-02 20:01:11 +0000
commitc1ba70c38adef885ee50687210732c3999951a08 (patch)
treec96c961c0f08cde85d990151937a6aa58b43e294 /src/couchdb/couch_file.erl
parent3a30ebac70a7baef18719357c8b7ff00edbbfa83 (diff)
Deterministic/synchronous shutdown code.
git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@930363 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/couchdb/couch_file.erl')
-rw-r--r--src/couchdb/couch_file.erl19
1 files changed, 15 insertions, 4 deletions
diff --git a/src/couchdb/couch_file.erl b/src/couchdb/couch_file.erl
index 5904260c..4c6928a7 100644
--- a/src/couchdb/couch_file.erl
+++ b/src/couchdb/couch_file.erl
@@ -176,13 +176,21 @@ sync(Fd) ->
gen_server:call(Fd, sync, infinity).
%%----------------------------------------------------------------------
-%% Purpose: Close the file. Is performed asynchronously.
+%% Purpose: Close the file.
%% Returns: ok
%%----------------------------------------------------------------------
close(Fd) ->
- Result = gen_server:cast(Fd, close),
- catch unlink(Fd),
- Result.
+ MRef = erlang:monitor(process, Fd),
+ try
+ catch unlink(Fd),
+ catch exit(Fd, shutdown),
+ receive
+ {'DOWN', MRef, _, _, _} ->
+ ok
+ end
+ after
+ erlang:demonitor(MRef, [flush])
+ end.
% 09 UPGRADE CODE
old_pread(Fd, Pos, Len) ->
@@ -219,6 +227,7 @@ init_status_error(ReturnPid, Ref, Error) ->
% server functions
init({Filepath, Options, ReturnPid, Ref}) ->
+ process_flag(trap_exit, true),
case lists:member(create, Options) of
true ->
filelib:ensure_dir(Filepath),
@@ -432,6 +441,8 @@ handle_cast(close, Fd) ->
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
+handle_info({'EXIT', _, normal}, Fd) ->
+ {noreply, Fd};
handle_info({'EXIT', _, Reason}, Fd) ->
{stop, Reason, Fd}.