@@ -8,7 +8,8 @@ import Network.Transport
88import Control.Concurrent (forkIO, ThreadId, killThread, myThreadId)
99import Control.Concurrent.Chan
1010import Control.Concurrent.MVar
11−import Control.Exception (SomeException, throwTo, catch)
11+import Control.Exception (SomeException, IOException, AsyncException(ThreadKilled),
12+ fromException, throwTo, throw, catch, handle)
1213import Control.Monad (forever, forM_)
1314import Data.ByteString.Lazy.Char8 (ByteString)
1415import Data.IntMap (IntMap)
@@ -23,6 +24,7 @@ import Safe
2324import System.IO (stderr, hPutStrLn)
2425
2526import qualified Data.Binary as B
27+import qualified Data.ByteString.Char8 as BSS
2628import qualified Data.ByteString.Lazy.Char8 as BS
2729import qualified Data.IntMap as IntMap
2830import qualified Network.Socket as N
@@ -207,7 +209,9 @@ procMessages chans chanId chan sock = do
207209-- indicates success. This hasn't been implemented, since a Transport `receive`
208210-- represents an atomic receipt of a message.
209211recvExact :: Socket -> Int64 -> IO [ByteString]
210−recvExact sock n = go [] sock n
212+recvExact sock n =
213+ interceptAllExn "recvExact" $
214+ go [] sock n
211215 where
212216 go :: [ByteString] -> Socket -> Int64 -> IO [ByteString]
213217 go bss _ 0 = return (reverse bss)
@@ -218,6 +222,10 @@ recvExact sock n = go [] sock n
218222 else go (bs:bss) sock (n - BS.length bs)
219223
220224
225+interceptAllExn msg =
226+ Control.Exception.handle $ \ e -> do
227+ BSS.hPutStrLn stderr $ BSS.pack$ "Exception inside "++msg++": "++show e
228+ throw (e :: SomeException)
221229
222230forkWithExceptions :: (IO () -> IO ThreadId) -> String -> IO () -> IO ThreadId
223231forkWithExceptions forkit descr action = do
@@ -225,7 +233,12 @@ forkWithExceptions forkit descr action = do
225233 forkit $
226234 Control.Exception.catch action
227235 (\ e -> do
228− hPutStrLn stderr $ "Exception inside child thread "++descr++": "++show e
229− throwTo parent (e::SomeException)
236+ case fromException e of
237+ Just ThreadKilled ->
238+-- BSS.hPutStrLn stderr $ BSS.pack$ "Note: Child thread killed: "++descr
239+ return ()
240+ _ -> do
241+ BSS.hPutStrLn stderr $ BSS.pack$ "Exception inside child thread "++descr++": "++show e
242+ throwTo parent (e::SomeException)
230243 )
231244