caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list@inria.fr
Subject: Re: FDs as cutsom blocks instead of int
Date: Sun, 21 Nov 2010 19:34:56 +0100	[thread overview]
Message-ID: <87aal2bk4v.fsf_-_@frosties.localnet> (raw)
In-Reply-To: <slrniegkp0.r67.sylvain@gallu.homelinux.org> (Sylvain Le Gall's message of "Sat, 20 Nov 2010 23:00:48 +0000 (UTC)")

Sylvain Le Gall <sylvain@le-gall.net> writes:

> On 20-11-2010, Goswin von Brederlow <goswin-v-b@web.de> wrote:
>> Sylvain Le Gall <sylvain@le-gall.net> writes:
>>> On 19-11-2010, Goswin von Brederlow <goswin-v-b@web.de> wrote:
>>>> Sylvain Le Gall <sylvain@le-gall.net> writes:
>>>>> On 19-11-2010, Goswin von Brederlow <goswin-v-b@web.de> wrote:
>>>>>> Sylvain Le Gall <sylvain@le-gall.net> writes:
>>>>>>> On 18-11-2010, Goswin von Brederlow <goswin-v-b@web.de> wrote:
>>>
>>> The best example about this: you cannot delete a file that has an FD
>>> still open on it. This makes harder to remove temporary file (and this
>>> piece of code was precisely made to track FD on temporary files, that
>>> let 1000s of unremoved temp file). 
>>>
>>> Regards,
>>> Sylvain Le Gall
>>
>> Which again speaks for my solution. The leaked FD will be closed much
>> faster (before the program terminates) and one can remove the tempfiles
>> while the program is still running.
>>
>
> I think, this totally off-topic. But anyway, when a program create a
> temporary files it needs to remove ASAP, itself. I wouldn't 
> deliver a program with a notice like "sometimes FD leaked, not a
> problem, just remove $TMP/myprogram-*". 
>
> For the little story, the leaked FD (hence the temporary files) was
> 400MB each and it quickly get noticed after a few run (and a full HD).
>
> Regards,
> Sylvain Le Gall

Consider the following code:

(* Make sure everything is finalised at exit *)
let () = at_exit Gc.full_major

let remove_tempfile name _ =
  Unix.unlink name

let fd = Unix.openfile name ...
in
  Gc.finalise (remove_tempfile name) fd
  (* do whatever with the FD *)

This ensures the tempfile is closed and removed once it becomes
unreachable. One doesn't need to catch all exceptions to ensure the file
is removed even on errors. Or need reference counting if the FD is
referenced multiple times.

Currently the above fails because Unix.file_descr (int) isn't heap
allocated.

I'm not saying it is a solves-all-problems solution. I just found it
verry helpfull with other abstract types that needed to be explicitly
closed before they can be discarded.

MfG
        Goswin


  reply	other threads:[~2010-11-21 18:39 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-18 17:40 Looking for stubs for sendmsg/recvmsg Goswin von Brederlow
2010-11-19  8:50 ` [Caml-list] " Jérémie Dimino
2010-11-19  8:56 ` Sylvain Le Gall
2010-11-19 16:06   ` [Caml-list] " Goswin von Brederlow
2010-11-19 16:30     ` Sylvain Le Gall
2010-11-19 18:39       ` [Caml-list] " Goswin von Brederlow
2010-11-19 22:10         ` Sylvain Le Gall
2010-11-20 20:32           ` [Caml-list] " Goswin von Brederlow
2010-11-20 23:00             ` Sylvain Le Gall
2010-11-21 18:34               ` Goswin von Brederlow [this message]
2010-11-21 21:38       ` [Caml-list] " ygrek
2010-11-19 10:27 ` [Caml-list] " Dave Scott
2010-11-19 16:28   ` Goswin von Brederlow

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=87aal2bk4v.fsf_-_@frosties.localnet \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    /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).