Andrea Vezzosi pushed to branch wip/romes/async-rpc at Glasgow Haskell Compiler / GHC

Commits:

5 changed files:

Changes:

  • compiler/GHC/Runtime/Interpreter/Process.hs
    ... ... @@ -64,7 +64,7 @@ sendTHComputation i msg reqHandler = mask $ \restore -> do
    64 64
       where
    
    65 65
         proc = instProcess i
    
    66 66
         handleOne mbox doneRef = do
    
    67
    -      handleRemoteTHCall (interpPipe proc) mbox $ \req -> do
    
    67
    +      handleRemoteTHCall (interpPipe proc) mbox $ \_setCancel req -> do
    
    68 68
             (stop, res) <- reqHandler req
    
    69 69
             writeIORef doneRef stop
    
    70 70
             return res
    

  • libraries/ghci/GHCi/Message.hs
    ... ... @@ -75,6 +75,7 @@ import GHC.Stack.CCS
    75 75
     import qualified GHC.Boot.TH.Syntax        as TH
    
    76 76
     import qualified GHC.Boot.TH.Monad         as TH
    
    77 77
     import GHCi.Message.Async
    
    78
    +import Control.Monad.STM
    
    78 79
     
    
    79 80
     -- -----------------------------------------------------------------------------
    
    80 81
     -- The RPC protocol between GHC and the interactive server
    
    ... ... @@ -666,12 +667,12 @@ remoteCall pipe msg = either throwIO (pure . runGet get)
    666 667
     -- | Handle a single 'Message' request on the interpreter and reply to the host
    
    667 668
     handleRemoteCall :: Pipe
    
    668 669
                      -> (Msg -> IO Msg) {-^ Msg hook -}
    
    669
    -                 -> (forall a. (Show a, Binary a) => WriteReply a %1 -> Message a -> IO r)
    
    670
    +                 -> (forall a. (Show a, Binary a) => WriteReply a %1 -> (IO () -> IO ()) -> Message a -> IO r)
    
    670 671
                      -> IO r
    
    671 672
     handleRemoteCall pipe hook k =
    
    672
    -  handleAsyncReq pipe getMessage Nothing \(WriteReply writer) msg0 -> IO \s0 ->
    
    673
    +  handleAsyncReq pipe getMessage Nothing \(WriteReply writer) sa msg0 -> IO \s0 ->
    
    673 674
         case hook msg0 |#> s0 of -- unIO for linearity checking
    
    674
    -      (# s1, Msg msg #) -> k (WriteReply \x -> writer (fmap put x)) msg |#> s1
    
    675
    +      (# s1, Msg msg #) -> k (WriteReply \x -> writer (fmap put x)) sa msg |#> s1
    
    675 676
     
    
    676 677
     -- | Send a 'THMessage' request to the host and wait for a reply from the host
    
    677 678
     -- to the interpreter.
    
    ... ... @@ -687,9 +688,9 @@ remoteTHCall pipe ii msg = either throwIO (pure . runGet get)
    687 688
     -- Symmterically to 'remoteTHCall', we always want to specify the 'InboxId' for
    
    688 689
     -- RunTH "reverse requests"
    
    689 690
     handleRemoteTHCall
    
    690
    -  :: Pipe -> InboxId -> (forall a. (Show a, Binary a) => THMessage a -> IO a) -> IO ()
    
    691
    +  :: Pipe -> InboxId -> (forall a. (Show a, Binary a) => (IO () -> IO ()) -> THMessage a -> IO a) -> IO ()
    
    691 692
     handleRemoteTHCall pipe ii k =
    
    692
    -  handleAsyncReq pipe getTHMessage (Just ii) \writer (THMsg msg) -> IO \s0 ->
    
    693
    -    case k msg |#> s0 of -- unIO for linearity checking
    
    693
    +  handleAsyncReq pipe getTHMessage (Just ii) \writer sa (THMsg msg) -> IO \s0 ->
    
    694
    +    case k sa msg |#> s0 of -- unIO for linearity checking
    
    694 695
           (# s1, a #) -> writeReply writer (put a) |#> s1
    
    695 696
     

  • libraries/ghci/GHCi/Message/Async.hs
    ... ... @@ -160,14 +160,19 @@ instance Exception PipeWriteClosedException
    160 160
     -- and identify whether they are requests or replies.
    
    161 161
     
    
    162 162
     writePipe :: Pipe -> IddMsg -> IO ()
    
    163
    -writePipe Pipe{..} msg = do
    
    164
    -  var <- newEmptyTMVarIO
    
    165
    -  atomically $ do
    
    163
    +writePipe p msg = do
    
    164
    +  var <- atomically $ writePipeAsync p msg
    
    165
    +  either throwIO pure =<< atomically (takeTMVar var)
    
    166
    +
    
    167
    +writePipeAsync :: Pipe -> IddMsg -> STM (TMVar (Either SomeException ()))
    
    168
    +writePipeAsync Pipe{..} msg = do
    
    169
    +    var <- newEmptyTMVar
    
    166 170
         readTVar pipeWriteClosed >>= \case
    
    167 171
           True -> throwSTM PipeWriteClosedException
    
    168 172
           False -> pure ()
    
    169 173
         writeTQueue (writeQueue pipeAsyncMngr) (msg, var)
    
    170
    -  either throwIO pure =<< atomically (takeTMVar var)
    
    174
    +    pure var
    
    175
    +
    
    171 176
     
    
    172 177
     -- | Only the 'drainPipe' thread should read from the pipe directly. Other
    
    173 178
     -- functions should wait for the matching reply to be delivered asynchronously.
    
    ... ... @@ -239,14 +244,18 @@ remoteAsyncReq pipe msg ii = do
    239 244
       -- Write request
    
    240 245
       writePipe pipe (Request uq ii (runPut msg))
    
    241 246
         `onException`
    
    242
    -      -- the req wasn't done, or no one will be reading the reply:
    
    243
    -      -- in either case, drop it from the pending map.
    
    244
    -      atomically (modifyTVar pendingReplies (IntMap.delete uq))
    
    247
    +        -- the req wasn't done, or no one will be reading the reply:
    
    248
    +        -- in either case, drop it from the pending map.
    
    249
    +        atomically (modifyTVar pendingReplies (IntMap.delete uq))
    
    245 250
     
    
    246 251
       -- Block waiting for reply
    
    247 252
       atomically (takeTMVar wv)
    
    248 253
         `onException`
    
    249
    -      atomically (modifyTVar pendingReplies (IntMap.delete uq))
    
    254
    +       atomically (do
    
    255
    +         modifyTVar pendingReplies (IntMap.delete uq)
    
    256
    +         -- we don't wait for Cancel to be written on the pipe,
    
    257
    +         -- and won't get a reply: nowhere to report it anyway.
    
    258
    +         writePipeAsync pipe (Cancel uq))
    
    250 259
     
    
    251 260
     -- | Receive a single asynchronous request and send a matching reply
    
    252 261
     -- asynchronously (the handler may fork a computation that eventually replies
    
    ... ... @@ -258,7 +267,7 @@ remoteAsyncReq pipe msg ii = do
    258 267
     -- branch, and if the handler otherwise throws, it is handled by 'handleAsyncReq'.
    
    259 268
     --
    
    260 269
     -- See Note [Exceptions during serialization] about exceptions the 'Put' argument might raise.
    
    261
    -handleAsyncReq :: Pipe -> Get a -> (Maybe InboxId) -> (WriteReply Put %1 -> a -> IO r) -> IO r
    
    270
    +handleAsyncReq :: Pipe -> Get a -> (Maybe InboxId) -> (WriteReply Put %1 -> (IO () -> IO ()) -> a -> IO r) -> IO r
    
    262 271
     handleAsyncReq pipe getit mii k = mask $ \restore -> do
    
    263 272
       let am = pipeAsyncMngr pipe
    
    264 273
       ack <- newEmptyMVar
    
    ... ... @@ -274,7 +283,7 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do
    274 283
           Nothing -> tryReadTQueue  $  requestsQueue am
    
    275 284
           Just ii -> tryReadTQueue =<< getMailbox am ii
    
    276 285
         case taken of
    
    277
    -      Just req -> pure (Just req)
    
    286
    +      Just req@(uq,_) -> fetchedRequest am uq >> pure (Just req)
    
    278 287
           Nothing  | isClosed  -> pure Nothing -- empty and closed
    
    279 288
                    | otherwise -> retry -- wait for msg
    
    280 289
     
    
    ... ... @@ -282,9 +291,9 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do
    282 291
         Nothing -> throwIO PipeReadClosedException -- can't expect reqs
    
    283 292
         Just (uq, bs) -> do
    
    284 293
           let reply p = do
    
    285
    -            writePipe pipe (Reply uq (bimap toSerializableException runPut p))
    
    286
    -            putMVar ack ()
    
    287
    -
    
    294
    +            b <- atomically $ doneHandling am uq
    
    295
    +            when b $ writePipe pipe (Reply uq (bimap toSerializableException runPut p))
    
    296
    +            putMVar ack () -- TODO: superfluous?
    
    288 297
           -- Run the continuation which must reply exactly once with 'writeReply'
    
    289 298
           case runGetOrFail getit bs of
    
    290 299
             Left (_, _, err) -> do
    
    ... ... @@ -292,7 +301,7 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do
    292 301
               reply (Left (toException exc))
    
    293 302
               throwIO exc
    
    294 303
             Right (_, _, it) ->
    
    295
    -          restore (k (WriteReply reply) it)
    
    304
    +          restore (k (WriteReply reply) (setActive am uq) it)
    
    296 305
     
    
    297 306
                 -- If there's an exception before the reply is sent, reply with an error instead.
    
    298 307
                 `catch` \(e :: SomeException) -> do
    
    ... ... @@ -306,6 +315,7 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do
    306 315
                         reply (Left e)
    
    307 316
                         throwIO e
    
    308 317
     
    
    318
    +
    
    309 319
     -- | 'handleSyncReq' failed to decode an incoming request payload using the given @'Get' a@
    
    310 320
     newtype DecodeAsyncReqException = DecodeAsyncReqException String deriving Show
    
    311 321
     instance Exception DecodeAsyncReqException
    
    ... ... @@ -347,6 +357,15 @@ getMailbox am (InboxId ii) = do
    347 357
     
    
    348 358
     -- ** Async matching "internals" -----------------------------------------------
    
    349 359
     
    
    360
    +-- | Requests go
    
    361
    +--   requestsQueue -> Fetched -> Active cancel -> gone
    
    362
    +--                         .. -> Canceling     -> gone
    
    363
    +-- Canceling is needed to avoid race condition in gap between Fetched and Active.
    
    364
    +data RequestStatus
    
    365
    +  = Fetched
    
    366
    +  | Active (IO ()) -- ^ must not be blocking.
    
    367
    +  | Canceling
    
    368
    +
    
    350 369
     data AsyncManager = AsyncManager
    
    351 370
       { pendingReply :: !(TVar (IntMap (TMVar (Either SomeException LB.ByteString))))
    
    352 371
         -- ^ When a request is sent, keep track that we're expecting a reply. The
    
    ... ... @@ -357,6 +376,9 @@ data AsyncManager = AsyncManager
    357 376
         -- /without/ a specific 'InboxId', queue it here. A request handler will
    
    358 377
         -- pop the next request from it. (Nothing except the 'drainPipe' thread
    
    359 378
         -- should read from the pipe). Read with @'handleAsyncReq' p get Nothing@.
    
    379
    +  , cancelStatus :: !(TVar (IntMap RequestStatus))
    
    380
    +    -- ^ When a `Cancel uq` is received, we lookup `uq` in this map and call the
    
    381
    +    -- callback if present.
    
    360 382
       , mailboxQueues :: !(TVar (IntMap (TQueue (CorrelationId, LB.ByteString))))
    
    361 383
         -- ^ Requests for a specific 'InboxId' that can be read using
    
    362 384
         -- @'handleAsyncReq' p get (Just <mailboxid>)@.
    
    ... ... @@ -389,15 +411,21 @@ type CorrelationId = Int
    389 411
     -- exclusively.
    
    390 412
     data IddMsg  = Request !CorrelationId !(Maybe InboxId) LB.ByteString
    
    391 413
                  | Reply   !CorrelationId (Either SerializableException LB.ByteString)
    
    414
    +             | Cancel  !CorrelationId -- !(Maybe InboxId) --? ^ Technically a
    
    415
    +               -- Request, but no Reply is expected, the CorrelationId is the
    
    416
    +               -- one of the Request being canceled.
    
    392 417
     
    
    393 418
     instance Binary IddMsg where
    
    394 419
       put (Request i ii bs) = putWord8 0 >> put i >> put ii >> put bs
    
    395 420
       put (Reply i bs)      = putWord8 1 >> put i >> put bs
    
    421
    +  put (Cancel i)        = putWord8 2 >> put i
    
    422
    +
    
    396 423
       get = do
    
    397 424
         b <- getWord8
    
    398 425
         case b of
    
    399 426
           0 -> Request <$> get <*> get <*> get
    
    400 427
           1 -> Reply   <$> get <*> get
    
    428
    +      2 -> Cancel  <$> get
    
    401 429
           _ -> fail $ "impossible: Binary IddMsg - " ++ show b
    
    402 430
     
    
    403 431
     newAsyncManager :: Pipe -> IO AsyncManager
    
    ... ... @@ -405,6 +433,7 @@ newAsyncManager pipe = do
    405 433
       asyncFreshId  <- newIORef 0
    
    406 434
       pendingReply  <- newTVarIO IntMap.empty
    
    407 435
       requestsQueue <- newTQueueIO
    
    436
    +  cancelStatus  <- newTVarIO IntMap.empty
    
    408 437
       mailboxQueues <- newTVarIO IntMap.empty
    
    409 438
       writeQueue <- newTQueueIO
    
    410 439
     
    
    ... ... @@ -430,41 +459,84 @@ drainPipe :: Pipe -> (forall x. IO x -> IO x) -> IO ()
    430 459
     drainPipe p unmask = catch (unmask $ loop `finally` pipeCloseRead p) replyToPendings  where
    
    431 460
       loop = do
    
    432 461
         readPipe p >>= \case
    
    433
    -      -- Receive a reply to a pending notification
    
    434
    -      Just (Reply uq bs) -> do
    
    435
    -        atomically $ do
    
    436
    -          m <- readTVar (pendingReply am)
    
    437
    -          case IntMap.lookup uq m of
    
    438
    -            Nothing ->
    
    439
    -              -- the req was dropped from pending bc there was an exception, and no
    
    440
    -              -- one's listening any more. the reply is ignored
    
    441
    -              pure ()
    
    442
    -            Just wv -> do
    
    443
    -              writeTVar (pendingReply am) (IntMap.delete uq m)
    
    444
    -              -- signal matching request
    
    445
    -              putTMVar wv (bimap (toException . RemoteProcessException) id bs)
    
    446
    -        loop
    
    447
    -      -- Receive a request and queue it
    
    448
    -      Just (Request uq mbi bs) -> do
    
    449
    -        atomically $ case mbi of
    
    450
    -          Nothing ->
    
    451
    -            -- "Global" request
    
    452
    -            writeTQueue (requestsQueue am) (uq,bs)
    
    453
    -          Just ii -> do
    
    454
    -            -- "Specific mailbox" request
    
    455
    -            q <- getMailbox am ii
    
    456
    -            writeTQueue q (uq,bs)
    
    462
    +      Just msg -> do
    
    463
    +        handleMsg msg
    
    457 464
             loop
    
    458 465
           -- EOF on read handle
    
    459 466
           Nothing -> do
    
    460 467
             -- clean up pipe then exit loop
    
    461 468
             replyToPendings (toException PipeReadClosedException)
    
    462 469
             pure ()
    
    470
    +  handleMsg = \case
    
    471
    +    -- Receive a reply to a pending notification
    
    472
    +    Reply uq bs -> do
    
    473
    +      atomically $ do
    
    474
    +        m <- readTVar (pendingReply am)
    
    475
    +        case IntMap.lookup uq m of
    
    476
    +          Nothing ->
    
    477
    +            -- the req was dropped from pending bc there was an exception, and no
    
    478
    +            -- one's listening any more. the reply is ignored
    
    479
    +            pure ()
    
    480
    +          Just wv -> do
    
    481
    +            writeTVar (pendingReply am) (IntMap.delete uq m)
    
    482
    +            -- signal matching request
    
    483
    +            putTMVar wv (bimap (toException . RemoteProcessException) id bs)
    
    484
    +    -- Receive a request and queue it
    
    485
    +    Request uq mbi bs -> do
    
    486
    +      atomically $ case mbi of
    
    487
    +        Nothing ->
    
    488
    +          -- "Global" request
    
    489
    +          writeTQueue (requestsQueue am) (uq,bs)
    
    490
    +        Just ii -> do
    
    491
    +          -- "Specific mailbox" request
    
    492
    +          q <- getMailbox am ii
    
    493
    +          writeTQueue q (uq,bs)
    
    494
    +    Cancel uq -> cancelRequest am uq
    
    463 495
     
    
    464 496
       replyToPendings e = atomically $ failPendingReplies p e
    
    465 497
     
    
    466 498
       am = pipeAsyncMngr p
    
    467 499
     
    
    500
    +cancelRequest :: AsyncManager -> CorrelationId -> IO ()
    
    501
    +cancelRequest am uq = join . atomically $ do
    
    502
    +  cs <- readTVar (cancelStatus am)
    
    503
    +  case IntMap.lookup uq cs of
    
    504
    +    Nothing -> pure (pure ())
    
    505
    +    Just Fetched -> writeTVar (cancelStatus am) (IntMap.insert uq Canceling cs) >> pure (pure ())
    
    506
    +    Just (Active cancel) -> writeTVar (cancelStatus am) (IntMap.delete uq cs) >> pure cancel
    
    507
    +    Just Canceling -> pure (pure ())
    
    508
    +
    
    509
    +doneHandling :: AsyncManager -> CorrelationId -> STM Bool
    
    510
    +doneHandling am uq = do
    
    511
    +  cs <- readTVar (cancelStatus am)
    
    512
    +  case IntMap.lookup uq cs of
    
    513
    +    Nothing -> pure False
    
    514
    +    Just s -> do
    
    515
    +      writeTVar (cancelStatus am) $ IntMap.delete uq cs
    
    516
    +      case s of
    
    517
    +        Canceling -> pure False
    
    518
    +        _         -> pure True
    
    519
    +
    
    520
    +setActive :: AsyncManager -> CorrelationId -> IO () -> IO ()
    
    521
    +setActive am uq cancel = join . atomically $ do
    
    522
    +  cs <- readTVar (cancelStatus am)
    
    523
    +  case IntMap.lookup uq cs of
    
    524
    +    Nothing -> pure (pure ()) -- reply already received.
    
    525
    +    Just Fetched -> writeTVar (cancelStatus am) (IntMap.insert uq (Active cancel) cs) >> pure (pure ())
    
    526
    +    Just (Active _) -> writeTVar (cancelStatus am) (IntMap.insert uq (Active cancel) cs) >> pure (pure ())
    
    527
    +    Just Canceling -> pure cancel
    
    528
    +
    
    529
    +fetchedRequest :: AsyncManager -> CorrelationId -> STM ()
    
    530
    +fetchedRequest am uq = do
    
    531
    +  cs <- readTVar (cancelStatus am)
    
    532
    +  case IntMap.lookup uq cs of
    
    533
    +    Nothing -> writeTVar (cancelStatus am) (IntMap.insert uq Fetched cs)
    
    534
    +    Just Fetched -> error "impossible"
    
    535
    +    Just (Active _) -> error "impossible"
    
    536
    +    Just Canceling -> error "impossible"
    
    537
    +
    
    538
    +
    
    539
    +
    
    468 540
     {-
    
    469 541
     Note [Exceptions during serialization]
    
    470 542
     -----------------------------------------------------------------------------
    
    ... ... @@ -553,7 +625,7 @@ runWriteReply (WriteReply r) = r
    553 625
     (|#>) (IO fs) s = fs s
    
    554 626
     
    
    555 627
     (%>>=) :: (IO a) -> (a -> IO b) %1 -> IO b
    
    556
    -(%>>=) = \ m f -> IO (\s0 ->
    
    628
    +(%>>=) = \ m -> \ f -> IO (\s0 ->
    
    557 629
             case m |#> s0 of -- unIO for linearity checking
    
    558 630
               (# s1, x #) -> f x |#> s1)
    
    559 631
     infixl 1 %>>=
    

  • libraries/ghci/GHCi/Server.hs
    ... ... @@ -39,6 +39,7 @@ import System.Exit
    39 39
     import System.IO
    
    40 40
     
    
    41 41
     import Control.Concurrent
    
    42
    +import Control.Monad.STM
    
    42 43
     import GHCi.Message.Async (runWriteReply, (%>>=))
    
    43 44
     import GHCi.Server.Threads
    
    44 45
     
    
    ... ... @@ -66,13 +67,13 @@ servWithCustom
    66 67
       -> IO ()
    
    67 68
     servWithCustom verbose hook pipe restore customHandler = do
    
    68 69
       ref <- newIORef False
    
    69
    -  withThreadManager $ \mngr ->
    
    70
    +  withThreadManager $ \mngr -> do
    
    70 71
         loop ref mngr `finally` closePipe pipe
    
    71 72
      where
    
    72 73
       loop doneRef mngr = do
    
    73 74
         when verbose $ trace "reading pipe..."
    
    74 75
     
    
    75
    -    handleRemoteCall pipe hook $ \wr (msg :: Message a) ->
    
    76
    +    handleRemoteCall pipe hook $ \wr setCancel (msg :: Message a) ->
    
    76 77
     
    
    77 78
           discardCtrlC                                 %>>= \_ ->
    
    78 79
           when verbose (trace ("msg: " ++ (show msg))) %>>= \_ ->
    
    ... ... @@ -83,8 +84,10 @@ servWithCustom verbose hook pipe restore customHandler = do
    83 84
                                          reply ()                 %>>= writeReply wr
    
    84 85
             RunTH ii st q ty loc      -> spawnFinally mngr         (runWriteReply wr)
    
    85 86
                                          (wrapRunTH ii (runTH pipe ii st q ty loc))
    
    87
    +                                     (setCancel . void . forkIO . killThread)
    
    86 88
             RunModFinalizers ii st qs -> spawnFinally mngr         (runWriteReply wr)
    
    87 89
                                          (wrapRunTH ii (runModFinalizerRefs pipe ii st qs))
    
    90
    +                                     (setCancel . void . forkIO . killThread)
    
    88 91
             _other                    -> run msg >>= reply        %>>= writeReply wr
    
    89 92
     
    
    90 93
         isDone <- readIORef doneRef
    

  • libraries/ghci/GHCi/Server/Threads.hs
    ... ... @@ -41,8 +41,8 @@ withThreadManager k = do
    41 41
     
    
    42 42
     -- | Linear and resource-safe because 'ThreadManager' is guaranteed to wait or
    
    43 43
     -- cancel all threads.
    
    44
    -spawnFinally :: ThreadManager -> (Either SomeException a -> IO ()) %1 -> IO a -> IO ()
    
    45
    -spawnFinally ThreadManager{..} = unsafe_linear \fin -> \action -> void $
    
    44
    +spawnFinally :: ThreadManager -> (Either SomeException a -> IO ()) %1 -> IO a -> (ThreadId -> IO b) -> IO b
    
    45
    +spawnFinally ThreadManager{..} = unsafe_linear \fin -> \action -> \k ->
    
    46 46
       forkFinally
    
    47 47
         ( mask $ \unmask -> do
    
    48 48
             tid <- myThreadId
    
    ... ... @@ -57,6 +57,7 @@ spawnFinally ThreadManager{..} = unsafe_linear \fin -> \action -> void $
    57 57
               else throwIO ThreadKilled
    
    58 58
         )
    
    59 59
         ( \ea -> fin ea )
    
    60
    +    >>= k
    
    60 61
     
    
    61 62
     unsafe_linear :: (a -> b) %1 -> (a %1 -> b)
    
    62 63
     unsafe_linear = Unsafe.unsafeCoerce (Unsafe.unsafeCoerce :: (a -> b) -> (a %1 -> b))