@@ -9,6 +9,7 @@ import Network.Transport
99 , EndPointAddress
1010 , newEndPoint
1111 )
12+import Control.Exception (throwIO)
1213import Control.Concurrent (forkIO)
1314import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar)
1415
@@ -17,9 +18,16 @@ import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar)
1718-- Returns the address of the new end point.
1819spawn :: Transport -> (EndPoint -> IO ()) -> IO EndPointAddress
1920spawn transport proc = do
20− addr <- newEmptyMVar
21+ addrMVar <- newEmptyMVar
2122 forkIO $ do
22− Right endpoint <- newEndPoint transport
23− putMVar addr (address endpoint)
24− proc endpoint
25− takeMVar addr
23+ mEndPoint <- newEndPoint transport
24+ case mEndPoint of
25+ Left err ->
26+ putMVar addrMVar (Left err)
27+ Right endPoint -> do
28+ putMVar addrMVar (Right (address endPoint))
29+ proc endPoint
30+ mAddr <- takeMVar addrMVar
31+ case mAddr of
32+ Left err -> throwIO err
33+ Right addr -> return addr