Are handles closed automatically when they fall out of scope?

Simon Marlow simonmar at microsoft.com
Mon Oct 25 09:56:34 EDT 2004


On 25 October 2004 14:24, John Goerzen wrote:

> On 2004-10-25, Simon Marlow <simonmar at microsoft.com> wrote:
>> On 22 October 2004 21:58, Peter Simons wrote:
>> 
>> On 24 October 2004 23:37, John Goerzen wrote:
>> 
>>> * What happens when one Handle corresponding to a socket is closed,
>>>   but another isn't?
>> 
>> You shouldn't have two Handles on the same socket.  This is an
>> unchecked error.
> 
> This does seem useful, though.  I am actually doing this in my code
> and it works.  One Handle is opened ReadOnly, the other WriteOnly. 
> That way, I can use hGetContents on the reading side in my network
> code. 
> 
> If I tried that with a single Handle opened ReadWrite, then I'd get
> errors about it being closed whenever I'd try to write out some data.
> 
> I wasn't able to find any other good way around it.

Hmmm, you should still be able to *write* to a socket handle that has
had hGetContents applied to it.  In GHC, a ReadWrite handle to a socket
basically consists of a wrapper around two independent Handles, one for
read and one for write, each with its own buffer.

... I just tested it with 6.2.2, and indeed it does work as I expected.
But perhaps there's a bug lurking somewhere?

If you do socketToHandle twice, then the danger is that one of the
Handles will be finalized and close the FD before the other Handle has
finished with it.

In 6.4 you'll be able to use hDuplicate for this, BTW.

Cheers,
	Simon


More information about the Glasgow-haskell-users mailing list