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(..)
    
    ... ... @@ -666,13 +666,12 @@ remoteCall pipe msg = either throwIO (pure . runGet get)
    666 666
     -- | Handle a single 'Message' request on the interpreter and reply to the host
    
    667 667
     handleRemoteCall :: Pipe
    
    668 668
                      -> (Msg -> IO Msg) {-^ Msg hook -}
    
    669
    -                 -> (forall a. (Show a, Binary a) => Message a -> IO a)
    
    670
    -                 -> IO ()
    
    669
    +                 -> (forall a. (Show a, Binary a) => WriteReply a %1 -> Message a -> IO r)
    
    670
    +                 -> IO r
    
    671 671
     handleRemoteCall pipe hook k =
    
    672
    -  handleAsyncReq pipe getMessage Nothing \writer msg0 -> IO \s0 ->
    
    672
    +  handleAsyncReq pipe getMessage Nothing \(WriteReply writer) msg0 -> IO \s0 ->
    
    673 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
    
    674
    +      (# s1, Msg msg #) -> k (WriteReply \x -> writer (fmap put x)) msg |#> s1
    
    676 675
     
    
    677 676
     -- | Send a 'THMessage' request to the host and wait for a reply from the host
    
    678 677
     -- to the interpreter.
    

  • 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, (|#>)
    
    26
    +  , WriteReply(..), writeReply
    
    27
    +  , runWriteReply, (|#>), (%>>=)
    
    28 28
     
    
    29 29
         -- * Serializable exceptions
    
    30 30
       , SerializableException(..)
    
    ... ... @@ -77,6 +77,10 @@ data Pipe = Pipe
    77 77
       , pipeReadClosed  :: !(TVar Bool)
    
    78 78
         -- ^ After the pipe is closed, messages can no longer be received from the
    
    79 79
         -- other end, but the 'AsyncManager' queues might not be empty yet.
    
    80
    +  , pipeWriteClosed :: !(TVar Bool)
    
    81
    +    -- ^ After the write end of the pipe is closed we can no longer submit
    
    82
    +    -- requests or respond to ones we receive, but we might still receive
    
    83
    +    -- replies.
    
    80 84
       }
    
    81 85
     
    
    82 86
     -- | Make a 'Pipe' from a 'Handle' to read and a 'Handle' to write.
    
    ... ... @@ -101,6 +105,7 @@ mkPipeFromContinuations getSome putAll pipeCloseRead pipeCloseWrite = mdo
    101 105
       pipeLeftovers   <- newIORef Nothing
    
    102 106
       pipeLock        <- newMVar ()
    
    103 107
       pipeReadClosed  <- newTVarIO False
    
    108
    +  pipeWriteClosed  <- newTVarIO False
    
    104 109
       mdo
    
    105 110
         pipeAsyncMngr <- newAsyncManager p
    
    106 111
         let p = Pipe { .. }
    
    ... ... @@ -114,20 +119,34 @@ mkPipeFromContinuations getSome putAll pipeCloseRead pipeCloseWrite = mdo
    114 119
     -- write. Only then do we stop the drain thread, which closes the read end
    
    115 120
     -- itself once its read has finished. On Windows its read can't be
    
    116 121
     -- interrupted, so this waits for the other side to close its write end too.
    
    122
    +-- Finally we stop the write thread, which could be blocked waiting for more to write.
    
    117 123
     closePipe :: Pipe -> IO ()
    
    118 124
     closePipe p@Pipe{..} = do
    
    119 125
       readAlreadyClosed <- atomically $ do
    
    120 126
         closed <- readTVar pipeReadClosed
    
    121 127
         failPendingReplies p (toException PipeReadClosedException)
    
    122 128
         pure closed
    
    123
    -  withLock p pipeCloseWrite `finally`
    
    124
    -    unless readAlreadyClosed
    
    125
    -      (throwTo (asyncDrainTid pipeAsyncMngr) PipeReadClosedException)
    
    129
    +
    
    130
    +  writeAlreadyClosed <- atomically $ do
    
    131
    +    closed <- readTVar pipeWriteClosed
    
    132
    +    failPendingWrites p (toException PipeWriteClosedException)
    
    133
    +    pure closed
    
    134
    +
    
    135
    +  unless writeAlreadyClosed (withLock p pipeCloseWrite)
    
    136
    +    `finally` do
    
    137
    +      unless readAlreadyClosed
    
    138
    +        (throwTo (asyncDrainTid pipeAsyncMngr) PipeReadClosedException)
    
    139
    +      unless writeAlreadyClosed
    
    140
    +        (throwTo (asyncFillTid pipeAsyncMngr) PipeWriteClosedException)
    
    126 141
     
    
    127 142
     -- | We've closed the pipe read end on this process
    
    128 143
     data PipeReadClosedException = PipeReadClosedException deriving Show
    
    129 144
     instance Exception PipeReadClosedException
    
    130 145
     
    
    146
    +-- | We've closed the pipe write end on this process
    
    147
    +data PipeWriteClosedException = PipeWriteClosedException deriving Show
    
    148
    +instance Exception PipeWriteClosedException
    
    149
    +
    
    131 150
     --------------------------------------------------------------------------------
    
    132 151
     -- ** Pipe thread-safe read/write
    
    133 152
     --------------------------------------------------------------------------------
    
    ... ... @@ -135,7 +154,14 @@ instance Exception PipeReadClosedException
    135 154
     -- and identify whether they are requests or replies.
    
    136 155
     
    
    137 156
     writePipe :: Pipe -> IddMsg -> IO ()
    
    138
    -writePipe p@Pipe{..} = withLock p . putAll . execPut . put
    
    157
    +writePipe Pipe{..} msg = do
    
    158
    +  var <- newEmptyTMVarIO
    
    159
    +  atomically $ do
    
    160
    +    readTVar pipeWriteClosed >>= \case
    
    161
    +      True -> throwSTM PipeWriteClosedException
    
    162
    +      False -> pure ()
    
    163
    +    writeTQueue (writeQueue pipeAsyncMngr) (msg, var)
    
    164
    +  either throwIO pure =<< atomically (takeTMVar var)
    
    139 165
     
    
    140 166
     -- | Only the 'drainPipe' thread should read from the pipe directly. Other
    
    141 167
     -- functions should wait for the matching reply to be delivered asynchronously.
    
    ... ... @@ -229,6 +255,10 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do
    229 255
     
    
    230 256
       -- Block waiting for request
    
    231 257
       mbreq <- atomically $ do
    
    258
    +    readTVar (pipeWriteClosed pipe) >>= \case
    
    259
    +      -- We will not be able to write the reply, so no point in continuiting.
    
    260
    +      True -> throwSTM PipeWriteClosedException
    
    261
    +      False -> pure ()
    
    232 262
         isClosed <- readTVar (pipeReadClosed pipe)
    
    233 263
         taken <- case mii of
    
    234 264
           Nothing -> tryReadTQueue  $  requestsQueue am
    
    ... ... @@ -320,7 +350,10 @@ data AsyncManager = AsyncManager
    320 350
       , mailboxQueues :: !(TVar (IntMap (TQueue (CorrelationId, LB.ByteString))))
    
    321 351
         -- ^ Requests for a specific 'InboxId' that can be read using
    
    322 352
         -- @'handleAsyncReq' p get (Just <mailboxid>)@.
    
    353
    +  , writeQueue    :: !(TQueue (IddMsg, TMVar (Either SomeException ())))
    
    354
    +    -- ^ Messages to write on the pipe.
    
    323 355
       , asyncDrainTid :: !ThreadId
    
    356
    +  , asyncFillTid  :: !ThreadId
    
    324 357
       , asyncFreshId  :: !(IORef Int)
    
    325 358
       }
    
    326 359
     
    
    ... ... @@ -363,11 +396,16 @@ newAsyncManager pipe = do
    363 396
       pendingReply  <- newTVarIO IntMap.empty
    
    364 397
       requestsQueue <- newTQueueIO
    
    365 398
       mailboxQueues <- newTVarIO IntMap.empty
    
    399
    +  writeQueue <- newTQueueIO
    
    366 400
     
    
    367 401
       -- Fork thread to drain read end.
    
    368 402
       -- (cleaned up on 'closePipe')
    
    369 403
       asyncDrainTid <- forkIOWithUnmask (drainPipe pipe)
    
    370 404
     
    
    405
    +  -- Fork thread to fill write end.
    
    406
    +  -- (cleaned up on 'closePipe')
    
    407
    +  asyncFillTid <- forkIOWithUnmask (fillPipe pipe)
    
    408
    +
    
    371 409
       let am = AsyncManager{..}
    
    372 410
       pure am
    
    373 411
     
    
    ... ... @@ -417,6 +455,30 @@ drainPipe p unmask = catch (unmask $ loop `finally` pipeCloseRead p) replyToPend
    417 455
     
    
    418 456
       am = pipeAsyncMngr p
    
    419 457
     
    
    458
    +
    
    459
    +-- | "Fill"s write end of the Pipe with queues messages.
    
    460
    +--
    
    461
    +-- Because Pipe is written to from this thread rather than 'writePipe', calls to
    
    462
    +-- the latter can be interrupted without compromising the protocol.
    
    463
    +--
    
    464
    +-- Closes the Pipe write end when stopping, notifying any messages still queued to write.
    
    465
    +fillPipe :: Pipe -> (forall x. IO x -> IO x) -> IO ()
    
    466
    +fillPipe p unmask = catch (loop `finally` pipeCloseWrite p) (atomically . failPendingWrites p) where
    
    467
    +  loop = do
    
    468
    +    (msg,var) <- atomically $ readTQueue (writeQueue am)
    
    469
    +    unmask (withLock p (putAll p . execPut . put $ msg))
    
    470
    +      `catch` \ e -> atomically (putTMVar var (Left e)) >> throwIO e
    
    471
    +    atomically $ putTMVar var (Right ())
    
    472
    +    loop
    
    473
    +
    
    474
    +  am = pipeAsyncMngr p
    
    475
    +
    
    476
    +failPendingWrites :: Pipe -> SomeException -> STM ()
    
    477
    +failPendingWrites p e = do
    
    478
    +  writeTVar (pipeWriteClosed p) True
    
    479
    +  writes <- flushTQueue (writeQueue (pipeAsyncMngr p))
    
    480
    +  mapM_ (`writeTMVar` Left e) (map snd writes)
    
    481
    +
    
    420 482
     failPendingReplies :: Pipe -> SomeException -> STM ()
    
    421 483
     failPendingReplies p e = do
    
    422 484
       -- reqs are only added if pipe not closed;
    
    ... ... @@ -447,10 +509,20 @@ data WriteReply a where
    447 509
     writeReply :: WriteReply a %1 -> a -> IO ()
    
    448 510
     writeReply (WriteReply r) = \x -> r (Right x)
    
    449 511
     
    
    512
    +-- | Write the reply with a value or an exception
    
    513
    +runWriteReply :: WriteReply a %1 -> Either SomeException a -> IO ()
    
    514
    +runWriteReply (WriteReply r) = r
    
    515
    +
    
    450 516
     -- | Infix, linear, 'unIO'
    
    451 517
     (|#>) :: IO a %1 -> State# RealWorld -> (# State# RealWorld, a #)
    
    452 518
     (|#>) (IO fs) s = fs s
    
    453 519
     
    
    520
    +(%>>=) :: (IO a) -> (a -> IO b) %1 -> IO b
    
    521
    +(%>>=) = \ m f -> IO (\s0 ->
    
    522
    +        case m |#> s0 of -- unIO for linearity checking
    
    523
    +          (# s1, x #) -> f x |#> s1)
    
    524
    +infixl 1 %>>=
    
    525
    +
    
    454 526
     --------------------------------------------------------------------------------
    
    455 527
     -- * Serializable Exceptions
    
    456 528
     --------------------------------------------------------------------------------
    

  • libraries/ghci/GHCi/Server.hs
    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,10 @@ import System.Environment (getProgName, getArgs)
    39 38
     import System.Exit
    
    40 39
     import System.IO
    
    41 40
     
    
    41
    +import Control.Concurrent
    
    42
    +import GHCi.Message.Async (runWriteReply, (%>>=))
    
    43
    +import GHCi.Server.Threads
    
    44
    +
    
    42 45
     type MessageHook = Msg -> IO Msg
    
    43 46
     
    
    44 47
     -- | How to interpret the 'CustomCommand'.
    
    ... ... @@ -58,29 +61,35 @@ servWithCustom
    58 61
       :: Bool
    
    59 62
       -> MessageHook
    
    60 63
       -> Pipe
    
    61
    -  -> (forall a .IO a -> IO a)
    
    64
    +  -> (forall a. IO a -> IO a)
    
    62 65
       -> CustomMessageHandler
    
    63 66
       -> IO ()
    
    64
    -servWithCustom verbose hook pipe restore customHandler = newIORef False >>= loop
    
    67
    +servWithCustom verbose hook pipe restore customHandler = do
    
    68
    +  ref <- newIORef False
    
    69
    +  withThreadManager $ \mngr ->
    
    70
    +    loop ref mngr `finally` closePipe pipe
    
    65 71
      where
    
    66
    -  loop doneRef = do
    
    72
    +  loop doneRef mngr = do
    
    67 73
         when verbose $ trace "reading pipe..."
    
    68
    -    handleRemoteCall pipe hook $ \(msg :: Message a) -> do
    
    69 74
     
    
    70
    -      discardCtrlC
    
    75
    +    handleRemoteCall pipe hook $ \wr (msg :: Message a) ->
    
    76
    +
    
    77
    +      discardCtrlC                                 %>>= \_ ->
    
    78
    +      when verbose (trace ("msg: " ++ (show msg))) %>>= \_ ->
    
    71 79
     
    
    72
    -      when verbose $ trace ("msg: " ++ (show msg))
    
    73 80
           case msg of
    
    74
    -        CustomMessage tag payload -> handleCustom tag payload
    
    75
    -        Shutdown -> writeIORef doneRef True >> reply ()
    
    76
    -        -- TODO: These two should probably be done off the main thread
    
    77
    -        RunTH ii st q ty loc -> wrapRunTH ii $ runTH pipe ii st q ty loc
    
    78
    -        RunModFinalizers ii st qrefs -> wrapRunTH ii $ runModFinalizerRefs pipe ii st qrefs
    
    79
    -        _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
    
    80 89
     
    
    81 90
         isDone <- readIORef doneRef
    
    82
    -    if isDone then closePipe pipe
    
    83
    -              else loop doneRef
    
    91
    +    if isDone then pure ()
    
    92
    +              else loop doneRef mngr
    
    84 93
     
    
    85 94
       handleCustom tag payload = do
    
    86 95
         mresp <- customHandler tag payload
    
    ... ... @@ -92,7 +101,7 @@ servWithCustom verbose hook pipe restore customHandler = newIORef False >>= loop
    92 101
       reply :: forall a. Show a => a -> IO a
    
    93 102
       reply r = do
    
    94 103
         when verbose $ trace ("writing pipe: " ++ show r)
    
    95
    -    return r -- the result of the handler gets sent
    
    104
    +    return r -- the result of the handler is passed to writeReply
    
    96 105
     
    
    97 106
       -- Run some TH code, which may interact with GHC by sending
    
    98 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{..} = unsafe_linear \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
    +unsafe_linear :: (a -> b) %1 -> (a %1 -> b)
    
    62
    +unsafe_linear = Unsafe.unsafeCoerce (Unsafe.unsafeCoerce :: (a -> b) -> (a %1 -> b))

  • 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: