@@ -11,23 +11,18 @@ import Network.Transport
1111 )
1212import Control.Exception (throwIO )
1313import Control.Concurrent (forkIO )
14- import Control.Concurrent.MVar (newEmptyMVar , putMVar , takeMVar )
1514
16- -- | Fork a new thread, create a new end point on that thread, and run the specified IO operation on that thread.
15+ -- | Create a new end point, fork a new thread, and run the specified IO operation on that thread.
1716--
1817-- Returns the address of the new end point.
1918spawn :: Transport -> (EndPoint -> IO () ) -> IO EndPointAddress
2019spawn transport proc = do
21- addrMVar <- newEmptyMVar
22- forkIO $ do
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
20+ -- `newEndPoint` used to be done in a separate thread, in case it was slow.
21+ -- However, in this case, care must be taken to appropriately handle asynchronous exceptions.
22+ -- Instead, for reliability, we now create the new endpoint in this thread.
23+ mEndPoint <- newEndPoint transport
24+ case mEndPoint of
25+ Left err -> throwIO err
26+ Right endPoint -> do
27+ forkIO $ proc endPoint
28+ return $ address endPoint
0 commit comments