Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] file descriptor leaks?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-05-04 (07:47)
From: Basile Starynkevitch local <basile.starynkevitch@i...>
Subject: Re: [Caml-list] file descriptor leaks?
On Mon, May 03, 2004 at 04:32:32PM -0700, Ker Lutyn wrote:

> Assuming f does not close the fd when it's done, [...] is this
> better?
> while true do
>   let fd, _ = Unix.accept sock in
>   let g fd = try f fd; Unix.close fd with e -> Unix.close fd; raise e in
>   ignore (Thread.create g fd)
> done

Yes. First, a file descriptor is mostly heavy in the OS kernel side;
inside a process, an fd is just an integer. To free the fd (kernel)
resource, you *have* to call Unix.close (i.e. to call the close(2)
system call).

Second, there are no implicit call to the close(2) system call inside
the Ocaml system (there used to be, long time ago, implicit close of
channels by the GC. This finalization is gone).

So yes, every file descriptor you got thru accept has to be explicitly
closed. Some higher level functions in the Unix library happen to do

Basile STARYNKEVITCH -- basile dot starynkevitch at inria dot fr
Project - phone +33 1 3963 5197 - mobile 6 8501 2359 --- all opinions are only mine 

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: