[Git][ghc/ghc][wip/romes/async-rpc] 3 commits: generalized handleRemoteCall to allow forking.
Andrea Vezzosi pushed to branch wip/romes/async-rpc at Glasgow Haskell Compiler / GHC Commits: 753abb81 by Andrea Vezzosi at 2026-10-16T16:04:25+02:00 generalized handleRemoteCall to allow forking. - - - - - ed3ec8fc by Andrea Vezzosi at 2026-10-16T16:16:56+02:00 GHCi.Server: handle RunTH and RunModFinalizers in forked thread - - - - - ee5d2db0 by Andrea Vezzosi at 2026-10-16T16:21:54+02:00 wip: GHCi.Message.Async: pipeCloseWrite if pipeWrite encounters IOError. - - - - - 3 changed files: - libraries/ghci/GHCi/Message.hs - libraries/ghci/GHCi/Message/Async.hs - libraries/ghci/GHCi/Server.hs Changes: ===================================== libraries/ghci/GHCi/Message.hs ===================================== @@ -55,7 +55,6 @@ import qualified GHC.Exts.Heap as Heap import GHC.ForeignSrcLang import GHC.Fingerprint import GHC.Conc (pseq, par) -import GHC.IO (IO(..)) import Control.Concurrent import Control.DeepSeq import Control.Exception @@ -665,14 +664,11 @@ remoteCall pipe msg = either throwIO (pure . runGet get) -- | Handle a single 'Message' request on the interpreter and reply to the host handleRemoteCall :: Pipe - -> (Msg -> IO Msg) {-^ Msg hook -} + -> (CorrelationId -> Msg -> (Msg -> IO ()) %1 -> IO ()) {-^ called with action that handles req and writes reply -} -> (forall a. (Show a, Binary a) => Message a -> IO a) -> IO () -handleRemoteCall pipe hook k = - handleAsyncReq pipe getMessage Nothing \writer msg0 -> IO \s0 -> - case hook msg0 |#> s0 of -- unIO for linearity checking - (# s1, Msg msg #) -> case k msg |#> s1 of - (# s2, a #) -> writeReply writer (put a) |#> s2 +handleRemoteCall pipe forkHandler k = + handleAsyncReq pipe getMessage Nothing (\(Msg msg) -> fmap put . k $ msg) forkHandler -- | Send a 'THMessage' request to the host and wait for a reply from the host -- to the interpreter. @@ -690,7 +686,5 @@ remoteTHCall pipe ii msg = either throwIO (pure . runGet get) handleRemoteTHCall :: Pipe -> InboxId -> (forall a. (Show a, Binary a) => THMessage a -> IO a) -> IO () handleRemoteTHCall pipe ii k = - handleAsyncReq pipe getTHMessage (Just ii) \writer (THMsg msg) -> IO \s0 -> - case k msg |#> s0 of -- unIO for linearity checking - (# s1, a #) -> writeReply writer (put a) |#> s1 + handleAsyncReq pipe getTHMessage (Just ii) (\(THMsg msg) -> fmap put . k $ msg) $ \ _ msg hndlr -> hndlr msg ===================================== libraries/ghci/GHCi/Message/Async.hs ===================================== @@ -24,7 +24,7 @@ module GHCi.Message.Async -- ** Consume `reply` exactly once , WriteReply(..) - , writeReply, (|#>) + , writeReply, (|#>), bindIO -- * Serializable exceptions , SerializableException(..) @@ -135,7 +135,11 @@ instance Exception PipeReadClosedException -- and identify whether they are requests or replies. writePipe :: Pipe -> IddMsg -> IO () -writePipe p@Pipe{..} = withLock p . putAll . execPut . put +writePipe p@Pipe{..} msg = withLock p $ do + putAll (execPut (put msg)) `catch` \(e :: IOError) -> do + pipeCloseWrite + -- TODO: do we need a pipeCloseWrite TVar ? + throwIO e -- | Only the 'drainPipe' thread should read from the pipe directly. Other -- functions should wait for the matching reply to be delivered asynchronously. @@ -222,10 +226,9 @@ remoteAsyncReq pipe msg ii = do -- It must be used exactly once (all requests expect a reply, and you can't -- reply more than once). Linear types enforce the handler replies in every -- branch, and if the handler otherwise throws, it is handled by 'handleAsyncReq'. -handleAsyncReq :: Pipe -> Get a -> (Maybe InboxId) -> (WriteReply Put %1 -> a -> IO r) -> IO r -handleAsyncReq pipe getit mii k = mask $ \restore -> do +handleAsyncReq :: Pipe -> Get a -> (Maybe InboxId) -> (a -> IO Put) -> (CorrelationId -> a -> (a -> IO ()) %1 -> IO ()) -> IO () +handleAsyncReq pipe getit mii hndlr k = mask $ \restore -> do let am = pipeAsyncMngr pipe - ack <- newEmptyMVar -- Block waiting for request mbreq <- atomically $ do @@ -243,7 +246,6 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do Just (uq, bs) -> do let reply p = do writePipe pipe (Reply uq (bimap toSerializableException runPut p)) - putMVar ack () -- Run the continuation which must reply exactly once with 'writeReply' case runGetOrFail getit bs of @@ -251,20 +253,11 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do let exc = DecodeAsyncReqException err reply (Left (toException exc)) throwIO exc - Right (_, _, it) -> - restore (k (WriteReply reply) it) - - -- If there's an exception before the reply is sent, reply with an error instead. - `catch` \(e :: SomeException) -> do - - tryTakeMVar ack >>= \case - Just () -> - -- Already replied - throwIO e - Nothing -> do - -- Reply with an error - reply (Left e) - throwIO e + Right (_, _, it) -> k uq it (\ msg -> do + try @SomeException (restore (hndlr msg)) >>= \x -> do + reply x + either throwIO (const $ pure ()) x + ) -- | 'handleSyncReq' failed to decode an incoming request payload using the given @'Get' a@ newtype DecodeAsyncReqException = DecodeAsyncReqException String deriving Show @@ -451,6 +444,11 @@ writeReply (WriteReply r) = \x -> r (Right x) (|#>) :: IO a %1 -> State# RealWorld -> (# State# RealWorld, a #) (|#>) (IO fs) s = fs s +bindIO :: (IO a) -> (a -> IO b) %1 -> IO b +bindIO = \ m f -> IO (\s0 -> + case m |#> s0 of -- unIO for linearity checking + (# s1, x #) -> f x |#> s1) + -------------------------------------------------------------------------------- -- * Serializable Exceptions -------------------------------------------------------------------------------- ===================================== libraries/ghci/GHCi/Server.hs ===================================== @@ -1,4 +1,4 @@ -{-# LANGUAGE LambdaCase, CPP, RankNTypes, RecordWildCards, GADTs, ScopedTypeVariables #-} +{-# LANGUAGE LambdaCase, CPP, RankNTypes, RecordWildCards, GADTs, ScopedTypeVariables, LinearTypes #-} module GHCi.Server ( MessageHook , CustomMessageHandler @@ -31,7 +31,6 @@ import GHCi.Utils import Control.DeepSeq import Control.Exception import Control.Monad -import Control.Concurrent (threadDelay) import Data.Binary import Text.Printf @@ -39,6 +38,15 @@ import System.Environment (getProgName, getArgs) import System.Exit import System.IO +import Control.Concurrent +import Control.Concurrent.STM +import Data.IntMap (IntMap) +import qualified Data.IntMap as IM +import GHCi.Message.Async (bindIO) +import qualified Unsafe.Coerce as Unsafe + +type CorrelationId = Int + type MessageHook = Msg -> IO Msg -- | How to interpret the 'CustomCommand'. @@ -54,22 +62,71 @@ serv :: Bool -> MessageHook -> Pipe -> (forall a .IO a -> IO a) -> IO () serv verbose hook pipe restore = servWithCustom verbose hook pipe restore noCustomHandler + +data ThreadManager = ThreadManager + { managedThreads :: !(TVar (IntMap ThreadId)) + , managerClosed :: !(TVar Bool) + } + +-- | Cancels and Waits for all threads spawned by ThreadManager to terminate before returning. +withThreadManager :: (ThreadManager -> IO a) -> IO a +withThreadManager k = do + managedThreads <- newTVarIO IM.empty + managerClosed <- newTVarIO False + let tm = ThreadManager{..} + k tm `finally` shutdown tm + where + shutdown ThreadManager{..} = do + ts <- atomically $ do + writeTVar managerClosed True + IM.elems <$> readTVar managedThreads + mapM_ killThread ts + +spawn :: ThreadManager -> CorrelationId -> IO () %1 -> IO () +spawn mng i = Unsafe.unsafeCoerce (spawn' mng i) + +spawn' :: ThreadManager -> CorrelationId -> IO () -> IO () +spawn' ThreadManager{..} i action = void $ mask_ $ forkIOWithUnmask $ \unmask -> do + tid <- myThreadId + registered <- atomically $ do + s <- readTVar managerClosed + unless s $ modifyTVar' managedThreads (IM.insert i tid) + pure (not s) + when registered $ + (unmask action) + `finally` atomically (modifyTVar' managedThreads (IM.delete i)) + +-- could be used to cancel a specific handler. +-- cancel :: ThreadManager -> CorrelationId -> IO () +-- cancel ThreadManager{..} i = do +-- ts <- atomically $ do +-- readTVar managedThreads +-- case IM.lookup i ts of +-- Just tid -> killThread tid +-- Nothing -> pure () + servWithCustom :: Bool -> MessageHook -> Pipe - -> (forall a .IO a -> IO a) + -> (forall a. IO a -> IO a) -> CustomMessageHandler -> IO () -servWithCustom verbose hook pipe restore customHandler = newIORef False >>= loop +servWithCustom verbose hook pipe restore customHandler = do + ref <- newIORef False + withThreadManager $ \mngr -> + loop ref mngr `finally` closePipe pipe where - loop doneRef = do + forkHandler mngr uq hndlr msg0@(Msg msg) = do + (do discardCtrlC + when verbose $ trace ("msg: " ++ (show msg))) `bindIO` \ _ -> do + case msg of + RunTH{} -> spawn mngr uq (hndlr msg0) + RunModFinalizers{} -> spawn mngr uq (hndlr msg0) + _ -> hndlr msg0 + loop doneRef mngr = do when verbose $ trace "reading pipe..." - handleRemoteCall pipe hook $ \(msg :: Message a) -> do - - discardCtrlC - - when verbose $ trace ("msg: " ++ (show msg)) + handleRemoteCall pipe (\uq msg act -> hook msg `bindIO` forkHandler mngr uq act) $ \(msg :: Message a) -> do case msg of CustomMessage tag payload -> handleCustom tag payload Shutdown -> writeIORef doneRef True >> reply () @@ -79,8 +136,8 @@ servWithCustom verbose hook pipe restore customHandler = newIORef False >>= loop _other -> run msg >>= reply isDone <- readIORef doneRef - if isDone then closePipe pipe - else loop doneRef + if isDone then pure () + else loop doneRef mngr handleCustom tag payload = do mresp <- customHandler tag payload View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/66d87bcad518937a6d50ff2fe692bd3... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/66d87bcad518937a6d50ff2fe692bd3... 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
participants (1)
-
Andrea Vezzosi (@trac-Saizan)