Skip to content

Commit

Permalink
handle specific windows network exceptions when waiting for port to o…
Browse files Browse the repository at this point in the history
…pen.

Seems like the servant-client does not handle IO / Network exceptions
that can occur on Windows correctly. So this is a first work-around
that. We may want to move this logic to our Jörmungandr http-client
itself to actually properly handle this type of failure on every
endpoint.
  • Loading branch information
KtorZ committed Nov 13, 2019
1 parent f322d36 commit f0aad4a
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/core/src/Cardano/Wallet/Network/Ports.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import Control.Monad.IO.Class
( liftIO )
import Control.Retry
( RetryPolicyM, retrying )
import Data.List
( isSubsequenceOf )
import Data.Streaming.Network
( bindRandomPortTCP )
import Data.Word
Expand All @@ -52,7 +54,6 @@ import Network.Socket
import UnliftIO.Exception
( bracket, throwIO, try )


-- | Wait until a TCP port is open to connections according to a given retry
-- policy. Throws an exception if the time out is reached.
waitForPort :: RetryPolicyM IO -> PortNumber -> IO Bool
Expand Down Expand Up @@ -89,10 +90,19 @@ isPortOpen sockAddr = do
res <- try $ connect sock sockAddr
case res of
Right () -> return True
Left e ->
if (Errno <$> ioe_errno e) == Just eCONNREFUSED
then return False
else throwIO e
Left e
| (Errno <$> ioe_errno e) == Just eCONNREFUSED -> pure False
| any (`isSubsequenceOf` show e) windowsNetworkError -> pure False
| otherwise -> throwIO e
where
windowsNetworkError :: [String]
windowsNetworkError =
[ "WSAECONNREFUSED" -- Connection refused
, "WSAEADDRNOTAVAIL" -- Cannot assign requested address
, "WSAETIMEDOUT" -- Connection timed out
, "WSAEHOSTUNREACH" -- Host is unreachable
, "WSAEHOSTDOWN" -- Host is down
]

-- | Creates a `SockAttr` from host IP and port number.
--
Expand Down

0 comments on commit f0aad4a

Please sign in to comment.