From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by sympa.inria.fr (Postfix) with ESMTPS id 033267ED25 for ; Mon, 21 May 2012 15:31:51 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvgCAL1Cuk/U436rlGdsb2JhbAA6CoMdghCuVCIBAQEBCQsJCRQDJIIVAQEFIwRSEAsOCgICJgICVwYTCYgJB6xJkk2BJolfDwqEIIEUA40niQOEQI0MgV0 X-IronPort-AV: E=Sophos;i="4.75,631,1330902000"; d="scan'208";a="144502565" Received: from moutng.kundenserver.de ([212.227.126.171]) by mail4-smtp-sop.national.inria.fr with ESMTP; 21 May 2012 15:31:51 +0200 Received: from office1.lan.sumadev.de (dslb-084-059-076-247.pools.arcor-ip.net [84.59.76.247]) by mrelayeu.kundenserver.de (node=mrbap3) with ESMTP (Nemesis) id 0Lo18K-1RqpND3Xk8-00fw5m; Mon, 21 May 2012 15:31:51 +0200 Received: from [192.168.5.106] (dslb-084-059-076-247.pools.arcor-ip.net [84.59.76.247]) by office1.lan.sumadev.de (Postfix) with ESMTPSA id 8A413C00D0; Mon, 21 May 2012 15:31:47 +0200 (CEST) From: Gerd Stolpmann To: Philippe Wang Cc: Lauri Alanko , caml-list@inria.fr In-Reply-To: References: <20120521142336.79901oeoxunjuoc8.lealanko@webmail.helsinki.fi> <1337601452.19263.0@samsung> Content-Type: text/plain; charset="UTF-8" Date: Mon, 21 May 2012 15:31:47 +0200 Message-ID: <1337607107.25112.77.camel@thinkpad> Mime-Version: 1.0 X-Mailer: Evolution 2.28.3 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:d66jbIGe86SS6GENVomadJvgiTyL4kR05Y3P4m6VUbu EkJO5m3R3EgLHnKI9k28ECD0DRuaLSw6CvX442cHbP4296Sj4C SCGBTACEq5No4tXeuYHX3fIR3T/GoIBCKvLNmjVR1+flJJp5AD WPi2DZ05TXD6ZyzZsHU4X5pC/1qC1b8v69GpvOlCABjJw40nbe OjO+WICzcVYUi9q7TTPJGsWAydutc+VtaNEf18U/01bb+1RO5n 73fzZbhP0PezaQ4Yja/Zfu+UevVeA9CCPN+/BbXVUNLPA4ejy0 /gJinrSnM15T3saRPAUOBY3CfcQwbgYUkQZrS3iJJg//hq1/lO DBYau9K5UwXfDYL2I0SugBdnS6J2hXGorP027CEuU Subject: Re: [Caml-list] Channels not closed on gc? Am Montag, den 21.05.2012, 14:53 +0200 schrieb Philippe Wang: > On Mon, May 21, 2012 at 1:57 PM, Gerd Stolpmann 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. ------------------------------------------------------------