Rodrigo Mesquita pushed to branch wip/romes/async-rpc at Glasgow Haskell Compiler / GHC Commits: d9dda4e4 by Rodrigo Mesquita at 2026-11-30T19:29:59+00:00 ext-interp: Don't crash on exception inside exception message When evaluating an expression such as error ("hi " ++ error "bye") the external interpreter would catch the outermost error, and try to serialize the exception message over the wire, only to hit a second error. In short, `toSerializableException` should catch exceptions that may arise when displaying the original exception. However, there's no easy way to recursively catch the exceptions and stream them to the ghc host (with the limit being e.g. `fix error`). So, if we catch an exception while displaying another one, we now just write `EExceptionWhilePrinting`. (Note: the external interpreter didn't handle this before async-rpc either, it just crashed) - - - - - 2 changed files: - libraries/ghci/GHCi/Message/Async.hs - libraries/ghci/GHCi/Run.hs Changes: ===================================== libraries/ghci/GHCi/Message/Async.hs ===================================== @@ -62,6 +62,7 @@ import System.Exit import System.IO import Control.Monad import Control.Concurrent.STM +import Control.DeepSeq (force) import Data.Bifunctor import GHC.Base (State#) import GHC.Exts (RealWorld) @@ -299,7 +300,8 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do let reply p = do - r <- atomically $ replyToReqAsync pipe uq p + p' <- either (fmap Left . toSerializableException) (pure . Right) p + r <- atomically $ replyToReqAsync pipe uq p' putMVar ack () -- TODO: log r if Left{} case r of @@ -333,11 +335,11 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do newtype DecodeAsyncReqException = DecodeAsyncReqException String deriving Show instance Exception DecodeAsyncReqException -replyToReqAsync :: Pipe -> CorrelationId -> Either SomeException Put +replyToReqAsync :: Pipe -> CorrelationId -> Either SerializableException Put -> STM (Maybe (TMVar (Either SomeException ()))) replyToReqAsync pipe uq p = do doneHandling am uq - writePipeAsync pipe (Reply uq (bimap toSerializableException runPut p)) + writePipeAsync pipe (Reply uq (runPut <$> p)) where am = pipeAsyncMngr pipe @@ -434,7 +436,7 @@ closeMailbox pipe@Pipe{pipeAsyncMngr = AsyncManager{..}} (InboxId ii) = atomical writeTVar mailboxQueues (IntMap.delete ii boxes) msgs <- flushTQueue q (`mapM` msgs) $ \(uq,msg) -> do - (msg,) . fmap takeTMVar <$> replyToReqAsync pipe uq (Left $ toException $ ClosedMailbox) + (msg,) . fmap takeTMVar <$> replyToReqAsync pipe uq (Left $ EOtherException $ show ClosedMailbox) data ClosedMailbox = ClosedMailbox deriving Show @@ -595,7 +597,7 @@ drainPipe p unmask = catch loop (void . replyToPendings . toException . PipeRead -- "Specific mailbox" request mq <- getMailbox am ii case mq of - Nothing -> void $ replyToReqAsync p uq (Left $ toException ClosedMailbox) + Nothing -> void $ replyToReqAsync p uq (Left $ EOtherException $ show ClosedMailbox) Just q -> writeTQueue q (uq,bs) Cancel uq -> cancelRequest am uq @@ -758,13 +760,15 @@ data SerializableException = EUserInterrupt | EExitCode ExitCode | EOtherException String + | EExceptionWhilePrinting deriving (Generic, Show) -toSerializableException :: SomeException -> SerializableException +toSerializableException :: SomeException -> IO SerializableException toSerializableException ex - | Just UserInterrupt <- fromException ex = EUserInterrupt - | Just (ec::ExitCode) <- fromException ex = (EExitCode ec) - | otherwise = EOtherException $ + | Just UserInterrupt <- fromException ex = pure EUserInterrupt + | Just (ec::ExitCode) <- fromException ex = pure (EExitCode ec) + | otherwise = handle (\(_ :: SomeException) -> pure EExceptionWhilePrinting) $ + fmap EOtherException $ evaluate $ force $ #if MIN_VERSION_base(4,20,0) -- Exception plus backtrace as seen in `displayExceptionWithInfo` case displayExceptionContext (someExceptionContext ex) of @@ -778,6 +782,8 @@ fromSerializableException :: SerializableException -> SomeException fromSerializableException EUserInterrupt = toException UserInterrupt fromSerializableException (EExitCode c) = toException c fromSerializableException (EOtherException str) = toException (ErrorCall str) +fromSerializableException EExceptionWhilePrinting = + toException (ErrorCall "<<exception thrown while printing an exception>>") instance Binary ExitCode instance Binary SerializableException ===================================== libraries/ghci/GHCi/Run.hs ===================================== @@ -206,7 +206,7 @@ tryEval :: IO a -> IO (EvalResult a) tryEval io = do e <- try io case e of - Left ex -> return (EvalException (toSerializableException ex)) + Left ex -> EvalException <$> toSerializableException ex Right a -> return (EvalSuccess a) -------------------------------------------------------------------------------- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/d9dda4e4a295f8930071454b8ea05ac8... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/d9dda4e4a295f8930071454b8ea05ac8... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help