| ... |
... |
@@ -62,6 +62,7 @@ import System.Exit |
|
62
|
62
|
import System.IO
|
|
63
|
63
|
import Control.Monad
|
|
64
|
64
|
import Control.Concurrent.STM
|
|
|
65
|
+import Control.DeepSeq (force)
|
|
65
|
66
|
import Data.Bifunctor
|
|
66
|
67
|
import GHC.Base (State#)
|
|
67
|
68
|
import GHC.Exts (RealWorld)
|
| ... |
... |
@@ -299,7 +300,8 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do |
|
299
|
300
|
|
|
300
|
301
|
let
|
|
301
|
302
|
reply p = do
|
|
302
|
|
- r <- atomically $ replyToReqAsync pipe uq p
|
|
|
303
|
+ p' <- either (fmap Left . toSerializableException) (pure . Right) p
|
|
|
304
|
+ r <- atomically $ replyToReqAsync pipe uq p'
|
|
303
|
305
|
putMVar ack ()
|
|
304
|
306
|
-- TODO: log r if Left{}
|
|
305
|
307
|
case r of
|
| ... |
... |
@@ -333,11 +335,11 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do |
|
333
|
335
|
newtype DecodeAsyncReqException = DecodeAsyncReqException String deriving Show
|
|
334
|
336
|
instance Exception DecodeAsyncReqException
|
|
335
|
337
|
|
|
336
|
|
-replyToReqAsync :: Pipe -> CorrelationId -> Either SomeException Put
|
|
|
338
|
+replyToReqAsync :: Pipe -> CorrelationId -> Either SerializableException Put
|
|
337
|
339
|
-> STM (Maybe (TMVar (Either SomeException ())))
|
|
338
|
340
|
replyToReqAsync pipe uq p = do
|
|
339
|
341
|
doneHandling am uq
|
|
340
|
|
- writePipeAsync pipe (Reply uq (bimap toSerializableException runPut p))
|
|
|
342
|
+ writePipeAsync pipe (Reply uq (runPut <$> p))
|
|
341
|
343
|
where
|
|
342
|
344
|
am = pipeAsyncMngr pipe
|
|
343
|
345
|
|
| ... |
... |
@@ -434,7 +436,7 @@ closeMailbox pipe@Pipe{pipeAsyncMngr = AsyncManager{..}} (InboxId ii) = atomical |
|
434
|
436
|
writeTVar mailboxQueues (IntMap.delete ii boxes)
|
|
435
|
437
|
msgs <- flushTQueue q
|
|
436
|
438
|
(`mapM` msgs) $ \(uq,msg) -> do
|
|
437
|
|
- (msg,) . fmap takeTMVar <$> replyToReqAsync pipe uq (Left $ toException $ ClosedMailbox)
|
|
|
439
|
+ (msg,) . fmap takeTMVar <$> replyToReqAsync pipe uq (Left $ EOtherException $ show ClosedMailbox)
|
|
438
|
440
|
|
|
439
|
441
|
|
|
440
|
442
|
data ClosedMailbox = ClosedMailbox deriving Show
|
| ... |
... |
@@ -595,7 +597,7 @@ drainPipe p unmask = catch loop (void . replyToPendings . toException . PipeRead |
|
595
|
597
|
-- "Specific mailbox" request
|
|
596
|
598
|
mq <- getMailbox am ii
|
|
597
|
599
|
case mq of
|
|
598
|
|
- Nothing -> void $ replyToReqAsync p uq (Left $ toException ClosedMailbox)
|
|
|
600
|
+ Nothing -> void $ replyToReqAsync p uq (Left $ EOtherException $ show ClosedMailbox)
|
|
599
|
601
|
Just q -> writeTQueue q (uq,bs)
|
|
600
|
602
|
Cancel uq -> cancelRequest am uq
|
|
601
|
603
|
|
| ... |
... |
@@ -758,13 +760,15 @@ data SerializableException |
|
758
|
760
|
= EUserInterrupt
|
|
759
|
761
|
| EExitCode ExitCode
|
|
760
|
762
|
| EOtherException String
|
|
|
763
|
+ | EExceptionWhilePrinting
|
|
761
|
764
|
deriving (Generic, Show)
|
|
762
|
765
|
|
|
763
|
|
-toSerializableException :: SomeException -> SerializableException
|
|
|
766
|
+toSerializableException :: SomeException -> IO SerializableException
|
|
764
|
767
|
toSerializableException ex
|
|
765
|
|
- | Just UserInterrupt <- fromException ex = EUserInterrupt
|
|
766
|
|
- | Just (ec::ExitCode) <- fromException ex = (EExitCode ec)
|
|
767
|
|
- | otherwise = EOtherException $
|
|
|
768
|
+ | Just UserInterrupt <- fromException ex = pure EUserInterrupt
|
|
|
769
|
+ | Just (ec::ExitCode) <- fromException ex = pure (EExitCode ec)
|
|
|
770
|
+ | otherwise = handle (\(_ :: SomeException) -> pure EExceptionWhilePrinting) $
|
|
|
771
|
+ fmap EOtherException $ evaluate $ force $
|
|
768
|
772
|
#if MIN_VERSION_base(4,20,0)
|
|
769
|
773
|
-- Exception plus backtrace as seen in `displayExceptionWithInfo`
|
|
770
|
774
|
case displayExceptionContext (someExceptionContext ex) of
|
| ... |
... |
@@ -778,6 +782,8 @@ fromSerializableException :: SerializableException -> SomeException |
|
778
|
782
|
fromSerializableException EUserInterrupt = toException UserInterrupt
|
|
779
|
783
|
fromSerializableException (EExitCode c) = toException c
|
|
780
|
784
|
fromSerializableException (EOtherException str) = toException (ErrorCall str)
|
|
|
785
|
+fromSerializableException EExceptionWhilePrinting =
|
|
|
786
|
+ toException (ErrorCall "<<exception thrown while printing an exception>>")
|
|
781
|
787
|
|
|
782
|
788
|
instance Binary ExitCode
|
|
783
|
789
|
instance Binary SerializableException |