[Haskell-cafe] detecting socket closure in haskell

Tim Docker timd at macquarie.com.au
Thu Dec 4 18:34:25 EST 2008

This is a haskell + networking question...
I have a multi threaded haskell server, which accepts client
connections, processes their requests, and returns results. It currently
works as desired, except where a client drops a connection whilst the
server is processing a request. In this circumstance, the server
currently doesn't notice that the client has gone until it finishes the
processing and attempt to read the next request. I'd like to change it
so that the request is aborted as soon as the client disconnects.
One way of doing this would would be to maintain a separate thread that
is always reading from the client, and buffers until the main thread
needs the information. Whilst this would detect the remote close, it
also would potentially consume large amounts of memory to maintain this
Hence I seem to need a means of detecting that a socket has been closed
remotely, without actually reading from it. . Does anyone know how to do
this? One reference I've found is this:
Apparently recv() with appropriate flags can detect this - though I'm
not convinced that the code as shown on that page doesn't busy wait when
there is unread data from the client.

Any tips or pointers? Perhaps what I'm trying to do is not currently
possible in haskell, without using the FFI (which would be ok). Or
perhaps it's not possible in linux at all.



More information about the Haskell-Cafe mailing list