Skip to content

Commit fd8b105

Browse files
committed
Merge pull request #9 from takano-akio/ignore-epipe
Server shouldn't crash when the client dies
2 parents 59a7624 + 684defe commit fd8b105

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
Bit Connor <[email protected]>
2+
Takano Akio <[email protected]>

src/Server.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
module Server where
22

3-
import Control.Exception (bracket, finally, tryJust)
3+
import Control.Exception (bracket, finally, handleJust, tryJust)
44
import Control.Monad (guard)
55
import Data.IORef (IORef, newIORef, readIORef, writeIORef)
6+
import GHC.IO.Exception (IOErrorType(ResourceVanished))
67
import Network (PortID(UnixSocket), Socket, accept, listenOn, sClose)
78
import System.Directory (removeFile)
89
import System.Exit (ExitCode(ExitSuccess))
910
import System.IO (Handle, hClose, hFlush, hGetLine, hPutStrLn)
10-
import System.IO.Error (isDoesNotExistError)
11+
import System.IO.Error (ioeGetErrorType, isDoesNotExistError)
1112

1213
import CommandLoop (newCommandLoopState, startCommandLoop)
1314
import Types (ClientDirective(..), Command, ServerDirective(..))
@@ -44,11 +45,14 @@ clientSend :: IORef (Maybe Handle) -> ClientDirective -> IO ()
4445
clientSend currentClient clientDirective = do
4546
mbH <- readIORef currentClient
4647
case mbH of
47-
Just h -> do
48-
-- TODO catch exception
48+
Just h -> ignoreEPipe $ do
4949
hPutStrLn h (show clientDirective)
5050
hFlush h
5151
Nothing -> error "This is impossible"
52+
where
53+
-- EPIPE means that the client is no longer there.
54+
ignoreEPipe = handleJust (guard . isEPipe) (const $ return ())
55+
isEPipe = (==ResourceVanished) . ioeGetErrorType
5256

5357
getNextCommand :: IORef (Maybe Handle) -> Socket -> IO (Maybe (Command, [String]))
5458
getNextCommand currentClient sock = do

0 commit comments

Comments
 (0)