Skip to content

Socket close detection #212

@winterland1989

Description

@winterland1989

This is actually a continuation of #169 and pr #211 , I'm starting a new thread because i realized there's one more problem we should get done: how should we detect a Socket close event.

The obvious way is to catch IOError when using read/write. in #211 we take this step further so that read/write on a closed Socket is guaranteed to throw an IOError, no matter who close the socket, which fix a bug of unix itself in some sense.

But what if we want to actively detect if a Socket is closed? let's say we're performing a expensive calculation to respond a waiting client, it's reasonable to periodically check if the Socket is still opened, otherwise we can cancel the calculation, but currently with network we can't do that. it's a interesting problem in other language too, please read this to get a understand of how complex of unix socket.

here's some thought:

  1. we should improve Exception situation in network. but i don't know how we can get backward compatibility if we change the exception type.
  2. we should replace the buggy read/write with new one, which guarantee to throw when Socket was closed, and we should document about throwing behavior clearly. A MVar is cheap comparing actual IO operation.
  3. we should provide exception free version for receiving functions, i.e. recvMaybe :: Socket -> Int -> Maybe ByteString. sadly i don't think we can do much for sending functions: returning a Maybe () looks silly.
  4. Currently isConnected is not ideal for reasons i listed above, we can provide a proper implementation with a new name, but i'm not sure if we can do it under windows.

In #211 , i proposed withConnectedSocket, but this function has a subtle problem: you can't use any operations which read the socket status inside it. the problem is inside withMVar, the MVar is empty, if we use anything which try to takeMVar, we produced a deed lock.

I'm not sure if we sure we should do these in another network-safe package, feel free to discuss about above points : ).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions