[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|_] =
list_to_pid("<"++PidS++">") end.

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

There should be a better way.

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