[erlang-questions] who owns an open file

Ulf Wiger (TN/EAB) ulf.wiger@REDACTED
Wed Mar 21 13:36:38 CET 2007


I had reason to debug a system that ran out of file descriptors. It was
easy enough to verify that the list of ports kept growing, and to
extract the new ports in each poll iteration. More difficult -
surprisingly - to figure out who owned the open files.

erlang:port_info(P) will give the pid of the controlling process. In
this case, it's a file_io_server. It will be linked to the process which
spawned it - this is always file_server. But file_server has no info of
who ordered the opening of the file. The file_io_server does, but keeps
it in a state variable, and doesn't respond to e.g. sys:get_status(Pid).

Finally, I wrote the following hacks in the shell:

1> Owner = fun(P) -> {backtrace,Bin} = process_info(P,backtrace), Str =
binary_to_list(Bin), {match,Start,Len} =
regexp:match(Str,"#Port<.*>,.*}},"), [PidS|_] =
string:tokens(string:sub_string(Str,Start+Len),"<>"),
list_to_pid("<"++PidS++">") end.

#Fun<erl_eval.6.56006484>
2> {ok,Fd} = file:open("foo.txt",[write]) <file:open(> . 
{ok,<0.80.0>}
3> Owner(Fd).
<0.70.0>                                                             
4> self().
<0.70.0>
5> Filename = ets:lookup_element(file_io_servers,Fd,2).     
"foo.txt" 


There should be a better way.

BR,
Ulf W
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20070321/11c0f0c9/attachment.htm>


More information about the erlang-questions mailing list