-
Notifications
You must be signed in to change notification settings - Fork 201
Description
Viktor ([email protected]) wrote from haskell-cafe:
In older releases of Network, I was initially surprised that
closing a connected TCP socket could throw an exception,
and so I had to take some care with:
bracket (socket ...) -- create a stream socket
(try to do something with) -- catch I/O errors here
(close) -- have to try here too!
to avoding crashing when close fails. The reason close failed,
is that if the caller is the last writer on the socket, then it
is possible that the kernel queued the last write, but got an RST
response from the peer when trying to deliver the data to the
remote destination. This is reported during close(2):
[ECONNRESET] The underlying object was a stream socket that was
shut down by the peer before all pending data was
delivered
The original Network.Socket code converted this into an exception.
That had pros and cons. On the one hand, users naïvely assuming
that "close" can't fail won't find their code unexpectedly blowing
up. On the other hand users relying on the OS to report failure
of the final write will no longer get the delayed exception
(it is a feature that write(2) completes before the data is
acknowledged by the remote peer, otherwise TCP could not stream).
I am vaguely inclined to think that restoring the exception might
be the right thing, but on the other hand, if the last write is
important to deliver, it should perhaps be, and often is, more
properly confirmed at the application layer. So for many
applications the potential of an exception on close is an
annoyance.
One might also note that the documentation never covered the
possibility of such an exception, and the two code samples at
the top of the Network.Socket online docs don't suggest any
need to catch "close" errors. So this is likely a poorly
understood corner case.
With that said, what do you think? Should the Haskell Socket
"close" throw an exception when the native socket library
close returns an error? Or leave confirmation of the last
application data payload to the remote reader?