Rodrigo Mesquita pushed to branch wip/romes/async-rpc at Glasgow Haskell Compiler / GHC

Commits:

5 changed files:

Changes:

  • libraries/ghci/GHCi/Message.hs
    ... ... @@ -26,7 +26,7 @@ module GHCi.Message
    26 26
     
    
    27 27
       , Pipe(..), mkPipeFromHandles, mkPipeFromContinuations, closePipe
    
    28 28
       , remoteCall, remoteTHCall
    
    29
    -  , handleRemoteCall, handleRemoteTHCall
    
    29
    +  , handleRemoteCall, handleRemoteTHCall, writeReply
    
    30 30
       , RemoteProcessException(..), PipeReadClosedException(..)
    
    31 31
     
    
    32 32
       , InboxId(..)
    
    ... ... @@ -55,6 +55,7 @@ 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(..))
    
    58 59
     import Control.Concurrent
    
    59 60
     import Control.DeepSeq
    
    60 61
     import Control.Exception
    
    ... ... @@ -664,11 +665,13 @@ remoteCall pipe msg = either throwIO (pure . runGet get)
    664 665
     
    
    665 666
     -- | Handle a single 'Message' request on the interpreter and reply to the host
    
    666 667
     handleRemoteCall :: Pipe
    
    667
    -                 -> (CorrelationId -> Msg -> (Msg -> IO ()) %1 -> IO ()) {-^ called with action that handles req and writes reply -}
    
    668
    -                 -> (forall a. (Show a, Binary a) => Message a -> IO a)
    
    669
    -                 -> IO ()
    
    670
    -handleRemoteCall pipe forkHandler k =
    
    671
    -  handleAsyncReq pipe getMessage Nothing (\(Msg msg) -> fmap put . k $ msg) forkHandler
    
    668
    +                 -> (Msg -> IO Msg) {-^ Msg hook -}
    
    669
    +                 -> (forall a. (Show a, Binary a) => WriteReply a %1 -> Message a -> IO r)
    
    670
    +                 -> IO r
    
    671
    +handleRemoteCall pipe hook k =
    
    672
    +  handleAsyncReq pipe getMessage Nothing \(WriteReply writer) msg0 -> IO \s0 ->
    
    673
    +    case hook msg0 |#> s0 of -- unIO for linearity checking
    
    674
    +      (# s1, Msg msg #) -> k (WriteReply \x -> writer (fmap put x)) msg |#> s1
    
    672 675
     
    
    673 676
     -- | Send a 'THMessage' request to the host and wait for a reply from the host
    
    674 677
     -- to the interpreter.
    
    ... ... @@ -686,5 +689,7 @@ remoteTHCall pipe ii msg = either throwIO (pure . runGet get)
    686 689
     handleRemoteTHCall
    
    687 690
       :: Pipe -> InboxId -> (forall a. (Show a, Binary a) => THMessage a -> IO a) -> IO ()
    
    688 691
     handleRemoteTHCall pipe ii k =
    
    689
    -  handleAsyncReq pipe getTHMessage (Just ii) (\(THMsg msg) -> fmap put . k $ msg) $ \ _ msg hndlr -> hndlr msg
    
    692
    +  handleAsyncReq pipe getTHMessage (Just ii) \writer (THMsg msg) -> IO \s0 ->
    
    693
    +    case k msg |#> s0 of -- unIO for linearity checking
    
    694
    +      (# s1, a #) -> writeReply writer (put a) |#> s1
    
    690 695
     

  • libraries/ghci/GHCi/Message/Async.hs
    ... ... @@ -23,8 +23,8 @@ module GHCi.Message.Async
    23 23
       , IddMsg(..), newAsyncManager
    
    24 24
     
    
    25 25
         -- ** Consume `reply` exactly once
    
    26
    -  , WriteReply(..)
    
    27
    -  , writeReply, (|#>), bindIO
    
    26
    +  , WriteReply(..), writeReply
    
    27
    +  , runWriteReply, (|#>), (%>>=)
    
    28 28
     
    
    29 29
         -- * Serializable exceptions
    
    30 30
       , SerializableException(..)
    
    ... ... @@ -226,9 +226,10 @@ remoteAsyncReq pipe msg ii = do
    226 226
     -- It must be used exactly once (all requests expect a reply, and you can't
    
    227 227
     -- reply more than once). Linear types enforce the handler replies in every
    
    228 228
     -- branch, and if the handler otherwise throws, it is handled by 'handleAsyncReq'.
    
    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
    
    229
    +handleAsyncReq :: Pipe -> Get a -> (Maybe InboxId) -> (WriteReply Put %1 -> a -> IO r) -> IO r
    
    230
    +handleAsyncReq pipe getit mii k = mask $ \restore -> do
    
    231 231
       let am = pipeAsyncMngr pipe
    
    232
    +  ack <- newEmptyMVar
    
    232 233
     
    
    233 234
       -- Block waiting for request
    
    234 235
       mbreq <- atomically $ do
    
    ... ... @@ -246,6 +247,7 @@ handleAsyncReq pipe getit mii hndlr k = mask $ \restore -> do
    246 247
         Just (uq, bs) -> do
    
    247 248
           let reply p = do
    
    248 249
                 writePipe pipe (Reply uq (bimap toSerializableException runPut p))
    
    250
    +            putMVar ack ()
    
    249 251
     
    
    250 252
           -- Run the continuation which must reply exactly once with 'writeReply'
    
    251 253
           case runGetOrFail getit bs of
    
    ... ... @@ -253,11 +255,20 @@ handleAsyncReq pipe getit mii hndlr k = mask $ \restore -> do
    253 255
               let exc = DecodeAsyncReqException err
    
    254 256
               reply (Left (toException exc))
    
    255 257
               throwIO exc
    
    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
    -          )
    
    258
    +        Right (_, _, it) ->
    
    259
    +          restore (k (WriteReply reply) it)
    
    260
    +
    
    261
    +            -- If there's an exception before the reply is sent, reply with an error instead.
    
    262
    +            `catch` \(e :: SomeException) -> do
    
    263
    +
    
    264
    +                tryTakeMVar ack >>= \case
    
    265
    +                  Just () ->
    
    266
    +                    -- Already replied
    
    267
    +                    throwIO e
    
    268
    +                  Nothing -> do
    
    269
    +                    -- Reply with an error
    
    270
    +                    reply (Left e)
    
    271
    +                    throwIO e
    
    261 272
     
    
    262 273
     -- | 'handleSyncReq' failed to decode an incoming request payload using the given @'Get' a@
    
    263 274
     newtype DecodeAsyncReqException = DecodeAsyncReqException String deriving Show
    
    ... ... @@ -440,14 +451,19 @@ data WriteReply a where
    440 451
     writeReply :: WriteReply a %1 -> a -> IO ()
    
    441 452
     writeReply (WriteReply r) = \x -> r (Right x)
    
    442 453
     
    
    454
    +-- | Write the reply with a value or an exception
    
    455
    +runWriteReply :: WriteReply a %1 -> Either SomeException a -> IO ()
    
    456
    +runWriteReply (WriteReply r) = r
    
    457
    +
    
    443 458
     -- | Infix, linear, 'unIO'
    
    444 459
     (|#>) :: IO a %1 -> State# RealWorld -> (# State# RealWorld, a #)
    
    445 460
     (|#>) (IO fs) s = fs s
    
    446 461
     
    
    447
    -bindIO :: (IO a) -> (a -> IO b) %1 -> IO b
    
    448
    -bindIO = \ m f -> IO (\s0 ->
    
    462
    +(%>>=) :: (IO a) -> (a -> IO b) %1 -> IO b
    
    463
    +(%>>=) = \ m f -> IO (\s0 ->
    
    449 464
             case m |#> s0 of -- unIO for linearity checking
    
    450 465
               (# s1, x #) -> f x |#> s1)
    
    466
    +infixl 1 %>>=
    
    451 467
     
    
    452 468
     --------------------------------------------------------------------------------
    
    453 469
     -- * Serializable Exceptions
    

  • libraries/ghci/GHCi/Server.hs
    ... ... @@ -39,13 +39,8 @@ import System.Exit
    39 39
     import System.IO
    
    40 40
     
    
    41 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
    
    42
    +import GHCi.Message.Async (runWriteReply, (%>>=))
    
    43
    +import GHCi.Server.Threads
    
    49 44
     
    
    50 45
     type MessageHook = Msg -> IO Msg
    
    51 46
     
    
    ... ... @@ -62,49 +57,6 @@ serv :: Bool -> MessageHook -> Pipe -> (forall a .IO a -> IO a) -> IO ()
    62 57
     serv verbose hook pipe restore =
    
    63 58
       servWithCustom verbose hook pipe restore noCustomHandler
    
    64 59
     
    
    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
    -
    
    108 60
     servWithCustom
    
    109 61
       :: Bool
    
    110 62
       -> MessageHook
    
    ... ... @@ -117,23 +69,23 @@ servWithCustom verbose hook pipe restore customHandler = do
    117 69
       withThreadManager $ \mngr ->
    
    118 70
         loop ref mngr `finally` closePipe pipe
    
    119 71
      where
    
    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 72
       loop doneRef mngr = do
    
    128 73
         when verbose $ trace "reading pipe..."
    
    129
    -    handleRemoteCall pipe (\uq msg act -> hook msg `bindIO` forkHandler mngr uq act) $ \(msg :: Message a) -> do
    
    74
    +
    
    75
    +    handleRemoteCall pipe hook $ \wr (msg :: Message a) ->
    
    76
    +
    
    77
    +      discardCtrlC                                 %>>= \_ ->
    
    78
    +      when verbose (trace ("msg: " ++ (show msg))) %>>= \_ ->
    
    79
    +
    
    130 80
           case msg of
    
    131
    -        CustomMessage tag payload -> handleCustom tag payload
    
    132
    -        Shutdown -> writeIORef doneRef True >> reply ()
    
    133
    -        -- TODO: These two should probably be done off the main thread
    
    134
    -        RunTH ii st q ty loc -> wrapRunTH ii $ runTH pipe ii st q ty loc
    
    135
    -        RunModFinalizers ii st qrefs -> wrapRunTH ii $ runModFinalizerRefs pipe ii st qrefs
    
    136
    -        _other -> run msg >>= reply
    
    81
    +        CustomMessage tag payload -> handleCustom tag payload %>>= writeReply wr
    
    82
    +        Shutdown                  -> writeIORef doneRef True >>
    
    83
    +                                     reply ()                 %>>= writeReply wr
    
    84
    +        RunTH ii st q ty loc      -> spawnFinally mngr         (runWriteReply wr)
    
    85
    +                                     (wrapRunTH ii (runTH pipe ii st q ty loc))
    
    86
    +        RunModFinalizers ii st qs -> spawnFinally mngr         (runWriteReply wr)
    
    87
    +                                     (wrapRunTH ii (runModFinalizerRefs pipe ii st qs))
    
    88
    +        _other                    -> run msg >>= reply        %>>= writeReply wr
    
    137 89
     
    
    138 90
         isDone <- readIORef doneRef
    
    139 91
         if isDone then pure ()
    
    ... ... @@ -149,7 +101,7 @@ servWithCustom verbose hook pipe restore customHandler = do
    149 101
       reply :: forall a. Show a => a -> IO a
    
    150 102
       reply r = do
    
    151 103
         when verbose $ trace ("writing pipe: " ++ show r)
    
    152
    -    return r -- the result of the handler gets sent
    
    104
    +    return r -- the result of the handler is passed to writeReply
    
    153 105
     
    
    154 106
       -- Run some TH code, which may interact with GHC by sending
    
    155 107
       -- THMessage requests, and then finally send RunTHDone followed by a
    

  • libraries/ghci/GHCi/Server/Threads.hs
    1
    +{-# LANGUAGE LambdaCase, CPP, RankNTypes, RecordWildCards, GADTs, ScopedTypeVariables, LinearTypes, BlockArguments #-}
    
    2
    +module GHCi.Server.Threads
    
    3
    +  ( ThreadManager     -- abstract for linearity
    
    4
    +  , withThreadManager -- scoped, so that we're guaranteed to wait for threads
    
    5
    +  , spawnFinally
    
    6
    +  ) where
    
    7
    +
    
    8
    +import Prelude
    
    9
    +import Control.Exception
    
    10
    +import Control.Monad
    
    11
    +
    
    12
    +import Control.Concurrent
    
    13
    +import Control.Concurrent.STM
    
    14
    +import qualified Data.Set as Set
    
    15
    +import qualified Unsafe.Coerce as Unsafe
    
    16
    +
    
    17
    +data ThreadManager = ThreadManager
    
    18
    +  { managedThreads :: !(TVar (Set.Set ThreadId))
    
    19
    +  , managerClosed  :: !(TVar Bool)
    
    20
    +  }
    
    21
    +
    
    22
    +-- | Cancels and Waits for all threads spawned by 'ThreadManager' to terminate
    
    23
    +-- before returning.
    
    24
    +--
    
    25
    +-- This thread manager which cancels then waits for all pending threads allows
    
    26
    +-- us to provide a resource-safe linearly typed 'spawnFinally', because
    
    27
    +-- 'withThreadManager' guarantees neither the threads nor their exceptions
    
    28
    +-- leak.
    
    29
    +withThreadManager :: (ThreadManager -> IO a) -> IO a
    
    30
    +withThreadManager k = do
    
    31
    +  managedThreads <- newTVarIO Set.empty
    
    32
    +  managerClosed  <- newTVarIO False
    
    33
    +  let tm = ThreadManager{..}
    
    34
    +  k tm `finally` shutdown tm
    
    35
    +  where
    
    36
    +    shutdown ThreadManager{..} = do
    
    37
    +      ts <- atomically $ do
    
    38
    +        writeTVar managerClosed True
    
    39
    +        Set.elems <$> readTVar managedThreads
    
    40
    +      mapM_ killThread ts
    
    41
    +
    
    42
    +-- | Linear and resource-safe because 'ThreadManager' is guaranteed to wait or
    
    43
    +-- cancel all threads.
    
    44
    +spawnFinally :: ThreadManager -> (Either SomeException a -> IO ()) %1 -> IO a -> IO ()
    
    45
    +spawnFinally ThreadManager{..} = unsafeLinear \fin -> \action -> void $
    
    46
    +  forkFinally
    
    47
    +    ( mask $ \unmask -> do
    
    48
    +        tid <- myThreadId
    
    49
    +        registered <- atomically $ do
    
    50
    +          s <- readTVar managerClosed
    
    51
    +          unless s $ modifyTVar' managedThreads (Set.insert tid)
    
    52
    +          pure (not s)
    
    53
    +        if registered
    
    54
    +          then (unmask action)
    
    55
    +                `finally`
    
    56
    +                  atomically (modifyTVar' managedThreads (Set.delete tid))
    
    57
    +          else throwIO ThreadKilled
    
    58
    +    )
    
    59
    +    ( \ea -> fin ea )
    
    60
    +
    
    61
    +unsafeLinear :: (a -> b) %1 -> (a %1 -> b)
    
    62
    +unsafeLinear = Unsafe.unsafeCoerce (Unsafe.unsafeCoerce :: (a -> b) -> (a %1 -> b))
    
    63
    +
    
    64
    +-- TODO:
    
    65
    +-- could be used to cancel a specific handler.
    
    66
    +-- cancel :: ThreadManager -> CorrelationId -> IO ()
    
    67
    +-- cancel ThreadManager{..} i = do
    
    68
    +--   ts <- atomically $ do
    
    69
    +--     readTVar managedThreads
    
    70
    +--   case IM.lookup i ts of
    
    71
    +--     Just tid -> killThread tid
    
    72
    +--     Nothing  -> pure ()

  • libraries/ghci/ghci.cabal.in
    ... ... @@ -68,6 +68,7 @@ library
    68 68
                 GHCi.StaticPtrTable
    
    69 69
                 GHCi.TH
    
    70 70
                 GHCi.Server
    
    71
    +            GHCi.Server.Threads
    
    71 72
     
    
    72 73
             if !arch(javascript)
    
    73 74
               exposed-modules: