Rodrigo Mesquita pushed to branch wip/romes/async-rpc at Glasgow Haskell Compiler / GHC Commits: 6f97d0cf by Andrea Vezzosi at 2026-10-20T11:35:34+01:00 GHCi.Server: handle RunTH and RunModFinalizers in forked thread This commit makes the external interpreter able to many interpret template haskell splices in parallel without blocking the main server thread. Simply: fork off a thread to handle a RunTH request. The handler will issue the reverse-requests on the given RunTH mailbox, to keep each RunTH isolated from the others. `handleRemoteCall` was generalised slightly by allowing `WriteReply` to be used linearly in the handler directly, so that the caller of `handleRemoteCall` may `WriteReply` in the forked thread rather than in the main thread. The only question is how can we guarantee `WriteReply` is used linearly if we want to use it inside a forked thread? We need two guarantees: 1. `WriteReply` is used in the forked thread regardless of whether an exception is thrown to that thread. 2. The forked thread is always awaited for, so that the main program doesn't exit before the thread has a chance to actually use `WriteReply`. We introduced a small `ThreadManager` which provides a `spawnFinally` that provides these two guarantees: 1. is guaranteed by `forkFinally`, which always runs the clean up action exactly once. 2. is guaranteed by `ThreadManager` being abstract and the only exposed function that introduces `ThreadManager` being `withThreadManager`, which always awaits for all spawned threads. Co-authored-by: Rodrigo Mesquita <rodrigo.m.mesquita@gmail.com> - - - - - 42a3626a by Andrea Vezzosi at 2026-10-20T11:43:55+01:00 interp: spawn fillPipe thread, writePipe now safely interruptible TODO - - - - - 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(..) @@ -666,13 +666,12 @@ 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 -} - -> (forall a. (Show a, Binary a) => Message a -> IO a) - -> IO () + -> (forall a. (Show a, Binary a) => WriteReply a %1 -> Message a -> IO r) + -> IO r handleRemoteCall pipe hook k = - handleAsyncReq pipe getMessage Nothing \writer msg0 -> IO \s0 -> + handleAsyncReq pipe getMessage Nothing \(WriteReply 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 + (# 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. ===================================== libraries/ghci/GHCi/Message/Async.hs ===================================== @@ -23,8 +23,8 @@ module GHCi.Message.Async , IddMsg(..), newAsyncManager -- ** Consume `reply` exactly once - , WriteReply(..) - , writeReply, (|#>) + , WriteReply(..), writeReply + , runWriteReply, (|#>), (%>>=) -- * Serializable exceptions , SerializableException(..) @@ -77,6 +77,10 @@ data Pipe = Pipe , pipeReadClosed :: !(TVar Bool) -- ^ After the pipe is closed, messages can no longer be received from the -- other end, but the 'AsyncManager' queues might not be empty yet. + , pipeWriteClosed :: !(TVar Bool) + -- ^ After the write end of the pipe is closed we can no longer submit + -- requests or respond to ones we receive, but we might still receive + -- replies. } -- | Make a 'Pipe' from a 'Handle' to read and a 'Handle' to write. @@ -101,6 +105,7 @@ mkPipeFromContinuations getSome putAll pipeCloseRead pipeCloseWrite = mdo pipeLeftovers <- newIORef Nothing pipeLock <- newMVar () pipeReadClosed <- newTVarIO False + pipeWriteClosed <- newTVarIO False mdo pipeAsyncMngr <- newAsyncManager p let p = Pipe { .. } @@ -114,20 +119,34 @@ mkPipeFromContinuations getSome putAll pipeCloseRead pipeCloseWrite = mdo -- write. Only then do we stop the drain thread, which closes the read end -- itself once its read has finished. On Windows its read can't be -- interrupted, so this waits for the other side to close its write end too. +-- Finally we stop the write thread, which could be blocked waiting for more to write. closePipe :: Pipe -> IO () closePipe p@Pipe{..} = do readAlreadyClosed <- atomically $ do closed <- readTVar pipeReadClosed failPendingReplies p (toException PipeReadClosedException) pure closed - withLock p pipeCloseWrite `finally` - unless readAlreadyClosed - (throwTo (asyncDrainTid pipeAsyncMngr) PipeReadClosedException) + + writeAlreadyClosed <- atomically $ do + closed <- readTVar pipeWriteClosed + failPendingWrites p (toException PipeWriteClosedException) + pure closed + + unless writeAlreadyClosed (withLock p pipeCloseWrite) + `finally` do + unless readAlreadyClosed + (throwTo (asyncDrainTid pipeAsyncMngr) PipeReadClosedException) + unless writeAlreadyClosed + (throwTo (asyncFillTid pipeAsyncMngr) PipeWriteClosedException) -- | We've closed the pipe read end on this process data PipeReadClosedException = PipeReadClosedException deriving Show instance Exception PipeReadClosedException +-- | We've closed the pipe write end on this process +data PipeWriteClosedException = PipeWriteClosedException deriving Show +instance Exception PipeWriteClosedException + -------------------------------------------------------------------------------- -- ** Pipe thread-safe read/write -------------------------------------------------------------------------------- @@ -135,7 +154,14 @@ instance Exception PipeReadClosedException -- and identify whether they are requests or replies. writePipe :: Pipe -> IddMsg -> IO () -writePipe p@Pipe{..} = withLock p . putAll . execPut . put +writePipe Pipe{..} msg = do + var <- newEmptyTMVarIO + atomically $ do + readTVar pipeWriteClosed >>= \case + True -> throwSTM PipeWriteClosedException + False -> pure () + writeTQueue (writeQueue pipeAsyncMngr) (msg, var) + either throwIO pure =<< atomically (takeTMVar var) -- | Only the 'drainPipe' thread should read from the pipe directly. Other -- functions should wait for the matching reply to be delivered asynchronously. @@ -229,6 +255,10 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do -- Block waiting for request mbreq <- atomically $ do + readTVar (pipeWriteClosed pipe) >>= \case + -- We will not be able to write the reply, so no point in continuiting. + True -> throwSTM PipeWriteClosedException + False -> pure () isClosed <- readTVar (pipeReadClosed pipe) taken <- case mii of Nothing -> tryReadTQueue $ requestsQueue am @@ -320,7 +350,10 @@ data AsyncManager = AsyncManager , mailboxQueues :: !(TVar (IntMap (TQueue (CorrelationId, LB.ByteString)))) -- ^ Requests for a specific 'InboxId' that can be read using -- @'handleAsyncReq' p get (Just <mailboxid>)@. + , writeQueue :: !(TQueue (IddMsg, TMVar (Either SomeException ()))) + -- ^ Messages to write on the pipe. , asyncDrainTid :: !ThreadId + , asyncFillTid :: !ThreadId , asyncFreshId :: !(IORef Int) } @@ -363,11 +396,16 @@ newAsyncManager pipe = do pendingReply <- newTVarIO IntMap.empty requestsQueue <- newTQueueIO mailboxQueues <- newTVarIO IntMap.empty + writeQueue <- newTQueueIO -- Fork thread to drain read end. -- (cleaned up on 'closePipe') asyncDrainTid <- forkIOWithUnmask (drainPipe pipe) + -- Fork thread to fill write end. + -- (cleaned up on 'closePipe') + asyncFillTid <- forkIOWithUnmask (fillPipe pipe) + let am = AsyncManager{..} pure am @@ -417,6 +455,30 @@ drainPipe p unmask = catch (unmask $ loop `finally` pipeCloseRead p) replyToPend am = pipeAsyncMngr p + +-- | "Fill"s write end of the Pipe with queues messages. +-- +-- Because Pipe is written to from this thread rather than 'writePipe', calls to +-- the latter can be interrupted without compromising the protocol. +-- +-- Closes the Pipe write end when stopping, notifying any messages still queued to write. +fillPipe :: Pipe -> (forall x. IO x -> IO x) -> IO () +fillPipe p unmask = catch (loop `finally` pipeCloseWrite p) (atomically . failPendingWrites p) where + loop = do + (msg,var) <- atomically $ readTQueue (writeQueue am) + unmask (withLock p (putAll p . execPut . put $ msg)) + `catch` \ e -> atomically (putTMVar var (Left e)) >> throwIO e + atomically $ putTMVar var (Right ()) + loop + + am = pipeAsyncMngr p + +failPendingWrites :: Pipe -> SomeException -> STM () +failPendingWrites p e = do + writeTVar (pipeWriteClosed p) True + writes <- flushTQueue (writeQueue (pipeAsyncMngr p)) + mapM_ (`writeTMVar` Left e) (map snd writes) + failPendingReplies :: Pipe -> SomeException -> STM () failPendingReplies p e = do -- reqs are only added if pipe not closed; @@ -447,10 +509,20 @@ 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 +(%>>=) :: (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 ===================================== @@ -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,10 @@ import System.Environment (getProgName, getArgs) import System.Exit import System.IO +import Control.Concurrent +import GHCi.Message.Async (runWriteReply, (%>>=)) +import GHCi.Server.Threads + type MessageHook = Msg -> IO Msg -- | How to interpret the 'CustomCommand'. @@ -58,29 +61,35 @@ 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 + loop doneRef mngr = do when verbose $ trace "reading pipe..." - handleRemoteCall pipe hook $ \(msg :: Message a) -> do - discardCtrlC + handleRemoteCall pipe hook $ \wr (msg :: Message a) -> + + discardCtrlC %>>= \_ -> + when verbose (trace ("msg: " ++ (show msg))) %>>= \_ -> - 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 closePipe pipe - else loop doneRef + if isDone then pure () + else loop doneRef mngr handleCustom tag payload = do mresp <- customHandler tag payload @@ -92,7 +101,7 @@ servWithCustom verbose hook pipe restore customHandler = newIORef False >>= loop 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,62 @@ +{-# 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{..} = unsafe_linear \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 ) + +unsafe_linear :: (a -> b) %1 -> (a %1 -> b) +unsafe_linear = Unsafe.unsafeCoerce (Unsafe.unsafeCoerce :: (a -> b) -> (a %1 -> b)) ===================================== 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/436ac6992397cba842edefec57737f6... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/436ac6992397cba842edefec57737f6... 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