[erlang-questions] Should I use file:close/1 in terminate/2?

Matthias Lang matthias@REDACTED
Sun May 24 08:58:59 CEST 2009


On Thursday, May 21, Sergey Samokhin wrote:

> I have often wanted to ask if it's safe to let the file opened in
> init/1 of gen_server/gen_fsm be closed automatically when a server
> terminates?

Yes, it is, always.

A file is handled through yet another Erlang process. The file process
monitors the process which opened the file. When the opening process
ends, Erlang's monitor mechanism propagates the 'failure' and the file
gets closed.

Illustration:

  1> {ok, F} = file:open(".bashrc", [read]).
  {ok,<0.33.0>}
  2> erlang:process_info(self(), monitored_by).
  {monitored_by,[<0.33.0>]}

(In 'raw' mode, the mechanism is slightly different, but the results
are the same)

Matt



More information about the erlang-questions mailing list