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
-
ed3ec8fc
by Andrea Vezzosi at 2026-10-16T16:16:56+02:00
-
ee5d2db0
by Andrea Vezzosi at 2026-10-16T16:21:54+02:00
3 changed files:
Changes:
| ... | ... | @@ -55,7 +55,6 @@ import qualified GHC.Exts.Heap as Heap |
| 55 | 55 | import GHC.ForeignSrcLang
|
| 56 | 56 | import GHC.Fingerprint
|
| 57 | 57 | import GHC.Conc (pseq, par)
|
| 58 | -import GHC.IO (IO(..))
|
|
| 59 | 58 | import Control.Concurrent
|
| 60 | 59 | import Control.DeepSeq
|
| 61 | 60 | import Control.Exception
|
| ... | ... | @@ -665,14 +664,11 @@ remoteCall pipe msg = either throwIO (pure . runGet get) |
| 665 | 664 | |
| 666 | 665 | -- | Handle a single 'Message' request on the interpreter and reply to the host
|
| 667 | 666 | handleRemoteCall :: Pipe
|
| 668 | - -> (Msg -> IO Msg) {-^ Msg hook -}
|
|
| 667 | + -> (CorrelationId -> Msg -> (Msg -> IO ()) %1 -> IO ()) {-^ called with action that handles req and writes reply -}
|
|
| 669 | 668 | -> (forall a. (Show a, Binary a) => Message a -> IO a)
|
| 670 | 669 | -> IO ()
|
| 671 | -handleRemoteCall pipe hook k =
|
|
| 672 | - handleAsyncReq pipe getMessage Nothing \writer msg0 -> IO \s0 ->
|
|
| 673 | - case hook msg0 |#> s0 of -- unIO for linearity checking
|
|
| 674 | - (# s1, Msg msg #) -> case k msg |#> s1 of
|
|
| 675 | - (# s2, a #) -> writeReply writer (put a) |#> s2
|
|
| 670 | +handleRemoteCall pipe forkHandler k =
|
|
| 671 | + handleAsyncReq pipe getMessage Nothing (\(Msg msg) -> fmap put . k $ msg) forkHandler
|
|
| 676 | 672 | |
| 677 | 673 | -- | Send a 'THMessage' request to the host and wait for a reply from the host
|
| 678 | 674 | -- to the interpreter.
|
| ... | ... | @@ -690,7 +686,5 @@ remoteTHCall pipe ii msg = either throwIO (pure . runGet get) |
| 690 | 686 | handleRemoteTHCall
|
| 691 | 687 | :: Pipe -> InboxId -> (forall a. (Show a, Binary a) => THMessage a -> IO a) -> IO ()
|
| 692 | 688 | handleRemoteTHCall pipe ii k =
|
| 693 | - handleAsyncReq pipe getTHMessage (Just ii) \writer (THMsg msg) -> IO \s0 ->
|
|
| 694 | - case k msg |#> s0 of -- unIO for linearity checking
|
|
| 695 | - (# s1, a #) -> writeReply writer (put a) |#> s1
|
|
| 689 | + handleAsyncReq pipe getTHMessage (Just ii) (\(THMsg msg) -> fmap put . k $ msg) $ \ _ msg hndlr -> hndlr msg
|
|
| 696 | 690 |
| ... | ... | @@ -24,7 +24,7 @@ module GHCi.Message.Async |
| 24 | 24 | |
| 25 | 25 | -- ** Consume `reply` exactly once
|
| 26 | 26 | , WriteReply(..)
|
| 27 | - , writeReply, (|#>)
|
|
| 27 | + , writeReply, (|#>), bindIO
|
|
| 28 | 28 | |
| 29 | 29 | -- * Serializable exceptions
|
| 30 | 30 | , SerializableException(..)
|
| ... | ... | @@ -135,7 +135,11 @@ instance Exception PipeReadClosedException |
| 135 | 135 | -- and identify whether they are requests or replies.
|
| 136 | 136 | |
| 137 | 137 | writePipe :: Pipe -> IddMsg -> IO ()
|
| 138 | -writePipe p@Pipe{..} = withLock p . putAll . execPut . put
|
|
| 138 | +writePipe p@Pipe{..} msg = withLock p $ do
|
|
| 139 | + putAll (execPut (put msg)) `catch` \(e :: IOError) -> do
|
|
| 140 | + pipeCloseWrite
|
|
| 141 | + -- TODO: do we need a pipeCloseWrite TVar ?
|
|
| 142 | + throwIO e
|
|
| 139 | 143 | |
| 140 | 144 | -- | Only the 'drainPipe' thread should read from the pipe directly. Other
|
| 141 | 145 | -- functions should wait for the matching reply to be delivered asynchronously.
|
| ... | ... | @@ -222,10 +226,9 @@ remoteAsyncReq pipe msg ii = do |
| 222 | 226 | -- It must be used exactly once (all requests expect a reply, and you can't
|
| 223 | 227 | -- reply more than once). Linear types enforce the handler replies in every
|
| 224 | 228 | -- branch, and if the handler otherwise throws, it is handled by 'handleAsyncReq'.
|
| 225 | -handleAsyncReq :: Pipe -> Get a -> (Maybe InboxId) -> (WriteReply Put %1 -> a -> IO r) -> IO r
|
|
| 226 | -handleAsyncReq pipe getit mii k = mask $ \restore -> do
|
|
| 229 | +handleAsyncReq :: Pipe -> Get a -> (Maybe InboxId) -> (a -> IO Put) -> (CorrelationId -> a -> (a -> IO ()) %1 -> IO ()) -> IO ()
|
|
| 230 | +handleAsyncReq pipe getit mii hndlr k = mask $ \restore -> do
|
|
| 227 | 231 | let am = pipeAsyncMngr pipe
|
| 228 | - ack <- newEmptyMVar
|
|
| 229 | 232 | |
| 230 | 233 | -- Block waiting for request
|
| 231 | 234 | mbreq <- atomically $ do
|
| ... | ... | @@ -243,7 +246,6 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do |
| 243 | 246 | Just (uq, bs) -> do
|
| 244 | 247 | let reply p = do
|
| 245 | 248 | writePipe pipe (Reply uq (bimap toSerializableException runPut p))
|
| 246 | - putMVar ack ()
|
|
| 247 | 249 | |
| 248 | 250 | -- Run the continuation which must reply exactly once with 'writeReply'
|
| 249 | 251 | case runGetOrFail getit bs of
|
| ... | ... | @@ -251,20 +253,11 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do |
| 251 | 253 | let exc = DecodeAsyncReqException err
|
| 252 | 254 | reply (Left (toException exc))
|
| 253 | 255 | throwIO exc
|
| 254 | - Right (_, _, it) ->
|
|
| 255 | - restore (k (WriteReply reply) it)
|
|
| 256 | - |
|
| 257 | - -- If there's an exception before the reply is sent, reply with an error instead.
|
|
| 258 | - `catch` \(e :: SomeException) -> do
|
|
| 259 | - |
|
| 260 | - tryTakeMVar ack >>= \case
|
|
| 261 | - Just () ->
|
|
| 262 | - -- Already replied
|
|
| 263 | - throwIO e
|
|
| 264 | - Nothing -> do
|
|
| 265 | - -- Reply with an error
|
|
| 266 | - reply (Left e)
|
|
| 267 | - throwIO e
|
|
| 256 | + Right (_, _, it) -> k uq it (\ msg -> do
|
|
| 257 | + try @SomeException (restore (hndlr msg)) >>= \x -> do
|
|
| 258 | + reply x
|
|
| 259 | + either throwIO (const $ pure ()) x
|
|
| 260 | + )
|
|
| 268 | 261 | |
| 269 | 262 | -- | 'handleSyncReq' failed to decode an incoming request payload using the given @'Get' a@
|
| 270 | 263 | newtype DecodeAsyncReqException = DecodeAsyncReqException String deriving Show
|
| ... | ... | @@ -451,6 +444,11 @@ writeReply (WriteReply r) = \x -> r (Right x) |
| 451 | 444 | (|#>) :: IO a %1 -> State# RealWorld -> (# State# RealWorld, a #)
|
| 452 | 445 | (|#>) (IO fs) s = fs s
|
| 453 | 446 | |
| 447 | +bindIO :: (IO a) -> (a -> IO b) %1 -> IO b
|
|
| 448 | +bindIO = \ m f -> IO (\s0 ->
|
|
| 449 | + case m |#> s0 of -- unIO for linearity checking
|
|
| 450 | + (# s1, x #) -> f x |#> s1)
|
|
| 451 | + |
|
| 454 | 452 | --------------------------------------------------------------------------------
|
| 455 | 453 | -- * Serializable Exceptions
|
| 456 | 454 | --------------------------------------------------------------------------------
|
| 1 | -{-# LANGUAGE LambdaCase, CPP, RankNTypes, RecordWildCards, GADTs, ScopedTypeVariables #-}
|
|
| 1 | +{-# LANGUAGE LambdaCase, CPP, RankNTypes, RecordWildCards, GADTs, ScopedTypeVariables, LinearTypes #-}
|
|
| 2 | 2 | module GHCi.Server
|
| 3 | 3 | ( MessageHook
|
| 4 | 4 | , CustomMessageHandler
|
| ... | ... | @@ -31,7 +31,6 @@ import GHCi.Utils |
| 31 | 31 | import Control.DeepSeq
|
| 32 | 32 | import Control.Exception
|
| 33 | 33 | import Control.Monad
|
| 34 | -import Control.Concurrent (threadDelay)
|
|
| 35 | 34 | import Data.Binary
|
| 36 | 35 | |
| 37 | 36 | import Text.Printf
|
| ... | ... | @@ -39,6 +38,15 @@ import System.Environment (getProgName, getArgs) |
| 39 | 38 | import System.Exit
|
| 40 | 39 | import System.IO
|
| 41 | 40 | |
| 41 | +import Control.Concurrent
|
|
| 42 | +import Control.Concurrent.STM
|
|
| 43 | +import Data.IntMap (IntMap)
|
|
| 44 | +import qualified Data.IntMap as IM
|
|
| 45 | +import GHCi.Message.Async (bindIO)
|
|
| 46 | +import qualified Unsafe.Coerce as Unsafe
|
|
| 47 | + |
|
| 48 | +type CorrelationId = Int
|
|
| 49 | + |
|
| 42 | 50 | type MessageHook = Msg -> IO Msg
|
| 43 | 51 | |
| 44 | 52 | -- | How to interpret the 'CustomCommand'.
|
| ... | ... | @@ -54,22 +62,71 @@ serv :: Bool -> MessageHook -> Pipe -> (forall a .IO a -> IO a) -> IO () |
| 54 | 62 | serv verbose hook pipe restore =
|
| 55 | 63 | servWithCustom verbose hook pipe restore noCustomHandler
|
| 56 | 64 | |
| 65 | + |
|
| 66 | +data ThreadManager = ThreadManager
|
|
| 67 | + { managedThreads :: !(TVar (IntMap ThreadId))
|
|
| 68 | + , managerClosed :: !(TVar Bool)
|
|
| 69 | + }
|
|
| 70 | + |
|
| 71 | +-- | Cancels and Waits for all threads spawned by ThreadManager to terminate before returning.
|
|
| 72 | +withThreadManager :: (ThreadManager -> IO a) -> IO a
|
|
| 73 | +withThreadManager k = do
|
|
| 74 | + managedThreads <- newTVarIO IM.empty
|
|
| 75 | + managerClosed <- newTVarIO False
|
|
| 76 | + let tm = ThreadManager{..}
|
|
| 77 | + k tm `finally` shutdown tm
|
|
| 78 | + where
|
|
| 79 | + shutdown ThreadManager{..} = do
|
|
| 80 | + ts <- atomically $ do
|
|
| 81 | + writeTVar managerClosed True
|
|
| 82 | + IM.elems <$> readTVar managedThreads
|
|
| 83 | + mapM_ killThread ts
|
|
| 84 | + |
|
| 85 | +spawn :: ThreadManager -> CorrelationId -> IO () %1 -> IO ()
|
|
| 86 | +spawn mng i = Unsafe.unsafeCoerce (spawn' mng i)
|
|
| 87 | + |
|
| 88 | +spawn' :: ThreadManager -> CorrelationId -> IO () -> IO ()
|
|
| 89 | +spawn' ThreadManager{..} i action = void $ mask_ $ forkIOWithUnmask $ \unmask -> do
|
|
| 90 | + tid <- myThreadId
|
|
| 91 | + registered <- atomically $ do
|
|
| 92 | + s <- readTVar managerClosed
|
|
| 93 | + unless s $ modifyTVar' managedThreads (IM.insert i tid)
|
|
| 94 | + pure (not s)
|
|
| 95 | + when registered $
|
|
| 96 | + (unmask action)
|
|
| 97 | + `finally` atomically (modifyTVar' managedThreads (IM.delete i))
|
|
| 98 | + |
|
| 99 | +-- could be used to cancel a specific handler.
|
|
| 100 | +-- cancel :: ThreadManager -> CorrelationId -> IO ()
|
|
| 101 | +-- cancel ThreadManager{..} i = do
|
|
| 102 | +-- ts <- atomically $ do
|
|
| 103 | +-- readTVar managedThreads
|
|
| 104 | +-- case IM.lookup i ts of
|
|
| 105 | +-- Just tid -> killThread tid
|
|
| 106 | +-- Nothing -> pure ()
|
|
| 107 | + |
|
| 57 | 108 | servWithCustom
|
| 58 | 109 | :: Bool
|
| 59 | 110 | -> MessageHook
|
| 60 | 111 | -> Pipe
|
| 61 | - -> (forall a .IO a -> IO a)
|
|
| 112 | + -> (forall a. IO a -> IO a)
|
|
| 62 | 113 | -> CustomMessageHandler
|
| 63 | 114 | -> IO ()
|
| 64 | -servWithCustom verbose hook pipe restore customHandler = newIORef False >>= loop
|
|
| 115 | +servWithCustom verbose hook pipe restore customHandler = do
|
|
| 116 | + ref <- newIORef False
|
|
| 117 | + withThreadManager $ \mngr ->
|
|
| 118 | + loop ref mngr `finally` closePipe pipe
|
|
| 65 | 119 | where
|
| 66 | - loop doneRef = do
|
|
| 120 | + forkHandler mngr uq hndlr msg0@(Msg msg) = do
|
|
| 121 | + (do discardCtrlC
|
|
| 122 | + when verbose $ trace ("msg: " ++ (show msg))) `bindIO` \ _ -> do
|
|
| 123 | + case msg of
|
|
| 124 | + RunTH{} -> spawn mngr uq (hndlr msg0)
|
|
| 125 | + RunModFinalizers{} -> spawn mngr uq (hndlr msg0)
|
|
| 126 | + _ -> hndlr msg0
|
|
| 127 | + loop doneRef mngr = do
|
|
| 67 | 128 | when verbose $ trace "reading pipe..."
|
| 68 | - handleRemoteCall pipe hook $ \(msg :: Message a) -> do
|
|
| 69 | - |
|
| 70 | - discardCtrlC
|
|
| 71 | - |
|
| 72 | - when verbose $ trace ("msg: " ++ (show msg))
|
|
| 129 | + handleRemoteCall pipe (\uq msg act -> hook msg `bindIO` forkHandler mngr uq act) $ \(msg :: Message a) -> do
|
|
| 73 | 130 | case msg of
|
| 74 | 131 | CustomMessage tag payload -> handleCustom tag payload
|
| 75 | 132 | Shutdown -> writeIORef doneRef True >> reply ()
|
| ... | ... | @@ -79,8 +136,8 @@ servWithCustom verbose hook pipe restore customHandler = newIORef False >>= loop |
| 79 | 136 | _other -> run msg >>= reply
|
| 80 | 137 | |
| 81 | 138 | isDone <- readIORef doneRef
|
| 82 | - if isDone then closePipe pipe
|
|
| 83 | - else loop doneRef
|
|
| 139 | + if isDone then pure ()
|
|
| 140 | + else loop doneRef mngr
|
|
| 84 | 141 | |
| 85 | 142 | handleCustom tag payload = do
|
| 86 | 143 | mresp <- customHandler tag payload
|