Skip to content

Commit

Permalink
Create new endpoints in the current thread, avoiding the possibility …
Browse files Browse the repository at this point in the history
…of asynchronous exceptions (fixes #42)
  • Loading branch information
LaurentRDC committed Sep 3, 2024
1 parent 921f75f commit 61bbd12
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
4 changes: 4 additions & 0 deletions packages/network-transport/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Unreleased

* Prevent asynchronous exceptions from `Network.Transport.Util.spawn` (#42).

2024-09-03 Laurent P. René de Cotret <[email protected]> 0.5.8

* Bumped dependency bounds to support GHC 8.10.7 - GHC 9.10.1
Expand Down
25 changes: 10 additions & 15 deletions packages/network-transport/src/Network/Transport/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,18 @@ import Network.Transport
)
import Control.Exception (throwIO)
import Control.Concurrent (forkIO)
import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar)

-- | Fork a new thread, create a new end point on that thread, and run the specified IO operation on that thread.
-- | Create a new end point, fork a new thread, and run the specified IO operation on that thread.
--
-- Returns the address of the new end point.
spawn :: Transport -> (EndPoint -> IO ()) -> IO EndPointAddress
spawn transport proc = do
addrMVar <- newEmptyMVar
forkIO $ do
mEndPoint <- newEndPoint transport
case mEndPoint of
Left err ->
putMVar addrMVar (Left err)
Right endPoint -> do
putMVar addrMVar (Right (address endPoint))
proc endPoint
mAddr <- takeMVar addrMVar
case mAddr of
Left err -> throwIO err
Right addr -> return addr
-- `newEndPoint` used to be done in a separate thread, in case it was slow.
-- However, in this case, care must be taken to appropriately handle asynchronous exceptions.
-- Instead, for reliability, we now create the new endpoint in this thread.
mEndPoint <- newEndPoint transport
case mEndPoint of
Left err -> throwIO err
Right endPoint -> do
forkIO $ proc endPoint
return $ address endPoint

0 comments on commit 61bbd12

Please sign in to comment.