From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id BA08CBBAF for ; Sun, 21 Nov 2010 19:39:02 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ap0KABL26EzZSMDjZmdsb2JhbACUVY4EFQ0LCAgSAx+8JYVLBI1s X-IronPort-AV: E=Sophos;i="4.59,232,1288566000"; d="scan'208";a="89035166" Received: from fmmailgate02.web.de ([217.72.192.227]) by mail1-smtp-roc.national.inria.fr with ESMTP; 21 Nov 2010 19:39:00 +0100 Received: from smtp04.web.de ( [172.20.0.225]) by fmmailgate02.web.de (Postfix) with ESMTP id B2F0D1859C313 for ; Sun, 21 Nov 2010 19:34:59 +0100 (CET) Received: from [78.43.204.177] (helo=frosties.localdomain) by smtp04.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.110 #24) id 1PKEkd-0005TN-00 for caml-list@inria.fr; Sun, 21 Nov 2010 19:34:59 +0100 Received: from mrvn by frosties.localdomain with local (Exim 4.72) (envelope-from ) id 1PKEkb-00046q-L1 for caml-list@inria.fr; Sun, 21 Nov 2010 19:34:57 +0100 From: Goswin von Brederlow To: caml-list@inria.fr Subject: Re: FDs as cutsom blocks instead of int References: <87d3q21qe4.fsf@frosties.localnet> <87ipztl2ld.fsf@frosties.localnet> <8739qx88e9.fsf@frosties.localnet> <87lj4n3fdo.fsf@frosties.localnet> Date: Sun, 21 Nov 2010 19:34:56 +0100 In-Reply-To: (Sylvain Le Gall's message of "Sat, 20 Nov 2010 23:00:48 +0000 (UTC)") Message-ID: <87aal2bk4v.fsf_-_@frosties.localnet> User-Agent: Gnus/5.110009 (No Gnus v0.9) XEmacs/21.4.22 (linux, no MULE) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: goswin-v-b@web.de X-Sender: goswin-v-b@web.de X-Provags-ID: V01U2FsdGVkX18erWe5zIzyGEkLMd5D0sOLR1UlKyTkxEp0uqkv fIwXX3S853ymef7rE4L3xegnU40jZlh57y6XjqFkzdAkGM5KUK MCyy0DdDY= X-Spam: no; 0.00; le-gall:01 le-gall:01 tempfile:01 tempfile:01 mfg:98 terminates:01 wrote:01 wrote:01 unix:01 unix:01 abstract:01 heap:01 exceptions:01 precisely:01 int:01 Sylvain Le Gall writes: > On 20-11-2010, Goswin von Brederlow wrote: >> Sylvain Le Gall writes: >>> On 19-11-2010, Goswin von Brederlow wrote: >>>> Sylvain Le Gall writes: >>>>> On 19-11-2010, Goswin von Brederlow wrote: >>>>>> Sylvain Le Gall writes: >>>>>>> On 18-11-2010, Goswin von Brederlow 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