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

Commits:

17 changed files:

Changes:

  • changelog.d/T27960
    1
    +section: ghci
    
    2
    +synopsis: Make external interpreter communication use an explicit Async RPC
    
    3
    +issues: #27960
    
    4
    +mrs: !16802
    
    5
    +
    
    6
    +description: {
    
    7
    +    iserv already implements RPC today, although in a fairly ad-hoc and
    
    8
    +    convoluted manner. The template haskell consumer already requires
    
    9
    +    bidirectional communication and async requests, and got implemented in a
    
    10
    +    way that precludes other consumers with async needs.
    
    11
    +    This change streamlines all this by using an explicit Async RPC protocol.
    
    12
    +
    
    13
    +    It additionally clears the path to fix #18852 since the main thread is no
    
    14
    +    longer blocked per request, enables evaluating splices concurrently in the
    
    15
    +    external interpreter, and unblocks multi-threaded interactive debugging.
    
    16
    +}
    
    17
    +

  • compiler/GHC/Runtime/Interpreter.hs
    ... ... @@ -626,11 +626,9 @@ spawnIServ conf = do
    626 626
                                               []
    
    627 627
                                               (iservConfOpts    conf)
    
    628 628
       interpPipe <- mkPipeFromHandles rh wh
    
    629
    -  lock <- newMVar ()
    
    630 629
       let process = InterpProcess
    
    631 630
                       { interpHandle = ph
    
    632 631
                       , interpPipe
    
    633
    -                  , interpLock   = lock
    
    634 632
                       }
    
    635 633
     
    
    636 634
       pending_frees <- newMVar []
    
    ... ... @@ -655,7 +653,9 @@ stopInterp interp = case interpInstance interp of
    655 653
                 ex <- getProcessExitCode (interpHandle (instProcess i))
    
    656 654
                 if isJust ex
    
    657 655
                    then pure ()
    
    658
    -               else sendMessage i Shutdown
    
    656
    +               else sendMessage i Shutdown `catch` \ PipeReadClosedException -> pure ()
    
    657
    +            -- We asked the server to shutdown, so we will also shutdown any communication.
    
    658
    +            closePipe (interpPipe (instProcess i))
    
    659 659
                 pure InterpPending
    
    660 660
     
    
    661 661
     -- -----------------------------------------------------------------------------
    

  • compiler/GHC/Runtime/Interpreter/JS.hs
    ... ... @@ -127,11 +127,9 @@ startTHRunnerProcess interp_js settings = do
    127 127
       std_in <- readIORef interp_in
    
    128 128
     
    
    129 129
       interpPipe <- mkPipeFromHandles rh wh
    
    130
    -  lock <- newMVar ()
    
    131 130
       let proc = InterpProcess
    
    132 131
                   { interpHandle = hdl
    
    133 132
                   , interpPipe
    
    134
    -              , interpLock   = lock
    
    135 133
                   }
    
    136 134
       pure (std_in, proc)
    
    137 135
     
    
    ... ... @@ -382,7 +380,7 @@ jsLinkPlan logger tmpfs tmp_dir ar_cache link_cfg cfg inst link_plan = do
    382 380
       --   Haskell server.
    
    383 381
       server_started <- jsServerStarted <$> readMVar (instJSState (instExtra inst))
    
    384 382
       if server_started
    
    385
    -    then sendMessageNoResponse inst $ LoadObj all_js
    
    383
    +    then sendMessage inst $ LoadObj all_js
    
    386 384
         else jsLoadFile            inst all_js
    
    387 385
     
    
    388 386
       ----------------------------------------------------------------
    

  • compiler/GHC/Runtime/Interpreter/Process.hs
    ... ... @@ -2,16 +2,8 @@ module GHC.Runtime.Interpreter.Process
    2 2
       (
    
    3 3
       -- * Message API
    
    4 4
         Message(..)
    
    5
    -  , DelayedResponse (..)
    
    6
    -  -- * Top-level message API (these acquire/release a lock)
    
    7 5
       , sendMessage
    
    8
    -  , sendMessageNoResponse
    
    9
    -  , sendMessageDelayedResponse
    
    10
    -  , receiveDelayedResponse
    
    11
    -  -- * Nested message API (these require the interpreter to already be locked)
    
    12
    -  , sendAnyValue
    
    13
    -  , receiveAnyValue
    
    14
    -  , receiveTHMessage
    
    6
    +  , sendTHComputation
    
    15 7
       )
    
    16 8
     where
    
    17 9
     
    
    ... ... @@ -27,97 +19,63 @@ import GHC.Utils.Exception as Ex
    27 19
     import Data.Binary
    
    28 20
     import System.Exit
    
    29 21
     import System.Process
    
    30
    -import Control.Concurrent.MVar (MVar, withMVar, takeMVar, putMVar, isEmptyMVar)
    
    31
    -
    
    32
    -data DelayedResponse a = DelayedResponse
    
    22
    +import Control.Concurrent
    
    23
    +import Data.IORef
    
    24
    +import Control.Concurrent.STM
    
    33 25
     
    
    34 26
     -- -----------------------------------------------------------------------------
    
    35 27
     -- Top-level Message API
    
    36 28
     
    
    37
    --- | Send a message to the interpreter process that doesn't expect a response
    
    38
    ---   (locks the interpreter while sending)
    
    39
    -sendMessageNoResponse :: ExtInterpInstance d -> Message () -> IO ()
    
    40
    -sendMessageNoResponse i m =
    
    41
    -  withLock i $ writeInterpProcess (instProcess i) (putMessage m)
    
    42
    -
    
    43
    --- | Send a message to the interpreter that expects a response
    
    44
    ---   (locks the interpreter while until the response is received)
    
    29
    +-- | Send a message to the interpreter and expect a response, blocking until it arrives.
    
    30
    +--
    
    31
    +-- This issues an asynchronous RPC on the external interpreter which does not
    
    32
    +-- block the external interpreter thread. We block here on the host because we
    
    33
    +-- wait for the asynchronous response to arrive.
    
    45 34
     sendMessage :: Binary a => ExtInterpInstance d -> Message a -> IO a
    
    46
    -sendMessage i m = withLock i $ callInterpProcess (instProcess i) m
    
    47
    -
    
    48
    --- | Send a message to the interpreter process whose response is expected later
    
    35
    +sendMessage i msg =
    
    36
    +  remoteCall (interpPipe proc) msg
    
    37
    +    `catchException` \(e :: SomeException) -> handleInterpProcessFailure proc e
    
    38
    +  where
    
    39
    +    proc = instProcess i
    
    40
    +
    
    41
    +-- | Send a message with a TH computation to the ext. interpreter (like
    
    42
    +-- 'RunTH', or 'RunModFinalizers') and handle 'THMessage' requests from the
    
    43
    +-- ext. interpreter to us in the meantime.
    
    49 44
     --
    
    50
    --- This is useful to avoid forgetting to receive the value and to ensure that
    
    51
    --- the type of the response isn't lost. Use receiveDelayedResponse to read it.
    
    52
    --- (locks the interpreter until the response is received using
    
    53
    --- `receiveDelayedResponse`)
    
    54
    -sendMessageDelayedResponse :: ExtInterpInstance d -> Message a -> IO (DelayedResponse a)
    
    55
    -sendMessageDelayedResponse i m = do
    
    56
    -  lock i
    
    57
    -  writeInterpProcess (instProcess i) (putMessage m)
    
    58
    -  pure DelayedResponse
    
    59
    -
    
    60
    --- | Expect a delayed result to be received now
    
    61
    -receiveDelayedResponse :: Binary a => ExtInterpInstance d -> DelayedResponse a -> IO a
    
    62
    -receiveDelayedResponse i DelayedResponse = do
    
    63
    -  ensureLocked i
    
    64
    -  r <- readInterpProcess (instProcess i) get
    
    65
    -  unlock i
    
    66
    -  pure r
    
    67
    -
    
    68
    --- -----------------------------------------------------------------------------
    
    69
    --- Nested Message API
    
    70
    -
    
    71
    --- | Send any value (requires locked interpreter)
    
    72
    -sendAnyValue :: Binary a => ExtInterpInstance d -> a -> IO ()
    
    73
    -sendAnyValue i m = ensureLocked i >> writeInterpProcess (instProcess i) (put m)
    
    74
    -
    
    75
    --- | Expect a value to be received (requires locked interpreter)
    
    76
    -receiveAnyValue :: ExtInterpInstance d -> Get a -> IO a
    
    77
    -receiveAnyValue i get = ensureLocked i >> readInterpProcess (instProcess i) get
    
    78
    -
    
    79
    --- | Wait for a Template Haskell message (requires locked interpreter)
    
    80
    -receiveTHMessage :: ExtInterpInstance d -> IO THMsg
    
    81
    -receiveTHMessage i = ensureLocked i >> receiveAnyValue i getTHMessage
    
    82
    -
    
    83
    --- -----------------------------------------------------------------------------
    
    84
    -
    
    85
    -getLock :: ExtInterpInstance d -> MVar ()
    
    86
    -getLock = interpLock . instProcess
    
    87
    -
    
    88
    -withLock :: ExtInterpInstance d -> IO a -> IO a
    
    89
    -withLock i f = withMVar (getLock i) (const f)
    
    90
    -
    
    91
    -lock :: ExtInterpInstance d -> IO ()
    
    92
    -lock i = takeMVar (getLock i)
    
    93
    -
    
    94
    -unlock :: ExtInterpInstance d -> IO ()
    
    95
    -unlock i = putMVar (getLock i) ()
    
    96
    -
    
    97
    -ensureLocked :: ExtInterpInstance d -> IO ()
    
    98
    -ensureLocked i =
    
    99
    -  isEmptyMVar (getLock i) >>= \case
    
    100
    -    False -> panic "ensureLocked: external interpreter not locked"
    
    101
    -    _     -> pure ()
    
    102
    -
    
    103
    -
    
    104
    --- | Send a 'Message' and receive the response from the interpreter process
    
    105
    -callInterpProcess :: Binary a => InterpProcess -> Message a -> IO a
    
    106
    -callInterpProcess i msg =
    
    107
    -  remoteCall (interpPipe i) msg
    
    108
    -    `catchException` \(e :: SomeException) -> handleInterpProcessFailure i e
    
    109
    -
    
    110
    --- | Read a value from the interpreter process
    
    111
    -readInterpProcess :: InterpProcess -> Get a -> IO a
    
    112
    -readInterpProcess i get =
    
    113
    -  readPipe (interpPipe i) get
    
    114
    -    `catchException` \(e :: SomeException) -> handleInterpProcessFailure i e
    
    115
    -
    
    116
    --- | Send a value to the interpreter process
    
    117
    -writeInterpProcess :: InterpProcess -> Put -> IO ()
    
    118
    -writeInterpProcess i put =
    
    119
    -  writePipe (interpPipe i) put
    
    120
    -    `catchException` \(e :: SomeException) -> handleInterpProcessFailure i e
    
    45
    +-- This function blocks until the TH computation returns a value, but spawns in
    
    46
    +-- the background a thread to handle the 'THMessage's until the TH computation
    
    47
    +-- is fully done.
    
    48
    +sendTHComputation
    
    49
    +  :: Binary a
    
    50
    +  => ExtInterpInstance d
    
    51
    +  -> (InboxId -> Message a)
    
    52
    +  -> (forall b. Binary b => THMessage b -> IO (Bool, b))
    
    53
    +  -- ^ Handle one message. Return True to end the loop.
    
    54
    +  -- This handler must never uninterrutibly mask an expression which may get
    
    55
    +  -- stuck. If it does, then 'sendTHComputation' will also get uninterruptibly stuck.
    
    56
    +  -> IO a
    
    57
    +sendTHComputation i msg reqHandler = mask $ \restore -> do
    
    58
    +  -- inline "withAsync/wait"
    
    59
    +  var  <- newEmptyTMVarIO
    
    60
    +  mbox <- newMailbox (pipeAsyncMngr (interpPipe proc))
    
    61
    +  ht   <- forkFinally (restore (handlerLoop mbox)) (atomically . putTMVar var)
    
    62
    +  restore (sendMessage i (msg mbox) <* atomically (readTMVar var))
    
    63
    +    `onException` uninterruptibleMask_ (throwTo ht CancelTHHandler)
    
    64
    +  where
    
    65
    +    proc = instProcess i
    
    66
    +    handleOne mbox doneRef = do
    
    67
    +      handleRemoteTHCall (interpPipe proc) mbox $ \req -> do
    
    68
    +        (stop, res) <- reqHandler req
    
    69
    +        writeIORef doneRef stop
    
    70
    +        return res
    
    71
    +    handlerLoop mbox = do
    
    72
    +      doneRef <- newIORef False
    
    73
    +      let loop = do
    
    74
    +            handleOne mbox doneRef
    
    75
    +            isDone <- readIORef doneRef
    
    76
    +            if isDone then return ()
    
    77
    +                      else loop
    
    78
    +       in loop
    
    121 79
     
    
    122 80
     handleInterpProcessFailure :: InterpProcess -> SomeException -> IO a
    
    123 81
     handleInterpProcessFailure i e = do
    
    ... ... @@ -130,3 +88,8 @@ handleInterpProcessFailure i e = do
    130 88
           terminateProcess hdl
    
    131 89
           _ <- waitForProcess hdl
    
    132 90
           throw e
    
    91
    +
    
    92
    +data CancelTHHandler = CancelTHHandler deriving Show
    
    93
    +instance Exception CancelTHHandler where
    
    94
    +  fromException = asyncExceptionFromException
    
    95
    +  toException = asyncExceptionToException

  • compiler/GHC/Runtime/Interpreter/Types.hs
    ... ... @@ -111,7 +111,6 @@ type WasmInterp = ExtInterpState WasmInterpConfig ()
    111 111
     data InterpProcess = InterpProcess
    
    112 112
       { interpPipe   :: !Pipe           -- ^ Pipe to communicate with the server
    
    113 113
       , interpHandle :: !ProcessHandle  -- ^ Process handle of the server
    
    114
    -  , interpLock   :: !(MVar ())      -- ^ Lock to prevent concurrent access to the stream
    
    115 114
       }
    
    116 115
     
    
    117 116
     -- | Status of an external interpreter
    

  • compiler/GHC/Runtime/Interpreter/Wasm.hs
    ... ... @@ -73,14 +73,12 @@ spawnWasmInterp WasmInterpConfig {..} = do
    73 73
       hSetBuffering rh NoBuffering
    
    74 74
       interpPipe <- mkPipeFromHandles rh wh
    
    75 75
       pending_frees <- newMVar []
    
    76
    -  lock <- newMVar ()
    
    77 76
       pure
    
    78 77
         $ ExtInterpInstance
    
    79 78
           { instProcess =
    
    80 79
               InterpProcess
    
    81 80
                 { interpHandle = ph,
    
    82
    -              interpPipe,
    
    83
    -              interpLock = lock
    
    81
    +              interpPipe
    
    84 82
                 },
    
    85 83
             instPendingFrees = pending_frees,
    
    86 84
             instExtra = ()
    

  • compiler/GHC/Runtime/Utils.hs
    1
    -{-# LANGUAGE CPP #-}
    
    2
    -
    
    3 1
     module GHC.Runtime.Utils
    
    4 2
       ( runWithPipes
    
    5 3
       )
    
    6 4
     where
    
    7 5
     
    
    8 6
     import GHC.Prelude
    
    7
    +import GHC.Utils.Exception (onException)
    
    9 8
     
    
    10
    -#if defined(mingw32_HOST_OS)
    
    11
    -import Foreign.C
    
    12
    -import GHC.IO.Handle.FD (fdToHandle)
    
    13
    -import GHC.Utils.Exception as Ex
    
    14
    -# if defined(__IO_MANAGER_WINIO__)
    
    15
    -import GHC.IO.SubSystem ((<!>))
    
    16
    -import GHC.IO.Handle.Windows (handleToHANDLE)
    
    17
    -import GHC.Event.Windows (associateHandle')
    
    18
    -# endif
    
    19
    -#else
    
    20
    -import System.Posix as Posix
    
    21
    -#endif
    
    22 9
     import System.Process
    
    10
    +import System.Process.CommunicationHandle
    
    23 11
     import System.IO
    
    24 12
     
    
    13
    +-- | Spawn a process connected to us by two pipes. Important to close our copy
    
    14
    +-- of the endpoints passed to the child so each side only keeps open copies it
    
    15
    +-- needs. If we don't do that, then closing one end wouldn't interrupt reads on
    
    16
    +-- the other.
    
    25 17
     runWithPipes :: (CreateProcess -> IO ProcessHandle)
    
    26 18
                  -> FilePath -> [String] -> [String] -> IO (ProcessHandle, Handle, Handle)
    
    27
    -#if defined(mingw32_HOST_OS)
    
    28
    -foreign import ccall "io.h _close"
    
    29
    -   c__close :: CInt -> IO CInt
    
    30
    -
    
    31
    -foreign import ccall unsafe "io.h _get_osfhandle"
    
    32
    -   _get_osfhandle :: CInt -> IO CIntPtr
    
    33
    -
    
    34
    -runWithPipesPOSIX :: (CreateProcess -> IO ProcessHandle)
    
    35
    -                  -> FilePath -> [String] -> [String] -> IO (ProcessHandle, Handle, Handle)
    
    36
    -runWithPipesPOSIX createProc prog pre_opts opts = do
    
    37
    -    (rfd1, wfd1) <- createPipeFd -- we read on rfd1
    
    38
    -    (rfd2, wfd2) <- createPipeFd -- we write on wfd2
    
    39
    -    wh_client    <- _get_osfhandle wfd1
    
    40
    -    rh_client    <- _get_osfhandle rfd2
    
    41
    -    let args = pre_opts ++ (show wh_client : show rh_client : opts)
    
    42
    -    ph <- createProc (proc prog args)
    
    43
    -    rh <- mkHandle rfd1
    
    44
    -    wh <- mkHandle wfd2
    
    45
    -    return (ph, rh, wh)
    
    46
    -      where mkHandle :: CInt -> IO Handle
    
    47
    -            mkHandle fd = (fdToHandle fd) `Ex.onException` (c__close fd)
    
    48
    -
    
    49
    -# if defined (__IO_MANAGER_WINIO__)
    
    50
    -runWithPipesNative :: (CreateProcess -> IO ProcessHandle)
    
    51
    -                   -> FilePath -> [String] -> [String] -> IO (ProcessHandle, Handle, Handle)
    
    52
    -runWithPipesNative createProc prog pre_opts opts = do
    
    53
    -    (rh, wfd1) <- createPipe -- we read on rfd1
    
    54
    -    (rfd2, wh) <- createPipe -- we write on wfd2
    
    55
    -    wh_client    <- handleToHANDLE wfd1
    
    56
    -    rh_client    <- handleToHANDLE rfd2
    
    57
    -    -- Associate the handle with the current manager
    
    58
    -    -- but don't touch the ones we're passing to the child
    
    59
    -    -- since it needs to register the handle with its own manager.
    
    60
    -    associateHandle' =<< handleToHANDLE rh
    
    61
    -    associateHandle' =<< handleToHANDLE wh
    
    62
    -    let args = pre_opts ++ (show wh_client : show rh_client : opts)
    
    63
    -    ph <- createProc (proc prog args)
    
    64
    -    return (ph, rh, wh)
    
    65
    -
    
    66
    -runWithPipes = runWithPipesPOSIX <!> runWithPipesNative
    
    67
    -# else
    
    68
    -runWithPipes = runWithPipesPOSIX
    
    69
    -# endif
    
    70
    -#else
    
    71 19
     runWithPipes createProc prog pre_opts opts = do
    
    72
    -    (rfd1, wfd1) <- Posix.createPipe -- we read on rfd1
    
    73
    -    (rfd2, wfd2) <- Posix.createPipe -- we write on wfd2
    
    74
    -    setFdOption rfd1 CloseOnExec True
    
    75
    -    setFdOption wfd2 CloseOnExec True
    
    76
    -    let args = pre_opts ++ (show wfd1 : show rfd2 : opts)
    
    20
    +    (rh, chTheyWrite) <- createWeReadTheyWritePipe
    
    21
    +    (chTheyRead, wh)  <- createTheyReadWeWritePipe
    
    22
    +    let args = pre_opts ++ (show chTheyWrite : show chTheyRead : opts)
    
    23
    +        closeTheirs = do
    
    24
    +          closeCommunicationHandle chTheyWrite
    
    25
    +          closeCommunicationHandle chTheyRead
    
    26
    +    -- These handles have no finalizers, so close them ourselves if the spawn fails.
    
    77 27
         ph <- createProc (proc prog args)
    
    78
    -    closeFd wfd1
    
    79
    -    closeFd rfd2
    
    80
    -    rh <- fdToHandle rfd1
    
    81
    -    wh <- fdToHandle wfd2
    
    28
    +            `onException` (closeTheirs >> hClose rh >> hClose wh)
    
    29
    +    closeTheirs
    
    82 30
         return (ph, rh, wh)
    83
    -#endif
    
    84
    -

  • compiler/GHC/Tc/Gen/Splice.hs
    ... ... @@ -170,6 +170,7 @@ import Data.IORef
    170 170
     import GHC.Parser.HaddockLex (lexHsDoc)
    
    171 171
     import GHC.Parser (parseIdentifier)
    
    172 172
     import GHC.Rename.Doc (rnHsDoc)
    
    173
    +import Data.Function
    
    173 174
     
    
    174 175
     {-
    
    175 176
     Note [Template Haskell state diagram]
    
    ... ... @@ -1165,11 +1166,12 @@ runRemoteModFinalizers (ThModFinalizers finRefs) = do
    1165 1166
           case th_state of
    
    1166 1167
             Nothing -> return () -- TH was not started, nothing to do
    
    1167 1168
             Just fhv -> do
    
    1168
    -          r <- liftIO $ withForeignRef fhv $ \st ->
    
    1169
    -            withForeignRefs finRefs $ \qrefs ->
    
    1170
    -              sendMessageDelayedResponse inst (RunModFinalizers st qrefs)
    
    1171
    -          () <- runRemoteTH inst []
    
    1172
    -          qr <- liftIO $ receiveDelayedResponse inst r
    
    1169
    +          qr <- withRunInIO $ \runInIO -> do
    
    1170
    +            recovers <- newIORef []
    
    1171
    +            withForeignRef fhv $ \st ->
    
    1172
    +              withForeignRefs finRefs $ \qrefs ->
    
    1173
    +                sendTHComputation inst (\ii -> RunModFinalizers ii st qrefs) $ \msg ->
    
    1174
    +                  runInIO (runRemoteTH recovers msg)
    
    1173 1175
               checkQResult qr
    
    1174 1176
     
    
    1175 1177
     runQResult
    
    ... ... @@ -1824,48 +1826,47 @@ runTH ty fhv = do
    1824 1826
           rstate <- getTHState inst
    
    1825 1827
           loc <- location
    
    1826 1828
           -- run a remote TH request
    
    1827
    -      r <- liftIO $
    
    1829
    +      qr <- withRunInIO $ \runInIO -> do
    
    1830
    +        recovers <- newIORef []
    
    1828 1831
             withForeignRef rstate $ \state_hv ->
    
    1829
    -        withForeignRef fhv $ \q_hv ->
    
    1830
    -          sendMessageDelayedResponse inst (RunTH state_hv q_hv ty (Just loc))
    
    1831
    -      -- respond to requests from the interpreter
    
    1832
    -      runRemoteTH inst []
    
    1833
    -      -- get the final result
    
    1834
    -      qr <- liftIO $ receiveDelayedResponse inst r
    
    1832
    +          withForeignRef fhv $ \q_hv ->
    
    1833
    +            sendTHComputation inst (\ii -> RunTH ii state_hv q_hv ty (Just loc)) $ \msg ->
    
    1834
    +              runInIO (runRemoteTH recovers msg)
    
    1835 1835
           bs <- checkQResult qr
    
    1836 1836
           return $! runGet get (LB.fromStrict bs)
    
    1837 1837
     
    
    1838 1838
     
    
    1839
    --- | communicate with a remotely-running TH computation until it finishes.
    
    1839
    +-- | Communicate with a remotely-running TH computation until it finishes.
    
    1840 1840
     -- See Note [Remote Template Haskell] in libraries/ghci/GHCi/TH.hs.
    
    1841 1841
     runRemoteTH
    
    1842
    -  :: ExtInterpInstance d
    
    1843
    -  -> [Messages TcRnMessage]   --  saved from nested calls to qRecover
    
    1844
    -  -> TcM ()
    
    1845
    -runRemoteTH inst recovers = do
    
    1846
    -  THMsg msg <- liftIO $ receiveTHMessage inst
    
    1842
    +  :: Binary a
    
    1843
    +  => IORef [Messages TcRnMessage] -- ^ saved from nested calls to qRecover
    
    1844
    +  -> THMessage a
    
    1845
    +  -> TcM (Bool, a)
    
    1846
    +runRemoteTH recovers msg = do
    
    1847 1847
       case msg of
    
    1848
    -    RunTHDone -> return ()
    
    1848
    +    RunTHDone -> return (True, ())
    
    1849 1849
         StartRecover -> do -- Note [TH recover with -fexternal-interpreter]
    
    1850 1850
           v <- getErrsVar
    
    1851 1851
           msgs <- readTcRef v
    
    1852 1852
           writeTcRef v emptyMessages
    
    1853
    -      runRemoteTH inst (msgs : recovers)
    
    1853
    +      modifyIORef recovers (msgs:) & liftIO
    
    1854
    +      return (False, ())
    
    1854 1855
         EndRecover caught_error -> do
    
    1855
    -      let (prev_msgs, rest) = case recovers of
    
    1856
    -             [] -> panic "EndRecover"
    
    1857
    -             a : b -> (a,b)
    
    1856
    +      (prev_msgs, rest) <- liftIO $
    
    1857
    +        readIORef recovers >>= \case
    
    1858
    +          [] -> panic "EndRecover"
    
    1859
    +          a : b -> pure (a,b)
    
    1858 1860
           v <- getErrsVar
    
    1859 1861
           warn_msgs <- getWarningMessages <$> readTcRef v
    
    1860 1862
           -- keep the warnings only if there were no errors
    
    1861 1863
           writeTcRef v $ if caught_error
    
    1862 1864
             then prev_msgs
    
    1863 1865
             else mkMessages warn_msgs `unionMessages` prev_msgs
    
    1864
    -      runRemoteTH inst rest
    
    1866
    +      writeIORef recovers rest & liftIO
    
    1867
    +      return (False, ())
    
    1865 1868
         _other -> do
    
    1866
    -      r <- handleTHMessage msg
    
    1867
    -      liftIO $ sendAnyValue inst r
    
    1868
    -      runRemoteTH inst recovers
    
    1869
    +      (False,) <$> handleTHMessage msg
    
    1869 1870
     
    
    1870 1871
     -- | Check a QResult
    
    1871 1872
     checkQResult :: QResult a -> TcM a
    

  • compiler/ghc.cabal.in
    ... ... @@ -117,7 +117,7 @@ Library
    117 117
         Build-Depends: base       >= 4.11 && < 4.24,
    
    118 118
                        deepseq    >= 1.4 && < 1.6,
    
    119 119
                        directory  >= 1   && < 1.4,
    
    120
    -                   process    >= 1   && < 1.7,
    
    120
    +                   process    >= 1.6.20 && < 1.7,
    
    121 121
                        bytestring >= 0.11 && < 0.13,
    
    122 122
                        binary     == 0.8.*,
    
    123 123
                        text       >= 2.0 && < 2.2,
    

  • libraries/ghci/GHCi/Message.hs
    1
    -{-# LANGUAGE GADTs, DeriveGeneric, StandaloneDeriving, ScopedTypeVariables,
    
    2
    -    GeneralizedNewtypeDeriving, ExistentialQuantification, RecordWildCards,
    
    3
    -    CPP, NamedFieldPuns #-}
    
    1
    +{-# LANGUAGE RecursiveDo, GADTs, DeriveGeneric, StandaloneDeriving, ScopedTypeVariables,
    
    2
    +    GeneralizedNewtypeDeriving, ExistentialQuantification, RecordWildCards, CPP,
    
    3
    +    NamedFieldPuns, LambdaCase, TypeAbstractions, TypeApplications,
    
    4
    +    LinearTypes, RankNTypes, UnboxedTuples, BlockArguments, TupleSections, MagicHash #-}
    
    4 5
     {-# OPTIONS_GHC -fno-warn-name-shadowing -fno-warn-orphans #-}
    
    5 6
     
    
    6 7
     -- |
    
    ... ... @@ -22,7 +23,16 @@ module GHCi.Message
    22 23
       , ResumeContext(..)
    
    23 24
       , QState(..)
    
    24 25
       , getMessage, putMessage, getTHMessage, putTHMessage
    
    25
    -  , Pipe, mkPipeFromHandles, mkPipeFromContinuations, remoteCall, remoteTHCall, readPipe, writePipe
    
    26
    +
    
    27
    +  , Pipe(..), mkPipeFromHandles, mkPipeFromContinuations, closePipe
    
    28
    +  , remoteCall, remoteTHCall
    
    29
    +  , handleRemoteCall, handleRemoteTHCall, writeReply
    
    30
    +  , RemoteProcessException(..), PipeReadClosedException(..)
    
    31
    +
    
    32
    +  , InboxId(..)
    
    33
    +  , newMailbox
    
    34
    +  , closeMailbox
    
    35
    +
    
    26 36
       , BreakModule
    
    27 37
       , BreakUnitId
    
    28 38
       , LoadedDLL
    
    ... ... @@ -45,18 +55,14 @@ import qualified GHC.Exts.Heap as Heap
    45 55
     import GHC.ForeignSrcLang
    
    46 56
     import GHC.Fingerprint
    
    47 57
     import GHC.Conc (pseq, par)
    
    58
    +import GHC.IO (IO(..))
    
    48 59
     import Control.Concurrent
    
    49 60
     import Control.DeepSeq
    
    50 61
     import Control.Exception
    
    51
    -#if MIN_VERSION_base(4,20,0)
    
    52
    -import Control.Exception.Context
    
    53
    -#endif
    
    54 62
     import Data.Binary
    
    55 63
     import Data.Binary.Get
    
    56 64
     import Data.Binary.Put
    
    57 65
     import Data.ByteString (ByteString)
    
    58
    -import qualified Data.ByteString as B
    
    59
    -import qualified Data.ByteString.Builder as B
    
    60 66
     import qualified Data.ByteString.Lazy as LB
    
    61 67
     import qualified Data.ByteString.Short as BS
    
    62 68
     import Data.Dynamic
    
    ... ... @@ -68,9 +74,7 @@ import GHC.Generics
    68 74
     import GHC.Stack.CCS
    
    69 75
     import qualified GHC.Boot.TH.Syntax        as TH
    
    70 76
     import qualified GHC.Boot.TH.Monad         as TH
    
    71
    -import System.Exit
    
    72
    -import System.IO
    
    73
    -import System.IO.Error
    
    77
    +import GHCi.Message.Async
    
    74 78
     
    
    75 79
     -- -----------------------------------------------------------------------------
    
    76 80
     -- The RPC protocol between GHC and the interactive server
    
    ... ... @@ -214,14 +218,16 @@ data Message a where
    214 218
       -- they did, we have to serialize the value anyway, so we might
    
    215 219
       -- as well serialize it to force it.
    
    216 220
       RunTH
    
    217
    -   :: RemoteRef (IORef QState)
    
    221
    +   :: InboxId -- ^ announce mailbox for related 'remoteTHCall's
    
    222
    +   -> RemoteRef (IORef QState)
    
    218 223
        -> HValueRef {- e.g. TH.Q TH.Exp -}
    
    219 224
        -> THResultType
    
    220 225
        -> Maybe TH.Loc
    
    221 226
        -> Message (QResult ByteString)
    
    222 227
     
    
    223 228
       -- | Run the given mod finalizers.
    
    224
    -  RunModFinalizers :: RemoteRef (IORef QState)
    
    229
    +  RunModFinalizers :: InboxId -- ^ announce mailbox for related 'remoteTHCall's
    
    230
    +                   -> RemoteRef (IORef QState)
    
    225 231
                        -> [RemoteRef (TH.Q ())]
    
    226 232
                        -> Message (QResult ())
    
    227 233
     
    
    ... ... @@ -458,45 +464,6 @@ data BreakUnitId
    458 464
     -- | A dummy type that tags pointers returned by 'LoadDLLs'.
    
    459 465
     data LoadedDLL
    
    460 466
     
    
    461
    --- SomeException can't be serialized because it contains dynamic
    
    462
    --- types.  However, we do very limited things with the exceptions that
    
    463
    --- are thrown by interpreted computations:
    
    464
    ---
    
    465
    --- * We print them, e.g. "*** Exception: <something>"
    
    466
    --- * UserInterrupt has a special meaning
    
    467
    --- * In ghc -e, exitWith should exit with the appropriate exit code
    
    468
    ---
    
    469
    --- So all we need to do is distinguish UserInterrupt and ExitCode, and
    
    470
    --- all other exceptions can be represented by their 'show' string.
    
    471
    ---
    
    472
    -data SerializableException
    
    473
    -  = EUserInterrupt
    
    474
    -  | EExitCode ExitCode
    
    475
    -  | EOtherException String
    
    476
    -  deriving (Generic, Show)
    
    477
    -
    
    478
    -toSerializableException :: SomeException -> SerializableException
    
    479
    -toSerializableException ex
    
    480
    -  | Just UserInterrupt <- fromException ex  = EUserInterrupt
    
    481
    -  | Just (ec::ExitCode) <- fromException ex = (EExitCode ec)
    
    482
    -  | otherwise = EOtherException $
    
    483
    -#if MIN_VERSION_base(4,20,0)
    
    484
    -      -- Exception plus backtrace as seen in `displayExceptionWithInfo`
    
    485
    -      case displayExceptionContext (someExceptionContext ex) of
    
    486
    -        "" -> displayException (ex :: SomeException)
    
    487
    -        cx -> displayException (ex :: SomeException) ++ "\n\n" ++ cx
    
    488
    -#else
    
    489
    -      show (ex :: SomeException)
    
    490
    -#endif
    
    491
    -
    
    492
    -fromSerializableException :: SerializableException -> SomeException
    
    493
    -fromSerializableException EUserInterrupt = toException UserInterrupt
    
    494
    -fromSerializableException (EExitCode c) = toException c
    
    495
    -fromSerializableException (EOtherException str) = toException (ErrorCall str)
    
    496
    -
    
    497
    -instance Binary ExitCode
    
    498
    -instance Binary SerializableException
    
    499
    -
    
    500 467
     data THResult a
    
    501 468
       = THException String
    
    502 469
       | THComplete a
    
    ... ... @@ -513,11 +480,13 @@ instance Binary THResultType
    513 480
     -- StartTH message.  A new one is created per module that GHC
    
    514 481
     -- typechecks.
    
    515 482
     data QState = QState
    
    516
    -  { qsMap        :: Map TypeRep Dynamic
    
    483
    +  { qsMap      :: Map TypeRep Dynamic
    
    517 484
            -- ^ persistent data between splices in a module
    
    518
    -  , qsLocation   :: Maybe TH.Loc
    
    485
    +  , qsLocation :: Maybe TH.Loc
    
    519 486
            -- ^ location for current splice, if any
    
    520
    -  , qsPipe :: Pipe
    
    487
    +  , qsInboxId  :: InboxId
    
    488
    +      -- ^ the mailbox expecting requests for each RunTH-like run
    
    489
    +  , qsPipe     :: Pipe
    
    521 490
            -- ^ pipe to communicate with GHC
    
    522 491
       }
    
    523 492
     instance Show QState where show _ = "<QState>"
    
    ... ... @@ -600,9 +569,9 @@ getMessage = do
    600 569
           29 -> Msg <$> (BreakpointStatus <$> get <*> get)
    
    601 570
           30 -> Msg <$> (GetBreakpointVar <$> get <*> get)
    
    602 571
           31 -> Msg <$> return StartTH
    
    603
    -      32 -> Msg <$> (RunModFinalizers <$> get <*> get)
    
    572
    +      32 -> Msg <$> (RunModFinalizers <$> get <*> get <*> get)
    
    604 573
           33 -> Msg <$> (AddSptEntry <$> get <*> get)
    
    605
    -      34 -> Msg <$> (RunTH <$> get <*> get <*> get <*> get)
    
    574
    +      34 -> Msg <$> (RunTH <$> get <*> get <*> get <*> get <*> get)
    
    606 575
           35 -> Msg <$> (GetClosure <$> get)
    
    607 576
           36 -> Msg <$> (Seq <$> get)
    
    608 577
           37 -> Msg <$> return RtsRevertCAFs
    
    ... ... @@ -648,9 +617,9 @@ putMessage m = case m of
    648 617
       BreakpointStatus arr ix     -> putWord8 29 >> put arr >> put ix
    
    649 618
       GetBreakpointVar a b        -> putWord8 30 >> put a >> put b
    
    650 619
       StartTH                     -> putWord8 31
    
    651
    -  RunModFinalizers a b        -> putWord8 32 >> put a >> put b
    
    620
    +  RunModFinalizers a b c      -> putWord8 32 >> put a >> put b >> put c
    
    652 621
       AddSptEntry a b             -> putWord8 33 >> put a >> put b
    
    653
    -  RunTH st q loc ty           -> putWord8 34 >> put st >> put q >> put loc >> put ty
    
    622
    +  RunTH ii st q loc ty        -> putWord8 34 >> put ii >> put st >> put q >> put loc >> put ty
    
    654 623
       GetClosure a                -> putWord8 35 >> put a
    
    655 624
       Seq a                       -> putWord8 36 >> put a
    
    656 625
       RtsRevertCAFs               -> putWord8 37
    
    ... ... @@ -688,73 +657,39 @@ serializeBCOs rbcos = parMap doChunk (chunkList 100 rbcos)
    688 657
       chunkList _ [] = []
    
    689 658
       chunkList n xs = as : chunkList n bs where (as,bs) = splitAt n xs
    
    690 659
     
    
    691
    --- -----------------------------------------------------------------------------
    
    692
    --- Reading/writing messages
    
    693
    -
    
    694
    --- | An opaque pipe for bidirectional binary data transmission.
    
    695
    -data Pipe = Pipe
    
    696
    -  { getSome :: !(IO ByteString)
    
    697
    -  , putAll :: !(B.Builder -> IO ())
    
    698
    -  , pipeLeftovers :: !(IORef (Maybe ByteString))
    
    699
    -  }
    
    700
    -
    
    701
    --- | Make a 'Pipe' from a 'Handle' to read and a 'Handle' to write.
    
    702
    -mkPipeFromHandles :: Handle -> Handle -> IO Pipe
    
    703
    -mkPipeFromHandles pipeRead pipeWrite = do
    
    704
    -  let getSome = B.hGetSome pipeRead (32*1024)
    
    705
    -      putAll b = do
    
    706
    -        B.hPutBuilder pipeWrite b
    
    707
    -        hFlush pipeWrite
    
    708
    -  pipeLeftovers <- newIORef Nothing
    
    709
    -  pure $ Pipe { getSome, putAll, pipeLeftovers }
    
    710
    -
    
    711
    --- | Make a 'Pipe' from a reader function and a writer function.
    
    712
    -mkPipeFromContinuations :: IO ByteString -> (B.Builder -> IO ()) -> IO Pipe
    
    713
    -mkPipeFromContinuations getSome putAll = do
    
    714
    -  pipeLeftovers <- newIORef Nothing
    
    715
    -  pure $ Pipe { getSome, putAll, pipeLeftovers }
    
    716
    -
    
    660
    +-- | Send a 'Message' request to and wait for a reply from the interpreter
    
    661
    +-- (blocks on the calling side, doesn't necessarily block the interpreter)
    
    717 662
     remoteCall :: Binary a => Pipe -> Message a -> IO a
    
    718
    -remoteCall pipe msg = do
    
    719
    -  writePipe pipe (putMessage msg)
    
    720
    -  readPipe pipe get
    
    721
    -
    
    722
    -writePipe :: Pipe -> Put -> IO ()
    
    723
    -writePipe Pipe{..} put = putAll $ execPut put
    
    724
    -
    
    725
    -remoteTHCall :: Binary a => Pipe -> THMessage a -> IO a
    
    726
    -remoteTHCall pipe msg = do
    
    727
    -  writePipe pipe (putTHMessage msg)
    
    728
    -  readPipe pipe get
    
    729
    -
    
    730
    -readPipe :: Pipe -> Get a -> IO a
    
    731
    -readPipe Pipe{..} get = do
    
    732
    -  leftovers <- readIORef pipeLeftovers
    
    733
    -  m <- getBin getSome get leftovers
    
    734
    -  case m of
    
    735
    -    Nothing -> throw $
    
    736
    -      mkIOError eofErrorType "GHCi.Message.readPipe" Nothing Nothing
    
    737
    -    Just (result, new_leftovers) -> do
    
    738
    -      writeIORef pipeLeftovers new_leftovers
    
    739
    -      return result
    
    740
    -
    
    741
    -getBin
    
    742
    -  :: (IO ByteString) -> Get a -> Maybe ByteString
    
    743
    -  -> IO (Maybe (a, Maybe ByteString))
    
    744
    -
    
    745
    -getBin getsome get leftover = go leftover (runGetIncremental get)
    
    746
    - where
    
    747
    -   go Nothing (Done leftover _ msg) =
    
    748
    -     return (Just (msg, if B.null leftover then Nothing else Just leftover))
    
    749
    -   go _ Done{} = throwIO (ErrorCall "getBin: Done with leftovers")
    
    750
    -   go (Just leftover) (Partial fun) = do
    
    751
    -     go Nothing (fun (Just leftover))
    
    752
    -   go Nothing (Partial fun) = do
    
    753
    -     -- putStrLn "before hGetSome"
    
    754
    -     b <- getsome
    
    755
    -     -- putStrLn $ "hGetSome: " ++ show (B.length b)
    
    756
    -     if B.null b
    
    757
    -        then return Nothing
    
    758
    -        else go Nothing (fun (Just b))
    
    759
    -   go _lft (Fail _rest _off str) =
    
    760
    -     throwIO (ErrorCall ("getBin: " ++ str))
    663
    +remoteCall pipe msg = either throwIO (pure . runGet get)
    
    664
    +                  =<< remoteAsyncReq pipe (putMessage msg) Nothing
    
    665
    +
    
    666
    +-- | Handle a single 'Message' request on the interpreter and reply to the host
    
    667
    +handleRemoteCall :: Pipe
    
    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
    
    675
    +
    
    676
    +-- | Send a 'THMessage' request to the host and wait for a reply from the host
    
    677
    +-- to the interpreter.
    
    678
    +--
    
    679
    +-- A 'THMessage' request from the interpreter is always associated with a
    
    680
    +-- 'RunTH'-like run. Since the host may have many in-flight TH runs, we always
    
    681
    +-- want a specific host 'InboxId' (which is announced in the 'RunTH' request)
    
    682
    +remoteTHCall :: Binary a => Pipe -> InboxId -> THMessage a -> IO a
    
    683
    +remoteTHCall pipe ii msg = either throwIO (pure . runGet get)
    
    684
    +                       =<< remoteAsyncReq pipe (putTHMessage msg) (Just ii)
    
    685
    +
    
    686
    +-- | Handle a 'THMessage' request on the host and reply to the interpreter.
    
    687
    +-- Symmterically to 'remoteTHCall', we always want to specify the 'InboxId' for
    
    688
    +-- RunTH "reverse requests"
    
    689
    +handleRemoteTHCall
    
    690
    +  :: Pipe -> InboxId -> (forall a. (Show a, Binary a) => THMessage a -> IO a) -> IO ()
    
    691
    +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
    
    694
    +      (# s1, a #) -> writeReply writer (put a) |#> s1
    
    695
    +

  • libraries/ghci/GHCi/Message/Async.hs
    1
    +{-# LANGUAGE RecursiveDo, GADTs, DeriveGeneric, StandaloneDeriving, ScopedTypeVariables,
    
    2
    +    GeneralizedNewtypeDeriving, ExistentialQuantification, RecordWildCards, CPP,
    
    3
    +    NamedFieldPuns, LambdaCase, TypeAbstractions, TypeApplications,
    
    4
    +    LinearTypes, RankNTypes, UnboxedTuples, BlockArguments, TupleSections, MagicHash #-}
    
    5
    +{-# OPTIONS_GHC -fno-warn-name-shadowing -fno-warn-orphans #-}
    
    6
    +module GHCi.Message.Async
    
    7
    +  (
    
    8
    +    -- * Pipes
    
    9
    +    Pipe(..), mkPipeFromHandles, mkPipeFromContinuations
    
    10
    +  , closePipe, PipeReadClosedException(..)
    
    11
    +
    
    12
    +    -- * Interface for async bidirectional RPC
    
    13
    +  , remoteAsyncReq, handleAsyncReq
    
    14
    +  , RemoteProcessException(..), DecodeAsyncReqException(..)
    
    15
    +
    
    16
    +    -- ** Mailboxes
    
    17
    +  , InboxId(..)
    
    18
    +  , newMailbox
    
    19
    +  , closeMailbox
    
    20
    +
    
    21
    +    -- ** Async matching internals
    
    22
    +  , AsyncManager(..), CorrelationId
    
    23
    +  , IddMsg(..), newAsyncManager
    
    24
    +
    
    25
    +    -- ** Consume `reply` exactly once
    
    26
    +  , WriteReply(..), writeReply
    
    27
    +  , runWriteReply, (|#>), (%>>=)
    
    28
    +
    
    29
    +    -- * Serializable exceptions
    
    30
    +  , SerializableException(..)
    
    31
    +  , toSerializableException, fromSerializableException
    
    32
    +  ) where
    
    33
    +
    
    34
    +import Prelude -- See note [Why do we import Prelude here?]
    
    35
    +import GHCi.TH.Binary () -- For Binary instances
    
    36
    +
    
    37
    +import GHC.IO (IO(..))
    
    38
    +import Control.Concurrent
    
    39
    +import Control.Exception
    
    40
    +#if MIN_VERSION_base(4,20,0)
    
    41
    +import Control.Exception.Context
    
    42
    +#endif
    
    43
    +import Data.Binary
    
    44
    +import Data.Binary.Get
    
    45
    +import Data.Binary.Put
    
    46
    +import Data.ByteString (ByteString)
    
    47
    +import qualified Data.ByteString as B
    
    48
    +import qualified Data.ByteString.Builder as B
    
    49
    +import qualified Data.ByteString.Lazy as LB
    
    50
    +import Data.IORef
    
    51
    +import Data.IntMap (IntMap)
    
    52
    +import qualified Data.IntMap as IntMap
    
    53
    +import Data.Maybe
    
    54
    +import GHC.Generics
    
    55
    +import System.Exit
    
    56
    +import System.IO
    
    57
    +import Control.Monad
    
    58
    +import Control.Concurrent.STM
    
    59
    +import Data.Bifunctor
    
    60
    +import GHC.Base (State#)
    
    61
    +import GHC.Exts (RealWorld)
    
    62
    +
    
    63
    +--------------------------------------------------------------------------------
    
    64
    +-- * Reading/writing and Pipes
    
    65
    +--------------------------------------------------------------------------------
    
    66
    +
    
    67
    +-- | An opaque pipe for bidirectional binary data transmission.
    
    68
    +data Pipe = Pipe
    
    69
    +  { getSome         :: !(IO ByteString)
    
    70
    +  , putAll          :: !(B.Builder -> IO ())
    
    71
    +  , pipeLeftovers   :: !(IORef (Maybe ByteString))
    
    72
    +  , pipeLock        :: !(MVar ()) -- ^ Lock to prevent concurrent writes to the stream
    
    73
    +  , pipeAsyncMngr   :: !AsyncManager
    
    74
    +  , pipeCloseRead   :: !(IO ())
    
    75
    +  -- ^ closes read end of the pipe
    
    76
    +  , pipeCloseWrite  :: !(IO ())
    
    77
    +  -- ^ closes write end of the pipe
    
    78
    +  , pipeReadClosed  :: !(TVar Bool)
    
    79
    +    -- ^ After the pipe is closed, messages can no longer be received from the
    
    80
    +    -- other end, but the 'AsyncManager' queues might not be empty yet.
    
    81
    +  , pipeWriteClosed :: !(TVar Bool)
    
    82
    +    -- ^ After the write end of the pipe is closed we can no longer submit
    
    83
    +    -- requests or respond to ones we receive, but we might still receive
    
    84
    +    -- replies.
    
    85
    +  }
    
    86
    +
    
    87
    +-- | Make a 'Pipe' from a 'Handle' to read and a 'Handle' to write.
    
    88
    +-- Must call 'closePipe' at the end to clean up.
    
    89
    +mkPipeFromHandles :: Handle -> Handle -> IO Pipe
    
    90
    +mkPipeFromHandles pipeRead pipeWrite = do
    
    91
    +  let getSome = B.hGetSome pipeRead (32*1024)
    
    92
    +      putAll b = do
    
    93
    +        B.hPutBuilder pipeWrite b
    
    94
    +        hFlush pipeWrite
    
    95
    +  mkPipeFromContinuations getSome putAll (hClose pipeRead) (hClose pipeWrite)
    
    96
    +
    
    97
    +-- | Make a 'Pipe' from a reader function and a writer function.
    
    98
    +-- The pipe should be closed with 'closePipe', which will call 'pipeCloseRead' and 'pipeCloseWrite'.
    
    99
    +mkPipeFromContinuations :: IO ByteString -> (B.Builder -> IO ())
    
    100
    +                        -> IO ()
    
    101
    +                        -- ^ pipeCloseRead
    
    102
    +                        -> IO ()
    
    103
    +                        -- ^ pipeCloseWrite
    
    104
    +                        -> IO Pipe
    
    105
    +mkPipeFromContinuations getSome putAll pipeCloseRead pipeCloseWrite = mdo
    
    106
    +  pipeLeftovers   <- newIORef Nothing
    
    107
    +  pipeLock        <- newMVar ()
    
    108
    +  pipeReadClosed  <- newTVarIO False
    
    109
    +  pipeWriteClosed  <- newTVarIO False
    
    110
    +  mdo
    
    111
    +    pipeAsyncMngr <- newAsyncManager p
    
    112
    +    let p = Pipe { .. }
    
    113
    +    pure p
    
    114
    +
    
    115
    +-- | Close our ends of the pipe.
    
    116
    +--
    
    117
    +-- The ordering is subtle and important. We first fail any requests still
    
    118
    +-- waiting for a reply, then close the write end so the other side sees EOF.
    
    119
    +-- We take the write lock to close it, so we never close under a concurrent
    
    120
    +-- write. Only then do we stop the drain thread, which closes the read end
    
    121
    +-- itself once its read has finished. On Windows its read can't be
    
    122
    +-- interrupted, so this waits for the other side to close its write end too.
    
    123
    +-- Finally we stop the write thread, which could be blocked waiting for more to write.
    
    124
    +closePipe :: Pipe -> IO ()
    
    125
    +closePipe p@Pipe{..} = do
    
    126
    +  readAlreadyClosed <- atomically $ do
    
    127
    +    closed <- readTVar pipeReadClosed
    
    128
    +    failPendingReplies p (toException PipeReadClosedException)
    
    129
    +    pure closed
    
    130
    +
    
    131
    +  writeAlreadyClosed <- readTVarIO pipeWriteClosed
    
    132
    +
    
    133
    +  unless writeAlreadyClosed (withLock p pipeCloseWrite)
    
    134
    +    `finally` do
    
    135
    +      unless readAlreadyClosed
    
    136
    +        (throwTo (asyncDrainTid pipeAsyncMngr) PipeReadClosedException)
    
    137
    +      unless writeAlreadyClosed
    
    138
    +        (throwTo (asyncFillTid pipeAsyncMngr) PipeWriteClosedException)
    
    139
    +
    
    140
    +-- | We've closed the pipe read end on this process
    
    141
    +data PipeReadClosedException = PipeReadClosedException deriving Show
    
    142
    +instance Exception PipeReadClosedException
    
    143
    +
    
    144
    +-- | We've closed the pipe write end on this process
    
    145
    +data PipeWriteClosedException = PipeWriteClosedException deriving Show
    
    146
    +instance Exception PipeWriteClosedException
    
    147
    +
    
    148
    +--------------------------------------------------------------------------------
    
    149
    +-- ** Pipe thread-safe read/write
    
    150
    +--------------------------------------------------------------------------------
    
    151
    +-- All messages on the wire are 'IddMsg', which contain a correlation header
    
    152
    +-- and identify whether they are requests or replies.
    
    153
    +
    
    154
    +writePipe :: Pipe -> IddMsg -> IO ()
    
    155
    +writePipe Pipe{..} msg = atomically $ do
    
    156
    +  readTVar pipeWriteClosed >>= \case
    
    157
    +    True -> throwSTM PipeWriteClosedException
    
    158
    +    False -> pure ()
    
    159
    +  writeTQueue (writeQueue pipeAsyncMngr) msg
    
    160
    +
    
    161
    +-- | Only the 'drainPipe' thread should read from the pipe directly. Other
    
    162
    +-- functions should wait for the matching reply to be delivered asynchronously.
    
    163
    +-- (see 'withAsyncReq'). It doesn't lock to read bc it assumes single reader is 'drainPipe'.
    
    164
    +--
    
    165
    +-- Returns @Nothing@ on EOF.
    
    166
    +readPipe :: Pipe -> IO (Maybe IddMsg)
    
    167
    +readPipe Pipe{..} = do
    
    168
    +  leftovers <- readIORef pipeLeftovers
    
    169
    +  m <- getBin getSome get leftovers
    
    170
    +  case m of
    
    171
    +    Nothing -> return Nothing
    
    172
    +    Just (result, new_leftovers) -> do
    
    173
    +      writeIORef pipeLeftovers new_leftovers
    
    174
    +      return (Just result)
    
    175
    +
    
    176
    +getBin
    
    177
    +  :: (IO ByteString) -> Get a -> Maybe ByteString
    
    178
    +  -> IO (Maybe (a, Maybe ByteString))
    
    179
    +
    
    180
    +getBin getsome get leftover = go leftover (runGetIncremental get)
    
    181
    + where
    
    182
    +   go Nothing (Done leftover _ msg) =
    
    183
    +     return (Just (msg, if B.null leftover then Nothing else Just leftover))
    
    184
    +   go _ Done{} = throwIO (ErrorCall "getBin: Done with leftovers")
    
    185
    +   go (Just leftover) (Partial fun) = do
    
    186
    +     go Nothing (fun (Just leftover))
    
    187
    +   go Nothing (Partial fun) = do
    
    188
    +     -- putStrLn "before hGetSome"
    
    189
    +     b <- getsome
    
    190
    +     -- putStrLn $ "hGetSome: " ++ show (B.length b)
    
    191
    +     if B.null b
    
    192
    +        then return Nothing
    
    193
    +        else go Nothing (fun (Just b))
    
    194
    +   go _lft (Fail _rest _off str) =
    
    195
    +     throwIO (ErrorCall ("getBin: " ++ str))
    
    196
    +
    
    197
    +withLock :: Pipe -> IO c -> IO c
    
    198
    +withLock Pipe{..} = bracket (takeMVar pipeLock) (putMVar pipeLock) . const
    
    199
    +
    
    200
    +--------------------------------------------------------------------------------
    
    201
    +-- * Matching requests with async replies
    
    202
    +--------------------------------------------------------------------------------
    
    203
    +
    
    204
    +-- | Send an asynchronous request and block waiting for a matching reply
    
    205
    +remoteAsyncReq :: Pipe -> Put -> (Maybe InboxId) -> IO (Either SomeException LB.ByteString)
    
    206
    +remoteAsyncReq pipe msg ii = do
    
    207
    +
    
    208
    +  let pendingReplies = pendingReply (pipeAsyncMngr pipe)
    
    209
    +
    
    210
    +  -- Mark pending request
    
    211
    +  uq <- freshId (pipeAsyncMngr pipe)
    
    212
    +  wv <- newEmptyTMVarIO
    
    213
    +
    
    214
    +  isClosed <- atomically $ do
    
    215
    +    isClosed <- readTVar (pipeReadClosed pipe)
    
    216
    +                -- don't send the req if we won't get a reply.
    
    217
    +    if isClosed
    
    218
    +      then pure True
    
    219
    +      else do
    
    220
    +        modifyTVar pendingReplies (IntMap.insert uq wv)
    
    221
    +        pure False
    
    222
    +
    
    223
    +  when isClosed $ do
    
    224
    +    throwIO PipeReadClosedException
    
    225
    +
    
    226
    +  -- Write request
    
    227
    +  writePipe pipe (Request uq ii (runPut msg))
    
    228
    +    `onException`
    
    229
    +      -- the req wasn't done, or no one will be reading the reply:
    
    230
    +      -- in either case, drop it from the pending map.
    
    231
    +      atomically (modifyTVar pendingReplies (IntMap.delete uq))
    
    232
    +
    
    233
    +  -- Block waiting for reply
    
    234
    +  atomically (takeTMVar wv)
    
    235
    +    `onException`
    
    236
    +      atomically (modifyTVar pendingReplies (IntMap.delete uq))
    
    237
    +
    
    238
    +-- | Receive a single asynchronous request and send a matching reply
    
    239
    +-- asynchronously (the handler may fork a computation that eventually replies
    
    240
    +-- and can return before actually replying, as long as it eventually does).
    
    241
    +--
    
    242
    +-- The handler receives as an argument the function to use to reply to the request.
    
    243
    +-- It must be used exactly once (all requests expect a reply, and you can't
    
    244
    +-- reply more than once). Linear types enforce the handler replies in every
    
    245
    +-- branch, and if the handler otherwise throws, it is handled by 'handleAsyncReq'.
    
    246
    +handleAsyncReq :: Pipe -> Get a -> (Maybe InboxId) -> (WriteReply Put %1 -> a -> IO r) -> IO r
    
    247
    +handleAsyncReq pipe getit mii k = mask $ \restore -> do
    
    248
    +  let am = pipeAsyncMngr pipe
    
    249
    +  ack <- newEmptyMVar
    
    250
    +
    
    251
    +  -- Block waiting for request
    
    252
    +  mbreq <- atomically $ do
    
    253
    +    readTVar (pipeWriteClosed pipe) >>= \case
    
    254
    +      -- We will not be able to write the reply, so no point in continuiting.
    
    255
    +      True -> throwSTM PipeWriteClosedException
    
    256
    +      False -> pure ()
    
    257
    +    isClosed <- readTVar (pipeReadClosed pipe)
    
    258
    +    taken <- case mii of
    
    259
    +      Nothing -> tryReadTQueue  $  requestsQueue am
    
    260
    +      Just ii -> tryReadTQueue =<< getMailbox am ii
    
    261
    +    case taken of
    
    262
    +      Just req -> pure (Just req)
    
    263
    +      Nothing  | isClosed  -> pure Nothing -- empty and closed
    
    264
    +               | otherwise -> retry -- wait for msg
    
    265
    +
    
    266
    +  case mbreq of
    
    267
    +    Nothing -> throwIO PipeReadClosedException -- can't expect reqs
    
    268
    +    Just (uq, bs) -> do
    
    269
    +      let reply p = do
    
    270
    +            writePipe pipe (Reply uq (bimap toSerializableException runPut p))
    
    271
    +            putMVar ack ()
    
    272
    +
    
    273
    +      -- Run the continuation which must reply exactly once with 'writeReply'
    
    274
    +      case runGetOrFail getit bs of
    
    275
    +        Left (_, _, err) -> do
    
    276
    +          let exc = DecodeAsyncReqException err
    
    277
    +          reply (Left (toException exc))
    
    278
    +          throwIO exc
    
    279
    +        Right (_, _, it) ->
    
    280
    +          restore (k (WriteReply reply) it)
    
    281
    +
    
    282
    +            -- If there's an exception before the reply is sent, reply with an error instead.
    
    283
    +            `catch` \(e :: SomeException) -> do
    
    284
    +
    
    285
    +                tryTakeMVar ack >>= \case
    
    286
    +                  Just () ->
    
    287
    +                    -- Already replied
    
    288
    +                    throwIO e
    
    289
    +                  Nothing -> do
    
    290
    +                    -- Reply with an error
    
    291
    +                    reply (Left e)
    
    292
    +                    throwIO e
    
    293
    +
    
    294
    +-- | 'handleSyncReq' failed to decode an incoming request payload using the given @'Get' a@
    
    295
    +newtype DecodeAsyncReqException = DecodeAsyncReqException String deriving Show
    
    296
    +instance Exception DecodeAsyncReqException
    
    297
    +
    
    298
    +
    
    299
    +-- ** Mailboxes ----------------------------------------------------------------
    
    300
    +
    
    301
    +-- | A 'Request' may optionally declare an 'InboxId'. If it does, it can only
    
    302
    +-- be read by a 'handleAsyncReq' for that specific 'InboxId'. That is,
    
    303
    +-- @'handleAsyncReq' p get Nothing@ will not receive messages meant for a
    
    304
    +-- specific inbox.
    
    305
    +--
    
    306
    +-- This is useful to prevent mixing up reverse requests, where we want to send
    
    307
    +-- many reverse requests but have the caller handle them without interleaving
    
    308
    +-- them.
    
    309
    +newtype InboxId = InboxId Int deriving (Show, Binary)
    
    310
    +
    
    311
    +-- | Create a new mailbox from where we can read all requests addressed to this 'InboxId'.
    
    312
    +newMailbox :: AsyncManager -> IO InboxId
    
    313
    +newMailbox am = InboxId <$> freshId am
    
    314
    +  -- `mailboxQueues` is written on demand, no need to do it here.
    
    315
    +
    
    316
    +-- | Clean up a created mailbox by 'InboxId'.
    
    317
    +closeMailbox :: AsyncManager -> InboxId -> IO ()
    
    318
    +closeMailbox AsyncManager{..} (InboxId ii) =
    
    319
    +  atomically (modifyTVar mailboxQueues (IntMap.delete ii))
    
    320
    +
    
    321
    +-- | Get or create a new mailbox for this 'InboxId'
    
    322
    +getMailbox :: AsyncManager -> InboxId -> STM (TQueue (CorrelationId, LB.ByteString))
    
    323
    +getMailbox am (InboxId ii) = do
    
    324
    +  mboxes <- readTVar (mailboxQueues am)
    
    325
    +  case IntMap.lookup ii mboxes of
    
    326
    +    Nothing -> do
    
    327
    +      q <- newTQueue
    
    328
    +      writeTVar (mailboxQueues am) (IntMap.insert ii q mboxes)
    
    329
    +      pure q
    
    330
    +    Just q ->
    
    331
    +      pure q
    
    332
    +
    
    333
    +-- ** Async matching "internals" -----------------------------------------------
    
    334
    +
    
    335
    +data AsyncManager = AsyncManager
    
    336
    +  { pendingReply :: !(TVar (IntMap (TMVar (Either SomeException LB.ByteString))))
    
    337
    +    -- ^ When a request is sent, keep track that we're expecting a reply. The
    
    338
    +    -- TMVar is signaled when the reply arrives. The exception can originate
    
    339
    +    -- from the host process, or from the remote one (with 'RemoteProcessException').
    
    340
    +  , requestsQueue :: !(TQueue (CorrelationId, LB.ByteString))
    
    341
    +    -- ^ A queue of pending received requests. When a request is received
    
    342
    +    -- /without/ a specific 'InboxId', queue it here. A request handler will
    
    343
    +    -- pop the next request from it. (Nothing except the 'drainPipe' thread
    
    344
    +    -- should read from the pipe). Read with @'handleAsyncReq' p get Nothing@.
    
    345
    +  , mailboxQueues :: !(TVar (IntMap (TQueue (CorrelationId, LB.ByteString))))
    
    346
    +    -- ^ Requests for a specific 'InboxId' that can be read using
    
    347
    +    -- @'handleAsyncReq' p get (Just <mailboxid>)@.
    
    348
    +  , writeQueue    :: !(TQueue IddMsg)
    
    349
    +    -- ^ Messages to write on the pipe.
    
    350
    +  , asyncDrainTid :: !ThreadId
    
    351
    +  , asyncFillTid  :: !ThreadId
    
    352
    +  , asyncFreshId  :: !(IORef Int)
    
    353
    +  }
    
    354
    +
    
    355
    +-- | Each 'Request' expects a 'Reply' with a matching 'CorrelationId'.
    
    356
    +type CorrelationId = Int
    
    357
    +
    
    358
    +-- | We have a single type for requests and replies because both the host and
    
    359
    +-- interpreter may send requests *and* replies on the write end *and* on recv
    
    360
    +-- both on the read end.
    
    361
    +--
    
    362
    +-- For instance:
    
    363
    +--  1. The host sends a RunTH request on its Host Write End (HWE)
    
    364
    +--  2. The interpreter sends a NewName TH request on its Interpreter Write End (IWE)
    
    365
    +--  3. The host sends a reply to the TH request on its HWE
    
    366
    +--  4. The interpreter sends the reply to the RunTH request on its IWE
    
    367
    +--
    
    368
    +-- Additionally, we want to keep concurrent TH requests for independent RunTH
    
    369
    +-- calls separated. Thus the interpreter requests additionally specify a
    
    370
    +-- mailbox announced by RunTH:
    
    371
    +--  1. To interpreter:   @Request _ Nothing (RunTH <mailid>)@
    
    372
    +--  2. From interpreter: @Request _ (Just <mailid>) NewName@
    
    373
    +-- And the host will relate the <mailid> reverse requests to that RunTH run
    
    374
    +-- exclusively.
    
    375
    +data IddMsg  = Request !CorrelationId !(Maybe InboxId) LB.ByteString
    
    376
    +             | Reply   !CorrelationId (Either SerializableException LB.ByteString)
    
    377
    +
    
    378
    +instance Binary IddMsg where
    
    379
    +  put (Request i ii bs) = putWord8 0 >> put i >> put ii >> put bs
    
    380
    +  put (Reply i bs)      = putWord8 1 >> put i >> put bs
    
    381
    +  get = do
    
    382
    +    b <- getWord8
    
    383
    +    case b of
    
    384
    +      0 -> Request <$> get <*> get <*> get
    
    385
    +      1 -> Reply   <$> get <*> get
    
    386
    +      _ -> fail $ "impossible: Binary IddMsg - " ++ show b
    
    387
    +
    
    388
    +newAsyncManager :: Pipe -> IO AsyncManager
    
    389
    +newAsyncManager pipe = do
    
    390
    +  asyncFreshId  <- newIORef 0
    
    391
    +  pendingReply  <- newTVarIO IntMap.empty
    
    392
    +  requestsQueue <- newTQueueIO
    
    393
    +  mailboxQueues <- newTVarIO IntMap.empty
    
    394
    +  writeQueue <- newTQueueIO
    
    395
    +
    
    396
    +  -- Fork thread to drain read end.
    
    397
    +  -- (cleaned up on 'closePipe')
    
    398
    +  asyncDrainTid <- forkIOWithUnmask (drainPipe pipe)
    
    399
    +
    
    400
    +  -- Fork thread to fill write end.
    
    401
    +  -- (cleaned up on 'closePipe')
    
    402
    +  asyncFillTid <- forkIOWithUnmask (fillPipe pipe)
    
    403
    +
    
    404
    +  let am = AsyncManager{..}
    
    405
    +  pure am
    
    406
    +
    
    407
    +-- | Drains the read end of the Pipe and match 'Reply'es against pending 'Request's.
    
    408
    +--
    
    409
    +-- In pratice, we'll have both a thread draining the read end of the host's
    
    410
    +-- 'Pipe' and a thread draining the read end of the interpreter's 'Pipe', since
    
    411
    +-- both processes construct a 'Pipe' and send and receive messages
    
    412
    +--
    
    413
    +-- Closes the Pipe read end when done reading
    
    414
    +drainPipe :: Pipe -> (forall x. IO x -> IO x) -> IO ()
    
    415
    +drainPipe p unmask = catch (unmask $ loop `finally` pipeCloseRead p) replyToPendings  where
    
    416
    +  loop = do
    
    417
    +    readPipe p >>= \case
    
    418
    +      -- Receive a reply to a pending notification
    
    419
    +      Just (Reply uq bs) -> do
    
    420
    +        atomically $ do
    
    421
    +          m <- readTVar (pendingReply am)
    
    422
    +          case IntMap.lookup uq m of
    
    423
    +            Nothing ->
    
    424
    +              -- the req was dropped from pending bc there was an exception, and no
    
    425
    +              -- one's listening any more. the reply is ignored
    
    426
    +              pure ()
    
    427
    +            Just wv -> do
    
    428
    +              writeTVar (pendingReply am) (IntMap.delete uq m)
    
    429
    +              -- signal matching request
    
    430
    +              putTMVar wv (bimap (toException . RemoteProcessException) id bs)
    
    431
    +        loop
    
    432
    +      -- Receive a request and queue it
    
    433
    +      Just (Request uq mbi bs) -> do
    
    434
    +        atomically $ case mbi of
    
    435
    +          Nothing ->
    
    436
    +            -- "Global" request
    
    437
    +            writeTQueue (requestsQueue am) (uq,bs)
    
    438
    +          Just ii -> do
    
    439
    +            -- "Specific mailbox" request
    
    440
    +            q <- getMailbox am ii
    
    441
    +            writeTQueue q (uq,bs)
    
    442
    +        loop
    
    443
    +      -- EOF on read handle
    
    444
    +      Nothing -> do
    
    445
    +        -- clean up pipe then exit loop
    
    446
    +        replyToPendings (toException PipeReadClosedException)
    
    447
    +        pure ()
    
    448
    +
    
    449
    +  replyToPendings e = atomically $ failPendingReplies p e
    
    450
    +
    
    451
    +  am = pipeAsyncMngr p
    
    452
    +
    
    453
    +
    
    454
    +-- | "Fill"s write end of the Pipe with queues messages.
    
    455
    +--
    
    456
    +-- Closes the Pipe write end when stopping, notifying any requests still queued to write.
    
    457
    +fillPipe :: Pipe -> (forall x. IO x -> IO x) -> IO ()
    
    458
    +fillPipe p unmask = unmask $ catch (loop `finally` pipeCloseWrite p) failPendingWrites where
    
    459
    +  loop = do
    
    460
    +    msg <- atomically $ readTQueue (writeQueue am)
    
    461
    +    withLock p . putAll p . execPut . put $ msg
    
    462
    +    loop
    
    463
    +
    
    464
    +  am = pipeAsyncMngr p
    
    465
    +
    
    466
    +  failPendingWrites :: SomeException -> IO ()
    
    467
    +  failPendingWrites e = atomically $ do
    
    468
    +    writeTVar (pipeWriteClosed p) True
    
    469
    +
    
    470
    +    writes <- flushTQueue (writeQueue am)
    
    471
    +    let uqs = [uq | Request uq _ _ <- writes]
    
    472
    +    pendings <- readTVar (pendingReply (pipeAsyncMngr p))
    
    473
    +    let tmvars = mapMaybe (`IntMap.lookup` pendings) uqs
    
    474
    +    writeTVar (pendingReply (pipeAsyncMngr p)) $! (foldl' (flip IntMap.delete) pendings uqs)
    
    475
    +    mapM_ (`writeTMVar` Left e) tmvars
    
    476
    +
    
    477
    +failPendingReplies :: Pipe -> SomeException -> STM ()
    
    478
    +failPendingReplies p e = do
    
    479
    +  -- reqs are only added if pipe not closed;
    
    480
    +  -- thus we close and take all pending atomically.
    
    481
    +  writeTVar (pipeReadClosed p) True
    
    482
    +  pendings <- swapTVar (pendingReply (pipeAsyncMngr p)) IntMap.empty
    
    483
    +  mapM_ (\wv -> putTMVar wv (Left e)) pendings
    
    484
    +
    
    485
    +-- | An exception that can be thrown on the host process representing an
    
    486
    +-- expection that happened on the remote one.
    
    487
    +newtype RemoteProcessException = RemoteProcessException SerializableException deriving Show
    
    488
    +instance Exception RemoteProcessException
    
    489
    +
    
    490
    +-- | Come up with a fresh id for 'CorrelationId' or 'InboxId'
    
    491
    +freshId :: AsyncManager -> IO Int
    
    492
    +freshId AsyncManager{asyncFreshId} =
    
    493
    +  atomicModifyIORef' asyncFreshId $ \i -> (i+1, i)
    
    494
    +
    
    495
    +--------------------------------------------------------------------------------
    
    496
    +-- * Consume `reply` exactly once
    
    497
    +--------------------------------------------------------------------------------
    
    498
    +
    
    499
    +-- | Every request should get exactly one reply.
    
    500
    +data WriteReply a where
    
    501
    +  WriteReply :: (Either SomeException a -> IO ()) %1 -> WriteReply a
    
    502
    +
    
    503
    +-- | Write a reply exactly once (consuming the ability to write more replies)
    
    504
    +writeReply :: WriteReply a %1 -> a -> IO ()
    
    505
    +writeReply (WriteReply r) = \x -> r (Right x)
    
    506
    +
    
    507
    +-- | Write the reply with a value or an exception
    
    508
    +runWriteReply :: WriteReply a %1 -> Either SomeException a -> IO ()
    
    509
    +runWriteReply (WriteReply r) = r
    
    510
    +
    
    511
    +-- | Infix, linear, 'unIO'
    
    512
    +(|#>) :: IO a %1 -> State# RealWorld -> (# State# RealWorld, a #)
    
    513
    +(|#>) (IO fs) s = fs s
    
    514
    +
    
    515
    +(%>>=) :: (IO a) -> (a -> IO b) %1 -> IO b
    
    516
    +(%>>=) = \ m f -> IO (\s0 ->
    
    517
    +        case m |#> s0 of -- unIO for linearity checking
    
    518
    +          (# s1, x #) -> f x |#> s1)
    
    519
    +infixl 1 %>>=
    
    520
    +
    
    521
    +--------------------------------------------------------------------------------
    
    522
    +-- * Serializable Exceptions
    
    523
    +--------------------------------------------------------------------------------
    
    524
    +
    
    525
    +-- SomeException can't be serialized because it contains dynamic
    
    526
    +-- types.  However, we do very limited things with the exceptions that
    
    527
    +-- are thrown by interpreted computations:
    
    528
    +--
    
    529
    +-- * We print them, e.g. "*** Exception: <something>"
    
    530
    +-- * UserInterrupt has a special meaning
    
    531
    +-- * In ghc -e, exitWith should exit with the appropriate exit code
    
    532
    +--
    
    533
    +-- So all we need to do is distinguish UserInterrupt and ExitCode, and
    
    534
    +-- all other exceptions can be represented by their 'show' string.
    
    535
    +--
    
    536
    +data SerializableException
    
    537
    +  = EUserInterrupt
    
    538
    +  | EExitCode ExitCode
    
    539
    +  | EOtherException String
    
    540
    +  deriving (Generic, Show)
    
    541
    +
    
    542
    +toSerializableException :: SomeException -> SerializableException
    
    543
    +toSerializableException ex
    
    544
    +  | Just UserInterrupt <- fromException ex  = EUserInterrupt
    
    545
    +  | Just (ec::ExitCode) <- fromException ex = (EExitCode ec)
    
    546
    +  | otherwise = EOtherException $
    
    547
    +#if MIN_VERSION_base(4,20,0)
    
    548
    +      -- Exception plus backtrace as seen in `displayExceptionWithInfo`
    
    549
    +      case displayExceptionContext (someExceptionContext ex) of
    
    550
    +        "" -> displayException (ex :: SomeException)
    
    551
    +        cx -> displayException (ex :: SomeException) ++ "\n\n" ++ cx
    
    552
    +#else
    
    553
    +      show (ex :: SomeException)
    
    554
    +#endif
    
    555
    +
    
    556
    +fromSerializableException :: SerializableException -> SomeException
    
    557
    +fromSerializableException EUserInterrupt = toException UserInterrupt
    
    558
    +fromSerializableException (EExitCode c) = toException c
    
    559
    +fromSerializableException (EOtherException str) = toException (ErrorCall str)
    
    560
    +
    
    561
    +instance Binary ExitCode
    
    562
    +instance Binary SerializableException

  • libraries/ghci/GHCi/Server.hs
    1
    -{-# LANGUAGE 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
    
    ... ... @@ -15,6 +15,7 @@ import GHCi.Signals
    15 15
     import GHCi.TH
    
    16 16
     import GHCi.Message
    
    17 17
     import Data.ByteString (ByteString)
    
    18
    +import Data.IORef
    
    18 19
     #if defined(wasm32_HOST_ARCH)
    
    19 20
     import qualified Data.ByteString.Builder as B
    
    20 21
     import qualified Data.ByteString.Internal as B
    
    ... ... @@ -30,7 +31,6 @@ import GHCi.Utils
    30 31
     import Control.DeepSeq
    
    31 32
     import Control.Exception
    
    32 33
     import Control.Monad
    
    33
    -import Control.Concurrent (threadDelay)
    
    34 34
     import Data.Binary
    
    35 35
     
    
    36 36
     import Text.Printf
    
    ... ... @@ -38,6 +38,10 @@ import System.Environment (getProgName, getArgs)
    38 38
     import System.Exit
    
    39 39
     import System.IO
    
    40 40
     
    
    41
    +import Control.Concurrent
    
    42
    +import GHCi.Message.Async (runWriteReply, (%>>=))
    
    43
    +import GHCi.Server.Threads
    
    44
    +
    
    41 45
     type MessageHook = Msg -> IO Msg
    
    42 46
     
    
    43 47
     -- | How to interpret the 'CustomCommand'.
    
    ... ... @@ -57,30 +61,35 @@ servWithCustom
    57 61
       :: Bool
    
    58 62
       -> MessageHook
    
    59 63
       -> Pipe
    
    60
    -  -> (forall a .IO a -> IO a)
    
    64
    +  -> (forall a. IO a -> IO a)
    
    61 65
       -> CustomMessageHandler
    
    62 66
       -> IO ()
    
    63
    -servWithCustom verbose hook pipe restore customHandler = loop
    
    67
    +servWithCustom verbose hook pipe restore customHandler = do
    
    68
    +  ref <- newIORef False
    
    69
    +  withThreadManager $ \mngr ->
    
    70
    +    loop ref mngr `finally` closePipe pipe
    
    64 71
      where
    
    65
    -  loop = do
    
    72
    +  loop doneRef mngr = do
    
    66 73
         when verbose $ trace "reading pipe..."
    
    67
    -    Msg msg <- readPipe pipe getMessage >>= hook
    
    68 74
     
    
    69
    -    discardCtrlC
    
    75
    +    handleRemoteCall pipe hook $ \wr (msg :: Message a) ->
    
    70 76
     
    
    71
    -    when verbose $ trace ("msg: " ++ (show msg))
    
    72
    -    case msg of
    
    73
    -      CustomMessage tag payload -> handleCustom tag payload
    
    74
    -      Shutdown -> return ()
    
    75
    -      RunTH st q ty loc -> wrapRunTH $ runTH pipe st q ty loc
    
    76
    -      RunModFinalizers st qrefs -> wrapRunTH $ runModFinalizerRefs pipe st qrefs
    
    77
    -      _other -> run msg >>= reply
    
    77
    +      discardCtrlC                                 %>>= \_ ->
    
    78
    +      when verbose (trace ("msg: " ++ (show msg))) %>>= \_ ->
    
    78 79
     
    
    79
    -  reply :: forall a. (Binary a, Show a) => a -> IO ()
    
    80
    -  reply r = do
    
    81
    -    when verbose $ trace ("writing pipe: " ++ show r)
    
    82
    -    writePipe pipe (put r)
    
    83
    -    loop
    
    80
    +      case msg of
    
    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
    
    89
    +
    
    90
    +    isDone <- readIORef 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
    
    ... ... @@ -89,17 +98,22 @@ servWithCustom verbose hook pipe restore customHandler = loop
    89 98
           Nothing ->
    
    90 99
             error $ "GHCi.Server: unhandled CustomMessage with tag " ++ show tag
    
    91 100
     
    
    101
    +  reply :: forall a. Show a => a -> IO a
    
    102
    +  reply r = do
    
    103
    +    when verbose $ trace ("writing pipe: " ++ show r)
    
    104
    +    return r -- the result of the handler is passed to writeReply
    
    105
    +
    
    92 106
       -- Run some TH code, which may interact with GHC by sending
    
    93 107
       -- THMessage requests, and then finally send RunTHDone followed by a
    
    94 108
       -- QResult.  For an overview of how TH works with Remote GHCi, see
    
    95 109
       -- Note [Remote Template Haskell] in libraries/ghci/GHCi/TH.hs.
    
    96
    -  wrapRunTH :: forall a. (Binary a, Show a) => IO a -> IO ()
    
    97
    -  wrapRunTH io = do
    
    110
    +  wrapRunTH :: forall a. (Binary a, Show a) => InboxId -> IO a -> IO (QResult a)
    
    111
    +  wrapRunTH ii io = do
    
    98 112
         when verbose $ trace "wrapRunTH..."
    
    99 113
         r <- try io
    
    100 114
         when verbose $ trace "wrapRunTH done."
    
    101 115
         when verbose $ trace "writing RunTHDone."
    
    102
    -    writePipe pipe (putTHMessage RunTHDone)
    
    116
    +    remoteTHCall pipe ii RunTHDone
    
    103 117
         case r of
    
    104 118
           Left e
    
    105 119
             | Just (GHCiQException _ err) <- fromException e  -> do
    
    ... ... @@ -159,8 +173,8 @@ defaultServerWithCustom customHandler = do
    159 173
       (outh, inh, rest) <-
    
    160 174
           case args of
    
    161 175
             arg0:arg1:rest -> do
    
    162
    -            inh  <- readGhcHandle arg1
    
    163
    -            outh <- readGhcHandle arg0
    
    176
    +            inh  <- readGhcHandle ReadMode arg1
    
    177
    +            outh <- readGhcHandle WriteMode arg0
    
    164 178
                 return (outh, inh, rest)
    
    165 179
             _ -> dieWithUsage
    
    166 180
     #endif
    
    ... ... @@ -179,7 +193,7 @@ defaultServerWithCustom customHandler = do
    179 193
     #if defined(wasm32_HOST_ARCH)
    
    180 194
       -- See Note [wasm ghci signal handlers] for details
    
    181 195
       installSignalHandlers $ js_register_signal_handler cb_sig
    
    182
    -  pipe <- mkPipeFromContinuations (recv_buf cb_recv) (send_buf cb_send)
    
    196
    +  pipe <- mkPipeFromContinuations (recv_buf cb_recv) (send_buf cb_send) (pure ()) (pure ())
    
    183 197
     #else
    
    184 198
       when verbose $
    
    185 199
         printf "GHC iserv starting (in: %s; out: %s)\n" (show inh) (show outh)
    
    ... ... @@ -195,8 +209,6 @@ defaultServerWithCustom customHandler = do
    195 209
       uninterruptibleMask $ \restore -> servWithCustom verbose hook pipe restore customHandler
    
    196 210
     
    
    197 211
       where hook = return -- empty hook
    
    198
    -    -- we cannot allow any async exceptions while communicating, because
    
    199
    -    -- we will lose sync in the protocol, hence uninterruptibleMask.
    
    200 212
     
    
    201 213
     dieWithUsage :: IO a
    
    202 214
     dieWithUsage = do
    

  • 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/TH.hs
    ... ... @@ -115,8 +115,8 @@ import qualified GHC.Boot.TH.Monad as TH
    115 115
     import Unsafe.Coerce
    
    116 116
     
    
    117 117
     -- | Create a new instance of 'QState'
    
    118
    -initQState :: Pipe -> QState
    
    119
    -initQState p = QState M.empty Nothing p
    
    118
    +initQState :: Pipe -> InboxId -> QState
    
    119
    +initQState p ii = QState M.empty Nothing ii p
    
    120 120
     
    
    121 121
     -- | The monad in which we run TH computations on the server
    
    122 122
     newtype GHCiQ a = GHCiQ { runGHCiQ :: IORef QState -> IO a }
    
    ... ... @@ -156,7 +156,7 @@ noLoc = TH.Loc "<no file>" "<no package>" "<no module>" (0,0) (0,0)
    156 156
     ghcCmd :: Binary a => THMessage (THResult a) -> GHCiQ a
    
    157 157
     ghcCmd m = GHCiQ $ \sRef -> do
    
    158 158
       s <- readIORef sRef
    
    159
    -  r <- remoteTHCall (qsPipe s) m
    
    159
    +  r <- remoteTHCall (qsPipe s) (qsInboxId s) m
    
    160 160
       case r of
    
    161 161
         THException str -> throwIO (GHCiQException s str)
    
    162 162
         THComplete res -> return res
    
    ... ... @@ -189,9 +189,9 @@ metaHandlersGHCiQ runInIO = TH.MetaHandlers {
    189 189
       -- See Note [TH recover with -fexternal-interpreter] in GHC.Tc.Gen.Splice
    
    190 190
       , mRecover = \h a -> runInIO $ GHCiQ $ \sRef -> mask $ \unmask -> do
    
    191 191
           s <- readIORef sRef
    
    192
    -      remoteTHCall (qsPipe s) StartRecover
    
    192
    +      remoteTHCall (qsPipe s) (qsInboxId s) StartRecover
    
    193 193
           e <- try $ unmask $ runGHCiQ (runQinGHCiQ a <* ghcCmd FailIfErrs) sRef
    
    194
    -      remoteTHCall (qsPipe s) (EndRecover (isLeft e))
    
    194
    +      remoteTHCall (qsPipe s) (qsInboxId s) (EndRecover (isLeft e))
    
    195 195
           case e of
    
    196 196
             Left GHCiQException{} ->
    
    197 197
               runGHCiQ (runQinGHCiQ h) sRef
    
    ... ... @@ -233,26 +233,28 @@ metaHandlersGHCiQ runInIO = TH.MetaHandlers {
    233 233
     -- a new IORef QState, and return a RemoteRef to it.
    
    234 234
     startTH :: IO (RemoteRef (IORef QState))
    
    235 235
     startTH = do
    
    236
    -  r <- newIORef (initQState (error "startTH: no pipe"))
    
    236
    +  r <- newIORef (initQState (error "startTH: no pipe") (error "startTH: no inbox id"))
    
    237 237
       mkRemoteRef r
    
    238 238
     
    
    239 239
     -- | Runs the mod finalizers.
    
    240 240
     --
    
    241 241
     -- The references must be created on the caller process.
    
    242
    -runModFinalizerRefs :: Pipe -> RemoteRef (IORef QState)
    
    242
    +runModFinalizerRefs :: Pipe -> InboxId
    
    243
    +                    -> RemoteRef (IORef QState)
    
    243 244
                         -> [RemoteRef (TH.Q ())]
    
    244 245
                         -> IO ()
    
    245
    -runModFinalizerRefs pipe rstate qrefs = do
    
    246
    +runModFinalizerRefs pipe ii rstate qrefs = do
    
    246 247
       qs <- mapM localRef qrefs
    
    247 248
       qstateref <- localRef rstate
    
    248 249
       qstate <- readIORef qstateref
    
    249
    -  qstate' <- newIORef $ qstate { qsPipe = pipe }
    
    250
    +  qstate' <- newIORef $ qstate { qsInboxId = ii, qsPipe = pipe }
    
    250 251
       _ <- runGHCiQ (runQinGHCiQ $ sequence_ qs) qstate'
    
    251 252
       return ()
    
    252 253
     
    
    253 254
     -- | The implementation of the 'RunTH' message
    
    254 255
     runTH
    
    255 256
       :: Pipe
    
    257
    +  -> InboxId
    
    256 258
       -> RemoteRef (IORef QState)
    
    257 259
           -- ^ The TH state, created by 'startTH'
    
    258 260
       -> HValueRef
    
    ... ... @@ -264,13 +266,13 @@ runTH
    264 266
       -> IO ByteString
    
    265 267
           -- ^ Returns an (encoded) result that depends on the THResultType
    
    266 268
     
    
    267
    -runTH pipe rstate rhv ty mb_loc = do
    
    269
    +runTH pipe ii rstate rhv ty mb_loc = do
    
    268 270
       hv <- localRef rhv
    
    269 271
       case ty of
    
    270
    -    THExp -> runTHQ pipe rstate mb_loc (unsafeCoerce hv :: TH.Q TH.Exp)
    
    271
    -    THPat -> runTHQ pipe rstate mb_loc (unsafeCoerce hv :: TH.Q TH.Pat)
    
    272
    -    THType -> runTHQ pipe rstate mb_loc (unsafeCoerce hv :: TH.Q TH.Type)
    
    273
    -    THDec -> runTHQ pipe rstate mb_loc (unsafeCoerce hv :: TH.Q [TH.Dec])
    
    272
    +    THExp -> runTHQ pipe ii rstate mb_loc (unsafeCoerce hv :: TH.Q TH.Exp)
    
    273
    +    THPat -> runTHQ pipe ii rstate mb_loc (unsafeCoerce hv :: TH.Q TH.Pat)
    
    274
    +    THType -> runTHQ pipe ii rstate mb_loc (unsafeCoerce hv :: TH.Q TH.Type)
    
    275
    +    THDec -> runTHQ pipe ii rstate mb_loc (unsafeCoerce hv :: TH.Q [TH.Dec])
    
    274 276
         THAnnWrapper -> do
    
    275 277
           hv <- unsafeCoerce <$> localRef rhv
    
    276 278
           case hv :: AnnotationWrapper of
    
    ... ... @@ -279,10 +281,10 @@ runTH pipe rstate rhv ty mb_loc = do
    279 281
     
    
    280 282
     -- | Run a Q computation.
    
    281 283
     runTHQ
    
    282
    -  :: Binary a => Pipe -> RemoteRef (IORef QState) -> Maybe TH.Loc -> TH.Q a
    
    284
    +  :: Binary a => Pipe -> InboxId -> RemoteRef (IORef QState) -> Maybe TH.Loc -> TH.Q a
    
    283 285
       -> IO ByteString
    
    284
    -runTHQ pipe rstate mb_loc ghciq = do
    
    286
    +runTHQ pipe ii rstate mb_loc ghciq = do
    
    285 287
       qstateref <- localRef rstate
    
    286
    -  modifyIORef' qstateref (\qstate -> qstate { qsLocation = mb_loc, qsPipe = pipe })
    
    288
    +  modifyIORef' qstateref (\qstate -> qstate { qsLocation = mb_loc, qsInboxId = ii, qsPipe = pipe })
    
    287 289
       r <- runGHCiQ (runQinGHCiQ ghciq) qstateref
    
    288 290
       return $! LB.toStrict (runPut (put r))

  • libraries/ghci/GHCi/Utils.hsc
    ... ... @@ -8,19 +8,22 @@ where
    8 8
     import Prelude
    
    9 9
     import Foreign.C
    
    10 10
     import GHC.IO.Handle (Handle())
    
    11
    +import GHC.IO.IOMode
    
    11 12
     #if defined(mingw32_HOST_OS)
    
    12
    -import Foreign.Ptr (ptrToIntPtr,wordPtrToPtr)
    
    13
    +import Foreign.Ptr (ptrToIntPtr)
    
    13 14
     import GHC.IO (onException)
    
    14 15
     import GHC.IO.Handle.FD (fdToHandle)
    
    15
    -import GHC.Windows (HANDLE)
    
    16
    +import GHC.Windows (BOOL, DWORD, HANDLE, failIfFalse_)
    
    16 17
     import GHC.IO.SubSystem ((<!>))
    
    17 18
     import GHC.IO.Handle.Windows (mkHandleFromHANDLE)
    
    18 19
     import GHC.IO.Device as IODevice
    
    19 20
     import GHC.IO.Encoding (getLocaleEncoding)
    
    20
    -import GHC.IO.IOMode
    
    21 21
     import GHC.IO.Windows.Handle (fromHANDLE, Io(), NativeHandle())
    
    22
    +import System.Process.CommunicationHandle.Internal
    
    23
    +  ( CommunicationHandle(..), useCommunicationHandle )
    
    22 24
     
    
    23 25
     #include <fcntl.h>     /* for _O_BINARY */
    
    26
    +#include <windows.h>
    
    24 27
     
    
    25 28
     #else
    
    26 29
     import System.Posix
    
    ... ... @@ -54,18 +57,23 @@ getGhcHandle fd = fdToHandle $ Fd fd
    54 57
     #endif
    
    55 58
     
    
    56 59
     -- | Read a handle passed on the command-line and prepare it to be used with the IO manager
    
    57
    -readGhcHandle :: String -> IO Handle
    
    58
    -readGhcHandle s = do
    
    60
    +-- also make the handle non-inheritable so we don't have additional references floating
    
    61
    +-- around that stop reads from unblocking on close
    
    62
    +readGhcHandle :: IOMode -> String -> IO Handle
    
    59 63
     #if defined(mingw32_HOST_OS)
    
    60
    -  let fd = wordPtrToPtr (Prelude.read s)
    
    61
    -# if defined(__IO_MANAGER_WINIO__)
    
    62
    -  -- register the handles we received with
    
    63
    -  -- our I/O manager otherwise we can't use
    
    64
    -  -- them correctly.
    
    65
    -  return () <!> associateHandle' fd
    
    66
    -# endif
    
    64
    +readGhcHandle mode s = do
    
    65
    +  let ch@(CommunicationHandle h) = Prelude.read s
    
    66
    +  failIfFalse_ "SetHandleInformation" $
    
    67
    +    c_SetHandleInformation h (#const HANDLE_FLAG_INHERIT) 0
    
    68
    +  useCommunicationHandle (mode == ReadMode) ch
    
    69
    +
    
    70
    +foreign import ccall unsafe "windows.h SetHandleInformation"
    
    71
    +  c_SetHandleInformation :: HANDLE -> DWORD -> DWORD -> IO BOOL
    
    67 72
     #else
    
    73
    +readGhcHandle _ s = do
    
    68 74
       let fd = Prelude.read s
    
    69
    -#endif
    
    75
    +# if !defined(javascript_HOST_ARCH)
    
    76
    +  setFdOption (Fd fd) CloseOnExec True
    
    77
    +# endif
    
    70 78
       getGhcHandle fd
    
    71
    -
    79
    +#endif

  • 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:
    
    ... ... @@ -76,6 +77,7 @@ library
    76 77
         exposed-modules:
    
    77 78
             GHCi.BreakArray
    
    78 79
             GHCi.Message
    
    80
    +        GHCi.Message.Async
    
    79 81
             GHCi.ResolvedBCO
    
    80 82
             GHCi.RemoteTypes
    
    81 83
             GHCi.FFI
    
    ... ... @@ -95,7 +97,8 @@ library
    95 97
             filepath         >= 1.4 && < 1.6,
    
    96 98
             ghc-boot         == @ProjectVersionMunged@,
    
    97 99
             ghc-heap         >= 9.10.1 && <=@ProjectVersionMunged@,
    
    98
    -        transformers     >= 0.5 && < 0.7
    
    100
    +        transformers     >= 0.5 && < 0.7,
    
    101
    +        stm              >= 2.4 && < 2.6
    
    99 102
     
    
    100 103
         if flag(bootstrap)
    
    101 104
           build-depends:
    
    ... ... @@ -106,6 +109,8 @@ library
    106 109
     
    
    107 110
         if !os(windows)
    
    108 111
             Build-Depends: unix >= 2.7 && < 2.9
    
    112
    +    else
    
    113
    +        Build-Depends: process >= 1.6.20 && < 1.7
    
    109 114
     
    
    110 115
         if arch(wasm32)
    
    111 116
             build-depends: ghc-experimental == @ProjectVersionForLib@.0

  • testsuite/tests/ghci/custom-external-interpreter-commands/Main.hs
    ... ... @@ -12,6 +12,7 @@ import GHCi.Message
    12 12
       , mkPipeFromHandles
    
    13 13
       , remoteCall
    
    14 14
       , Pipe
    
    15
    +  , closePipe
    
    15 16
       )
    
    16 17
     import GHCi.Server
    
    17 18
       ( CustomMessageHandler
    
    ... ... @@ -153,14 +154,13 @@ customMessage pipe c = do
    153 154
     runClient :: Int -> IO ()
    
    154 155
     runClient input = do
    
    155 156
       serverExe <- getExecutablePath
    
    156
    -  withServer serverExe $ \hFromServer hToServer -> do
    
    157
    -    pipe <- mkPipeFromHandles hFromServer hToServer
    
    157
    +  withServer serverExe $ \pipe -> do
    
    158 158
         res  <- customMessage pipe (SquareCommand input)
    
    159 159
         putStrLn $ "Square returned: " ++ show res
    
    160 160
         res2  <- customMessage pipe (MulCommand 2 res)
    
    161 161
         putStrLn $ "Mul returned: " ++ show res2
    
    162 162
     
    
    163
    -withServer :: FilePath -> (Handle -> Handle -> IO a) -> IO a
    
    163
    +withServer :: FilePath -> (Pipe -> IO a) -> IO a
    
    164 164
     withServer serverExe action = do
    
    165 165
       (ghcRead, serverWrite) <- createPipe
    
    166 166
       (serverRead, ghcWrite) <- createPipe
    
    ... ... @@ -171,13 +171,12 @@ withServer serverExe action = do
    171 171
         , std_out = Inherit
    
    172 172
         , std_err = Inherit
    
    173 173
         }
    
    174
    -  bracket (mkHandles ghcRead ghcWrite)
    
    175
    -          (\(hFromServer, hToServer) -> do
    
    176
    -             hClose hFromServer
    
    177
    -             hClose hToServer
    
    174
    +  bracket (mkHandles ghcRead ghcWrite >>= uncurry mkPipeFromHandles)
    
    175
    +          (\pipe -> do
    
    176
    +             closePipe pipe
    
    178 177
                  terminateProcess ph
    
    179 178
                  void (waitForProcess ph))
    
    180
    -          (\(hFromServer, hToServer) -> action hFromServer hToServer)
    
    179
    +          (\pipe -> action pipe)
    
    181 180
       where
    
    182 181
         mkHandles r w = do
    
    183 182
           hR <- fdToHandle r