-
Notifications
You must be signed in to change notification settings - Fork 201
Description
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:
- we should improve
Exceptionsituation in network. but i don't know how we can get backward compatibility if we change the exception type. - we should replace the buggy
read/writewith new one, which guarantee to throw whenSocketwas closed, and we should document about throwing behavior clearly. AMVaris cheap comparing actual IO operation. - 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 aMaybe ()looks silly. - Currently
isConnectedis 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 : ).