Rodrigo Mesquita pushed to branch wip/romes/async-rpc at Glasgow Haskell Compiler / GHC Commits: de57cda3 by Rodrigo Mesquita at 2026-10-16T16:07:19+01:00 Revert "generalized handleRemoteCall to allow forking." This reverts commit 753abb8145b3591596e35f1e83307d1dcfae81e0. - - - - - 05b34675 by Rodrigo Mesquita at 2026-10-16T17:21:46+01:00 better forkIO linear api and direct use in loop - - - - - 5 changed files: - libraries/ghci/GHCi/Message.hs - libraries/ghci/GHCi/Message/Async.hs - libraries/ghci/GHCi/Server.hs - + libraries/ghci/GHCi/Server/Threads.hs - libraries/ghci/ghci.cabal.in Changes: ===================================== libraries/ghci/GHCi/Message.hs ===================================== @@ -26,7 +26,7 @@ module GHCi.Message , Pipe(..), mkPipeFromHandles, mkPipeFromContinuations, closePipe , remoteCall, remoteTHCall - , handleRemoteCall, handleRemoteTHCall + , handleRemoteCall, handleRemoteTHCall, writeReply , RemoteProcessException(..), PipeReadClosedException(..) , InboxId(..) @@ -55,6 +55,7 @@ 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 @@ -664,11 +665,13 @@ remoteCall pipe msg = either throwIO (pure . runGet get) -- | Handle a single 'Message' request on the interpreter and reply to the host handleRemoteCall :: Pipe - -> (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 forkHandler k = - handleAsyncReq pipe getMessage Nothing (\(Msg msg) -> fmap put . k $ msg) forkHandler + -> (Msg -> IO Msg) {-^ Msg hook -} + -> (forall a. (Show a, Binary a) => WriteReply a %1 -> Message a -> IO r) + -> IO r +handleRemoteCall pipe hook k = + handleAsyncReq pipe getMessage Nothing \(WriteReply writer) msg0 -> IO \s0 -> + case hook msg0 |#> s0 of -- unIO for linearity checking + (# s1, Msg msg #) -> k (WriteReply \x -> writer (fmap put x)) msg |#> s1 -- | Send a 'THMessage' request to the host and wait for a reply from the host -- to the interpreter. @@ -686,5 +689,7 @@ 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) (\(THMsg msg) -> fmap put . k $ msg) $ \ _ msg hndlr -> hndlr msg + 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 ===================================== libraries/ghci/GHCi/Message/Async.hs ===================================== @@ -23,8 +23,8 @@ module GHCi.Message.Async , IddMsg(..), newAsyncManager -- ** Consume `reply` exactly once - , WriteReply(..) - , writeReply, (|#>), bindIO + , WriteReply(..), writeReply + , runWriteReply, (|#>), (%>>=) -- * Serializable exceptions , SerializableException(..) @@ -226,9 +226,10 @@ 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) -> (a -> IO Put) -> (CorrelationId -> a -> (a -> IO ()) %1 -> IO ()) -> IO () -handleAsyncReq pipe getit mii hndlr k = mask $ \restore -> do +handleAsyncReq :: Pipe -> Get a -> (Maybe InboxId) -> (WriteReply Put %1 -> a -> IO r) -> IO r +handleAsyncReq pipe getit mii k = mask $ \restore -> do let am = pipeAsyncMngr pipe + ack <- newEmptyMVar -- Block waiting for request mbreq <- atomically $ do @@ -246,6 +247,7 @@ handleAsyncReq pipe getit mii hndlr 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 @@ -253,11 +255,20 @@ handleAsyncReq pipe getit mii hndlr k = mask $ \restore -> do let exc = DecodeAsyncReqException err reply (Left (toException exc)) throwIO exc - Right (_, _, it) -> k uq it (\ msg -> do - try @SomeException (restore (hndlr msg)) >>= \x -> do - reply x - either throwIO (const $ pure ()) x - ) + 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 -- | 'handleSyncReq' failed to decode an incoming request payload using the given @'Get' a@ newtype DecodeAsyncReqException = DecodeAsyncReqException String deriving Show @@ -440,14 +451,19 @@ data WriteReply a where writeReply :: WriteReply a %1 -> a -> IO () writeReply (WriteReply r) = \x -> r (Right x) +-- | Write the reply with a value or an exception +runWriteReply :: WriteReply a %1 -> Either SomeException a -> IO () +runWriteReply (WriteReply r) = r + -- | Infix, linear, 'unIO' (|#>) :: 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 -> +(%>>=) :: (IO a) -> (a -> IO b) %1 -> IO b +(%>>=) = \ m f -> IO (\s0 -> case m |#> s0 of -- unIO for linearity checking (# s1, x #) -> f x |#> s1) +infixl 1 %>>= -------------------------------------------------------------------------------- -- * Serializable Exceptions ===================================== libraries/ghci/GHCi/Server.hs ===================================== @@ -39,13 +39,8 @@ 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 +import GHCi.Message.Async (runWriteReply, (%>>=)) +import GHCi.Server.Threads type MessageHook = Msg -> IO Msg @@ -62,49 +57,6 @@ 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 @@ -117,23 +69,23 @@ servWithCustom verbose hook pipe restore customHandler = do withThreadManager $ \mngr -> loop ref mngr `finally` closePipe pipe where - 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 (\uq msg act -> hook msg `bindIO` forkHandler mngr uq act) $ \(msg :: Message a) -> do + + handleRemoteCall pipe hook $ \wr (msg :: Message a) -> + + discardCtrlC %>>= \_ -> + when verbose (trace ("msg: " ++ (show msg))) %>>= \_ -> + case msg of - CustomMessage tag payload -> handleCustom tag payload - Shutdown -> writeIORef doneRef True >> reply () - -- TODO: These two should probably be done off the main thread - RunTH ii st q ty loc -> wrapRunTH ii $ runTH pipe ii st q ty loc - RunModFinalizers ii st qrefs -> wrapRunTH ii $ runModFinalizerRefs pipe ii st qrefs - _other -> run msg >>= reply + CustomMessage tag payload -> handleCustom tag payload %>>= writeReply wr + Shutdown -> writeIORef doneRef True >> + reply () %>>= writeReply wr + RunTH ii st q ty loc -> spawnFinally mngr (runWriteReply wr) + (wrapRunTH ii (runTH pipe ii st q ty loc)) + RunModFinalizers ii st qs -> spawnFinally mngr (runWriteReply wr) + (wrapRunTH ii (runModFinalizerRefs pipe ii st qs)) + _other -> run msg >>= reply %>>= writeReply wr isDone <- readIORef doneRef if isDone then pure () @@ -149,7 +101,7 @@ servWithCustom verbose hook pipe restore customHandler = do reply :: forall a. Show a => a -> IO a reply r = do when verbose $ trace ("writing pipe: " ++ show r) - return r -- the result of the handler gets sent + return r -- the result of the handler is passed to writeReply -- Run some TH code, which may interact with GHC by sending -- THMessage requests, and then finally send RunTHDone followed by a ===================================== libraries/ghci/GHCi/Server/Threads.hs ===================================== @@ -0,0 +1,72 @@ +{-# LANGUAGE LambdaCase, CPP, RankNTypes, RecordWildCards, GADTs, ScopedTypeVariables, LinearTypes, BlockArguments #-} +module GHCi.Server.Threads + ( ThreadManager -- abstract for linearity + , withThreadManager -- scoped, so that we're guaranteed to wait for threads + , spawnFinally + ) where + +import Prelude +import Control.Exception +import Control.Monad + +import Control.Concurrent +import Control.Concurrent.STM +import qualified Data.Set as Set +import qualified Unsafe.Coerce as Unsafe + +data ThreadManager = ThreadManager + { managedThreads :: !(TVar (Set.Set ThreadId)) + , managerClosed :: !(TVar Bool) + } + +-- | Cancels and Waits for all threads spawned by 'ThreadManager' to terminate +-- before returning. +-- +-- This thread manager which cancels then waits for all pending threads allows +-- us to provide a resource-safe linearly typed 'spawnFinally', because +-- 'withThreadManager' guarantees neither the threads nor their exceptions +-- leak. +withThreadManager :: (ThreadManager -> IO a) -> IO a +withThreadManager k = do + managedThreads <- newTVarIO Set.empty + managerClosed <- newTVarIO False + let tm = ThreadManager{..} + k tm `finally` shutdown tm + where + shutdown ThreadManager{..} = do + ts <- atomically $ do + writeTVar managerClosed True + Set.elems <$> readTVar managedThreads + mapM_ killThread ts + +-- | Linear and resource-safe because 'ThreadManager' is guaranteed to wait or +-- cancel all threads. +spawnFinally :: ThreadManager -> (Either SomeException a -> IO ()) %1 -> IO a -> IO () +spawnFinally ThreadManager{..} = unsafeLinear \fin -> \action -> void $ + forkFinally + ( mask $ \unmask -> do + tid <- myThreadId + registered <- atomically $ do + s <- readTVar managerClosed + unless s $ modifyTVar' managedThreads (Set.insert tid) + pure (not s) + if registered + then (unmask action) + `finally` + atomically (modifyTVar' managedThreads (Set.delete tid)) + else throwIO ThreadKilled + ) + ( \ea -> fin ea ) + +unsafeLinear :: (a -> b) %1 -> (a %1 -> b) +unsafeLinear = Unsafe.unsafeCoerce (Unsafe.unsafeCoerce :: (a -> b) -> (a %1 -> b)) + +-- TODO: +-- 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 () ===================================== libraries/ghci/ghci.cabal.in ===================================== @@ -68,6 +68,7 @@ library GHCi.StaticPtrTable GHCi.TH GHCi.Server + GHCi.Server.Threads if !arch(javascript) exposed-modules: View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/ee5d2db04cf26509ae9bf565c73213d... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/ee5d2db04cf26509ae9bf565c73213d... 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