On Mon, Sep 27, 2010 at 3:34 PM, Adam Kocoloski <[email protected]> wrote:
> Hi Filipe, I'm quite certain that the file_descriptor is closed when
> couch_file dies. I believe what the man page means by that statement is that
> if you don't supply the 'raw' option, Erlang will spawn a process to handle
> IO for the file. That is, couch_file will send a message to another process
> which owns the port, rather than couch_file owning the port itself. Regards,
Right.
Digging into OTP's code (R14B actually, but I guess it didn't change
much from R13), when the option 'raw' is not given, file.erl (
lib/kernel/src/file.erl) will use a process (the gen_server defined in
lib/kernel/src/file_server.erl) to access the file. This server will
handle process EXIT messages and do the necessary cleanups.
When using the 'raw' flag, file.erl will do the file operations
through a port (erts/preloaded/src/prim_file.erl) - this is why I have
a hunch that in this case we need to explicitely close the file, which
will send the 'close' command to the port and close the port. From
prim_file.erl:
close(#file_descriptor{module = ?MODULE, data = {Port, _}}) ->
case drv_command(Port, <<?FILE_CLOSE>>) of
ok ->
drv_close(Port);
Error ->
Error
end;
I'm not 100% sure of course, but anyway, it doesn't hurt to
explicitely close the file.
cheers
>
> Adam
>
> On Sep 25, 2010, at 7:26 AM, [email protected] wrote:
>
>> Author: fdmanana
>> Date: Sat Sep 25 11:26:39 2010
>> New Revision: 1001196
>>
>> URL: http://svn.apache.org/viewvc?rev=1001196&view=rev
>> Log:
>> Explicitely close the file descriptor. It doesn't hurt.
>> Also, since we open the file in 'raw' mode, I suspect the file descriptor is
>> not closed when the couch_file server dies.
>>
>> From erl -man file:
>>
>> "The raw option allows faster access to a file, because no Erlang process is
>> needed to handle the file."
>>
>> For me, no Erlang process handling the file means that it's likely the file
>> is not closed by any other process.
>>
>>
>>
>> Modified:
>> couchdb/trunk/src/couchdb/couch_file.erl
>>
>> Modified: couchdb/trunk/src/couchdb/couch_file.erl
>> URL:
>> http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_file.erl?rev=1001196&r1=1001195&r2=1001196&view=diff
>> ==============================================================================
>> --- couchdb/trunk/src/couchdb/couch_file.erl (original)
>> +++ couchdb/trunk/src/couchdb/couch_file.erl Sat Sep 25 11:26:39 2010
>> @@ -278,8 +278,8 @@ maybe_track_open_os_files(FileOptions) -
>> couch_stats_collector:track_process_count({couchdb, open_os_files})
>> end.
>>
>> -terminate(_Reason, _Fd) ->
>> - ok.
>> +terminate(_Reason, #file{fd = Fd}) ->
>> + ok = file:close(Fd).
>>
>>
>> handle_call({pread_iolist, Pos}, _From, File) ->
>>
>>
>
>
--
Filipe David Manana,
[email protected], [email protected]
"Reasonable men adapt themselves to the world.
Unreasonable men adapt the world to themselves.
That's why all progress depends on unreasonable men."