Skip to content

Commit 61bbd12

Browse files
committed
Create new endpoints in the current thread, avoiding the possibility of asynchronous exceptions (fixes #42)
1 parent 921f75f commit 61bbd12

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

packages/network-transport/ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Unreleased
2+
3+
* Prevent asynchronous exceptions from `Network.Transport.Util.spawn` (#42).
4+
15
2024-09-03 Laurent P. René de Cotret <laurent.decotret@outlook.com> 0.5.8
26

37
* Bumped dependency bounds to support GHC 8.10.7 - GHC 9.10.1

packages/network-transport/src/Network/Transport/Util.hs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,18 @@ import Network.Transport
1111
)
1212
import Control.Exception (throwIO)
1313
import 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.
1918
spawn :: Transport -> (EndPoint -> IO ()) -> IO EndPointAddress
2019
spawn 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

Comments
 (0)