Skip to content

Commit

Permalink
bind: syntax & style tweaks as per review
Browse files Browse the repository at this point in the history
  • Loading branch information
infinity0 committed May 25, 2020
1 parent b7d2c4e commit f27b87c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
26 changes: 12 additions & 14 deletions Network/Socket/SockAddr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,18 @@ bind s a = case a of
-- domain sockets, inspired by https://stackoverflow.com/a/13719866
res <- try (G.bind s a)
case res of
Right () -> pure ()
Left e -> if not (isAlreadyInUseError (e :: IOException))
then throwIO e
else do
-- socket might be in use, try to connect
res2 <- try (G.connect s a)
case res2 of
Right () -> close s >> throwIO e
Left e2 -> if not (isDoesNotExistError (e2 :: IOException))
then throwIO e
else do
-- socket not actually in use, remove it and retry bind
removeFile p
G.bind s a
Right () -> return ()
Left e | not (isAlreadyInUseError e) -> throwIO (e :: IOException)
Left e | otherwise -> do
-- socket might be in use, try to connect
res2 <- try (G.connect s a)
case res2 of
Right () -> close s >> throwIO e
Left e2 | not (isDoesNotExistError e2) -> throwIO (e2 :: IOException)
_ -> do
-- socket not actually in use, remove it and retry bind
removeFile p
G.bind s a
_ -> G.bind s a

-- | Accept a connection. The socket must be bound to an address and
Expand Down
3 changes: 2 additions & 1 deletion tests/Network/SocketSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ spec = do
sock1 <- socket AF_UNIX Stream defaultProtocol
tryIOError (bind sock1 addr) >>= \o -> case o of
Right () -> error "bind should have failed but succeeded"
Left e -> if isAlreadyInUseError e then pure () else ioError e
Left e | not (isAlreadyInUseError e) -> ioError e
_ -> return ()

close sock0

Expand Down

0 comments on commit f27b87c

Please sign in to comment.