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

Commits:

8 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
    

  • compiler/GHC/Tc/Errors/Types.hs
    ... ... @@ -7091,6 +7091,7 @@ data SpliceFailReason
    7091 7091
                        th/T5976
    
    7092 7092
                        th/T7276a
    
    7093 7093
                        th/T8987
    
    7094
    +                   th/T8987b
    
    7094 7095
                        th/TH_exn1
    
    7095 7096
                        th/TH_exn2
    
    7096 7097
                        th/TH_runIO
    

  • compiler/GHC/Tc/Gen/Splice.hs
    ... ... @@ -1472,11 +1472,9 @@ when showing an error message.
    1472 1472
     To call runQ in the Tc monad, we need to make TcM an instance of Quasi:
    
    1473 1473
     -}
    
    1474 1474
     
    
    1475
    --- 'msg' is forced to ensure exceptions don't escape,
    
    1476
    --- see Note [Exceptions in TH]
    
    1477 1475
     report :: Bool -> [Char] -> TcM ()
    
    1478
    -report True msg  = seqList msg $ addErr        $ TcRnTHError $ ReportCustomQuasiError True  msg
    
    1479
    -report False msg = seqList msg $ addDiagnostic $ TcRnTHError $ ReportCustomQuasiError False msg
    
    1476
    +report True  msg = addErr        $ TcRnTHError $ ReportCustomQuasiError True  msg
    
    1477
    +report False msg = addDiagnostic $ TcRnTHError $ ReportCustomQuasiError False msg
    
    1480 1478
     
    
    1481 1479
     addTopDecls :: [TH.Dec] -> TcM ()
    
    1482 1480
     addTopDecls thds = do
    
    ... ... @@ -1575,7 +1573,7 @@ metaHandlersTcM runInIO = TH.MetaHandlers {
    1575 1573
                           ; let i = toInteger (getKey u)
    
    1576 1574
                           ; return (TH.mkNameU s i) }
    
    1577 1575
     
    
    1578
    -    , mReport = fmap runInIO . report
    
    1576
    +    , mReport = \ b msg -> runInIO (report b msg)
    
    1579 1577
     
    
    1580 1578
         , mLocation = runInIO location
    
    1581 1579
     
    

  • 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.
    
    ... ... @@ -91,7 +95,13 @@ mkPipeFromHandles pipeRead pipeWrite = do
    91 95
     
    
    92 96
     -- | Make a 'Pipe' from a reader function and a writer function.
    
    93 97
     -- The pipe should be closed with 'closePipe', which will call 'pipeCloseRead' and 'pipeCloseWrite'.
    
    94
    -mkPipeFromContinuations :: IO ByteString -> (B.Builder -> IO ())
    
    98
    +--
    
    99
    +-- Arguments 'getSome' and 'putAll' should throw an `IOException` when communication fails
    
    100
    +-- or is no longer possible.
    
    101
    +mkPipeFromContinuations :: IO ByteString
    
    102
    +                        -- ^ getSome
    
    103
    +                        -> (B.Builder -> IO ())
    
    104
    +                        -- ^ putAll
    
    95 105
                             -> IO ()
    
    96 106
                             -- ^ pipeCloseRead
    
    97 107
                             -> IO ()
    
    ... ... @@ -101,6 +111,7 @@ mkPipeFromContinuations getSome putAll pipeCloseRead pipeCloseWrite = mdo
    101 111
       pipeLeftovers   <- newIORef Nothing
    
    102 112
       pipeLock        <- newMVar ()
    
    103 113
       pipeReadClosed  <- newTVarIO False
    
    114
    +  pipeWriteClosed  <- newTVarIO False
    
    104 115
       mdo
    
    105 116
         pipeAsyncMngr <- newAsyncManager p
    
    106 117
         let p = Pipe { .. }
    
    ... ... @@ -114,20 +125,34 @@ mkPipeFromContinuations getSome putAll pipeCloseRead pipeCloseWrite = mdo
    114 125
     -- write. Only then do we stop the drain thread, which closes the read end
    
    115 126
     -- itself once its read has finished. On Windows its read can't be
    
    116 127
     -- interrupted, so this waits for the other side to close its write end too.
    
    128
    +-- Finally we stop the write thread, which could be blocked waiting for more to write.
    
    117 129
     closePipe :: Pipe -> IO ()
    
    118 130
     closePipe p@Pipe{..} = do
    
    119 131
       readAlreadyClosed <- atomically $ do
    
    120 132
         closed <- readTVar pipeReadClosed
    
    121 133
         failPendingReplies p (toException PipeReadClosedException)
    
    122 134
         pure closed
    
    123
    -  withLock p pipeCloseWrite `finally`
    
    124
    -    unless readAlreadyClosed
    
    125
    -      (throwTo (asyncDrainTid pipeAsyncMngr) PipeReadClosedException)
    
    135
    +
    
    136
    +  writeAlreadyClosed <- atomically $ do
    
    137
    +    closed <- readTVar pipeWriteClosed
    
    138
    +    failPendingWrites p (toException PipeWriteClosedException)
    
    139
    +    pure closed
    
    140
    +
    
    141
    +  unless writeAlreadyClosed (withLock p pipeCloseWrite)
    
    142
    +    `finally` do
    
    143
    +      unless readAlreadyClosed
    
    144
    +        (throwTo (asyncDrainTid pipeAsyncMngr) PipeReadClosedException)
    
    145
    +      unless writeAlreadyClosed
    
    146
    +        (throwTo (asyncFillTid pipeAsyncMngr) PipeWriteClosedException)
    
    126 147
     
    
    127 148
     -- | We've closed the pipe read end on this process
    
    128 149
     data PipeReadClosedException = PipeReadClosedException deriving Show
    
    129 150
     instance Exception PipeReadClosedException
    
    130 151
     
    
    152
    +-- | We've closed the pipe write end on this process
    
    153
    +data PipeWriteClosedException = PipeWriteClosedException deriving Show
    
    154
    +instance Exception PipeWriteClosedException
    
    155
    +
    
    131 156
     --------------------------------------------------------------------------------
    
    132 157
     -- ** Pipe thread-safe read/write
    
    133 158
     --------------------------------------------------------------------------------
    
    ... ... @@ -135,7 +160,14 @@ instance Exception PipeReadClosedException
    135 160
     -- and identify whether they are requests or replies.
    
    136 161
     
    
    137 162
     writePipe :: Pipe -> IddMsg -> IO ()
    
    138
    -writePipe p@Pipe{..} = withLock p . putAll . execPut . put
    
    163
    +writePipe Pipe{..} msg = do
    
    164
    +  var <- newEmptyTMVarIO
    
    165
    +  atomically $ do
    
    166
    +    readTVar pipeWriteClosed >>= \case
    
    167
    +      True -> throwSTM PipeWriteClosedException
    
    168
    +      False -> pure ()
    
    169
    +    writeTQueue (writeQueue pipeAsyncMngr) (msg, var)
    
    170
    +  either throwIO pure =<< atomically (takeTMVar var)
    
    139 171
     
    
    140 172
     -- | Only the 'drainPipe' thread should read from the pipe directly. Other
    
    141 173
     -- functions should wait for the matching reply to be delivered asynchronously.
    
    ... ... @@ -181,6 +213,8 @@ withLock Pipe{..} = bracket (takeMVar pipeLock) (putMVar pipeLock) . const
    181 213
     --------------------------------------------------------------------------------
    
    182 214
     
    
    183 215
     -- | Send an asynchronous request and block waiting for a matching reply
    
    216
    +--
    
    217
    +-- See Note [Exceptions during serialization] about exceptions the 'Put' argument might raise.
    
    184 218
     remoteAsyncReq :: Pipe -> Put -> (Maybe InboxId) -> IO (Either SomeException LB.ByteString)
    
    185 219
     remoteAsyncReq pipe msg ii = do
    
    186 220
     
    
    ... ... @@ -222,6 +256,8 @@ remoteAsyncReq pipe msg ii = do
    222 256
     -- It must be used exactly once (all requests expect a reply, and you can't
    
    223 257
     -- reply more than once). Linear types enforce the handler replies in every
    
    224 258
     -- branch, and if the handler otherwise throws, it is handled by 'handleAsyncReq'.
    
    259
    +--
    
    260
    +-- See Note [Exceptions during serialization] about exceptions the 'Put' argument might raise.
    
    225 261
     handleAsyncReq :: Pipe -> Get a -> (Maybe InboxId) -> (WriteReply Put %1 -> a -> IO r) -> IO r
    
    226 262
     handleAsyncReq pipe getit mii k = mask $ \restore -> do
    
    227 263
       let am = pipeAsyncMngr pipe
    
    ... ... @@ -229,6 +265,10 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do
    229 265
     
    
    230 266
       -- Block waiting for request
    
    231 267
       mbreq <- atomically $ do
    
    268
    +    readTVar (pipeWriteClosed pipe) >>= \case
    
    269
    +      -- We will not be able to write the reply, so no point in continuiting.
    
    270
    +      True -> throwSTM PipeWriteClosedException
    
    271
    +      False -> pure ()
    
    232 272
         isClosed <- readTVar (pipeReadClosed pipe)
    
    233 273
         taken <- case mii of
    
    234 274
           Nothing -> tryReadTQueue  $  requestsQueue am
    
    ... ... @@ -320,7 +360,10 @@ data AsyncManager = AsyncManager
    320 360
       , mailboxQueues :: !(TVar (IntMap (TQueue (CorrelationId, LB.ByteString))))
    
    321 361
         -- ^ Requests for a specific 'InboxId' that can be read using
    
    322 362
         -- @'handleAsyncReq' p get (Just <mailboxid>)@.
    
    363
    +  , writeQueue    :: !(TQueue (IddMsg, TMVar (Either SomeException ())))
    
    364
    +    -- ^ Messages to write on the pipe.
    
    323 365
       , asyncDrainTid :: !ThreadId
    
    366
    +  , asyncFillTid  :: !ThreadId
    
    324 367
       , asyncFreshId  :: !(IORef Int)
    
    325 368
       }
    
    326 369
     
    
    ... ... @@ -363,11 +406,16 @@ newAsyncManager pipe = do
    363 406
       pendingReply  <- newTVarIO IntMap.empty
    
    364 407
       requestsQueue <- newTQueueIO
    
    365 408
       mailboxQueues <- newTVarIO IntMap.empty
    
    409
    +  writeQueue <- newTQueueIO
    
    366 410
     
    
    367 411
       -- Fork thread to drain read end.
    
    368 412
       -- (cleaned up on 'closePipe')
    
    369 413
       asyncDrainTid <- forkIOWithUnmask (drainPipe pipe)
    
    370 414
     
    
    415
    +  -- Fork thread to fill write end.
    
    416
    +  -- (cleaned up on 'closePipe')
    
    417
    +  asyncFillTid <- forkIOWithUnmask (fillPipe pipe)
    
    418
    +
    
    371 419
       let am = AsyncManager{..}
    
    372 420
       pure am
    
    373 421
     
    
    ... ... @@ -417,6 +465,55 @@ drainPipe p unmask = catch (unmask $ loop `finally` pipeCloseRead p) replyToPend
    417 465
     
    
    418 466
       am = pipeAsyncMngr p
    
    419 467
     
    
    468
    +{-
    
    469
    +Note [Exceptions during serialization]
    
    470
    +-----------------------------------------------------------------------------
    
    471
    +
    
    472
    +For efficiency reasons, any `IddMsg` to be sent is forced only when already
    
    473
    +writing onto the Pipe, so we cannot discern whether any raised exception
    
    474
    +originated due to forcing during serialization or from interacting with the
    
    475
    +Pipe.
    
    476
    +
    
    477
    +If the exceptions happen to be `SomeAsyncException` or `IOException` the write
    
    478
    +end of the `Pipe` will be closed while rethrowing them, as those are likely to
    
    479
    +indicate something is wrong with the `Pipe` itself. Same for
    
    480
    +PipeWriteClosedException, used by `closePipe`.
    
    481
    +
    
    482
    +It is recommended to fully force messages at the source, to avoid needlessly
    
    483
    +interrupting communication.
    
    484
    +-}
    
    485
    +
    
    486
    +
    
    487
    +-- | "Fill"s write end of the Pipe with queued messages.
    
    488
    +--
    
    489
    +-- Because Pipe is written to from this thread rather than 'writePipe', calls to
    
    490
    +-- the latter can be interrupted without compromising the protocol.
    
    491
    +--
    
    492
    +-- Closes the Pipe write end when stopping, notifying any messages still queued to write.
    
    493
    +fillPipe :: Pipe -> (forall x. IO x -> IO x) -> IO ()
    
    494
    +fillPipe p unmask = catch (loop `finally` pipeCloseWrite p) (atomically . failPendingWrites p) where
    
    495
    +  loop = do
    
    496
    +    (msg,var) <- atomically $ readTQueue (writeQueue am)
    
    497
    +    success <- handle (\ e -> atomically (putTMVar var (Left e)) >> rethrowIfPipeFatal e >> pure False) $ do
    
    498
    +      unmask $ withLock p (putAll p . execPut . put $ msg)
    
    499
    +      pure True
    
    500
    +    when success $ atomically $ putTMVar var (Right ())
    
    501
    +    loop
    
    502
    +
    
    503
    +  -- See Note [Exceptions during serialization]
    
    504
    +  rethrowIfPipeFatal e
    
    505
    +    | Just SomeAsyncException{}     <- fromException e = throwIO e
    
    506
    +    | Just (_ :: IOException)       <- fromException e = throwIO e
    
    507
    +    | Just PipeWriteClosedException <- fromException e = throwIO e
    
    508
    +    | otherwise = pure ()
    
    509
    +  am = pipeAsyncMngr p
    
    510
    +
    
    511
    +failPendingWrites :: Pipe -> SomeException -> STM ()
    
    512
    +failPendingWrites p e = do
    
    513
    +  writeTVar (pipeWriteClosed p) True
    
    514
    +  writes <- flushTQueue (writeQueue (pipeAsyncMngr p))
    
    515
    +  mapM_ (`writeTMVar` Left e) (map snd writes)
    
    516
    +
    
    420 517
     failPendingReplies :: Pipe -> SomeException -> STM ()
    
    421 518
     failPendingReplies p e = do
    
    422 519
       -- reqs are only added if pipe not closed;
    

  • libraries/template-haskell/Language/Haskell/TH/Syntax.hs
    ... ... @@ -528,7 +528,9 @@ class (MonadIO m, MonadFail m) => Quasi m where
    528 528
       -- | Report an error (True) or warning (False)
    
    529 529
       -- ...but carry on; use 'fail' to stop. See 'report'.
    
    530 530
       qReport  :: Bool -> String -> m ()
    
    531
    -  qReport b s = qRunQ $ report b s
    
    531
    +  -- 'msg' is forced to ensure exceptions don't escape,
    
    532
    +  -- see Note [Exceptions in TH]
    
    533
    +  qReport b msg = seqList msg $ qRunQ $ Internal.report b msg
    
    532 534
     
    
    533 535
       -- | See 'recover'.
    
    534 536
       qRecover :: m a -- ^ the error handler
    
    ... ... @@ -665,13 +667,18 @@ instance Quasi Q where
    665 667
     -- | Report an error (True) or warning (False),
    
    666 668
     -- but carry on; use 'fail' to stop.
    
    667 669
     report  :: Bool -> String -> Q ()
    
    668
    -report = Internal.report
    
    670
    +report = qReport
    
    669 671
     {-# DEPRECATED report "Use reportError or reportWarning instead" #-} -- deprecated in 7.6
    
    670 672
     
    
    671 673
     -- | Report an error to the user, but allow the current splice's computation to carry on. To abort the computation, use 'fail'.
    
    672 674
     reportError :: String -> Q ()
    
    673
    -reportError = report True
    
    675
    +reportError = qReport True
    
    674 676
     
    
    675 677
     -- | Report a warning to the user, and carry on.
    
    676 678
     reportWarning :: String -> Q ()
    
    677
    -reportWarning = report False
    679
    +reportWarning = qReport False
    
    680
    +
    
    681
    +-- | copied from GHC.Utils.Misc
    
    682
    +seqList :: [a] -> b -> b
    
    683
    +seqList [] b = b
    
    684
    +seqList (x:xs) b = x `seq` seqList xs b

  • testsuite/tests/th/T8987b.hs
    1
    +{-# LANGUAGE TemplateHaskell #-}
    
    2
    +
    
    3
    +module T8987b where
    
    4
    +import Language.Haskell.TH
    
    5
    +import System.IO.Error
    
    6
    +import Control.Exception
    
    7
    +
    
    8
    +$(reportWarning ['1', throw (userError "IOError in warning")] >> return [])

  • testsuite/tests/th/T8987b.stderr
    1
    +T8987b.hs:1:1: error: [GHC-87897]
    
    2
    +    Exception when trying to run compile-time code:
    
    3
    +      user error (IOError in warning)
    
    4
    +    Code: (reportWarning ['1', throw (userError "IOError in warning")]
    
    5
    +             >> return [])
    
    6
    +

  • testsuite/tests/th/all.T
    ... ... @@ -306,6 +306,7 @@ test('T8884', normal, compile, ['-v0'])
    306 306
     test('T8954', normal, compile, ['-v0'])
    
    307 307
     test('T8932', normal, compile_fail, ['-v0'])
    
    308 308
     test('T8987', normal, compile_fail, ['-v0'])
    
    309
    +test('T8987b', normal, compile_fail, ['-v0'])
    
    309 310
     test('T7241', normal, compile_fail, ['-v0'])
    
    310 311
     test('T9262', normal, compile, ['-v0'])
    
    311 312
     test('T9199', normal, compile, ['-v0'])