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

Commits:

2 changed files:

Changes:

  • compiler/GHC/Runtime/Interpreter.hs
    ... ... @@ -197,9 +197,7 @@ interpCmd interp msg = case interpInstance interp of
    197 197
       InternalInterp     -> run msg -- Just run it directly
    
    198 198
     #endif
    
    199 199
       ExternalInterp ext -> withExtInterp ext $ \inst ->
    
    200
    -    uninterruptibleMask_ $ -- Note [uninterruptibleMask_ and interpCmd]
    
    201
    -      sendMessage inst msg
    
    202
    -
    
    200
    +    sendMessage inst msg
    
    203 201
     
    
    204 202
     withExtInterp :: ExceptionMonad m => ExtInterp -> (forall d. ExtInterpInstance d -> m a) -> m a
    
    205 203
     withExtInterp ext action = case ext of
    
    ... ... @@ -213,15 +211,6 @@ withExtInterpStatus ext action = case ext of
    213 211
       ExtWasm  i -> action $ interpStatus i
    
    214 212
       ExtIServ i -> action (interpStatus i)
    
    215 213
     
    
    216
    --- Note [uninterruptibleMask_ and interpCmd]
    
    217
    --- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    218
    --- If we receive an async exception, such as ^C, while communicating
    
    219
    --- with the iserv process then we will be out-of-sync and not be able
    
    220
    --- to recover.  Thus we use uninterruptibleMask_ during
    
    221
    --- communication.  A ^C will be delivered to the iserv process (because
    
    222
    --- signals get sent to the whole process group) which will interrupt
    
    223
    --- the running computation and return an EvalException result.
    
    224
    -
    
    225 214
     -- | Grab a lock on the 'IServ' and do something with it.
    
    226 215
     -- Overloaded because this is used from TcM as well as IO.
    
    227 216
     withIServ
    

  • libraries/ghci/GHCi/Message/Async.hs
    ... ... @@ -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;