caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gerd Stolpmann <info@gerd-stolpmann.de>
To: Philippe Wang <mail@philippewang.info>
Cc: Lauri Alanko <la@iki.fi>, caml-list@inria.fr
Subject: Re: [Caml-list] Channels not closed on gc?
Date: Mon, 21 May 2012 15:31:47 +0200	[thread overview]
Message-ID: <1337607107.25112.77.camel@thinkpad> (raw)
In-Reply-To: <CAAFfW_qcFEFgtFAcdscsG9wAowzqdxh3u+RVuBFjyA=MtHmqNw@mail.gmail.com>

Am Montag, den 21.05.2012, 14:53 +0200 schrieb Philippe Wang:
> On Mon, May 21, 2012 at 1:57 PM, Gerd Stolpmann <info@gerd-stolpmann.de> wrote:
> > Am 21.05.2012 13:23:36 schrieb(en) Lauri Alanko:
> >
> >> I only recently noticed that ocaml does not close open channels when they
> >> are garbage collected. This is evidently intentional behavior, but it was
> >> quite unexpected.
> >>
> >> To be clear, I do think it's bad style to rely on GC for releasing OS
> >> resources, but that doesn't explain why GC shouldn't do this if the
> >> programmer has failed to explicitly close the channel. And if the intention
> >> were to _enforce_ good style, the channel finaliser would spout out an error
> >> or warning upon detecting that the channel hasn't yet been closed, instead
> >> of just silently leaking file handles like it does currently.
> >>
> >> It is of course trivial to "fix" this by attaching a simple finaliser, but
> >> the fact that this is not done by default makes me suspect that there would
> >> be something fishy with this approach. So, what's the rationale for the
> >> current behavior?
> >
> >
> > It's predictable.
> >
> > Closing a channel is not only about releasing OS resources. Imagine the
> > channel is actually a pipe - closing it means to signal EOF, i.e. it's a way
> > of notifying another program about an event. This should not happen behind
> > one's back.
> >
> > Closing a regular file would in deed be harmless, but there is no generic
> > way to identify such channels (in the OS).
> >
> > Gerd
> >
> >
> >> Thanks,
> >>
> >>
> >> Lauri
> 
> Wouldn't it be a good idea to provide both ways?
> i.e., one that collects automatically, in addition to the current one
> that doesn't.

You can easily do this yourself:

Gc.finalise close_in ch

(if ch is an in_channel; for out_channels use close_out_noerr). We
exploit here that channels track whether they are already closed. If
there is the danger of getting an exception, catch it:

let close_out_catch ch =
  try close_out ch
  with error ->
    prerr_endline("Exception from attempt to close channel: " ^ Printexc.to_string error)

This points, btw, to another problem: Exceptions can no longer be
reasonably handled, but only be printed. You cannot do much in a
finaliser.

Gerd

> -- 
> Philippe Wang
>    mail@philippewang.info
> 

-- 
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany    gerd@gerd-stolpmann.de
Creator of GODI and camlcity.org.
Contact details:        http://www.camlcity.org/contact.html
Company homepage:       http://www.gerd-stolpmann.de
*** Searching for new projects! Need consulting for system
*** programming in Ocaml? Gerd Stolpmann can help you.
------------------------------------------------------------


  reply	other threads:[~2012-05-21 13:31 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-21 11:23 Lauri Alanko
2012-05-21 11:57 ` AW: " Gerd Stolpmann
2012-05-21 12:53   ` Philippe Wang
2012-05-21 13:31     ` Gerd Stolpmann [this message]
2012-05-21 14:18       ` Philippe Wang
2012-05-21 14:48         ` Mehdi Dogguy
2012-05-29 12:08   ` AW: " Goswin von Brederlow
2012-05-29 12:46     ` Gerd Stolpmann
2012-05-29 14:13       ` oliver
2012-05-29 18:39         ` Török Edwin
2012-05-29 18:58           ` Philippe Veber
2012-05-29 12:49     ` Jérémie Dimino

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1337607107.25112.77.camel@thinkpad \
    --to=info@gerd-stolpmann.de \
    --cc=caml-list@inria.fr \
    --cc=la@iki.fi \
    --cc=mail@philippewang.info \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).