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

Commits:

6 changed files:

Changes:

  • 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.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/Async.hs
    ... ... @@ -106,29 +106,23 @@ mkPipeFromContinuations getSome putAll pipeCloseRead pipeCloseWrite = mdo
    106 106
         let p = Pipe { .. }
    
    107 107
         pure p
    
    108 108
     
    
    109
    --- | Cleanly close our ends of the Pipe, terminating the async manager's
    
    110
    --- drain-read-end thread. Also calls 'pipeCloseRead' and 'pipeCloseWrite', in that order.
    
    109
    +-- | Close our ends of the pipe.
    
    111 110
     --
    
    112
    --- If the pipe was created using 'mkPipeFromHandles', the handles are also closed.
    
    113
    ---
    
    114
    --- The requests we've sent that are pending are all canceled with
    
    115
    --- 'PipeReadClosedException' by ourselves.
    
    116
    ---
    
    117
    --- No special notification is sent to the remote process, a message to that
    
    118
    --- effect should be sent ahead of time if one is needed (see 'Shutdown').
    
    111
    +-- The ordering is subtle and important. We first fail any requests still
    
    112
    +-- waiting for a reply, then close the write end so the other side sees EOF.
    
    113
    +-- We take the write lock to close it, so we never close under a concurrent
    
    114
    +-- write. Only then do we stop the drain thread, which closes the read end
    
    115
    +-- itself once its read has finished. On Windows its read can't be
    
    116
    +-- interrupted, so this waits for the other side to close its write end too.
    
    119 117
     closePipe :: Pipe -> IO ()
    
    120
    -closePipe Pipe{..} = do
    
    121
    -
    
    122
    -  readAlreadyClosed <- atomically $ swapTVar pipeReadClosed True
    
    123
    -
    
    124
    -  -- this triggers pipeCloseRead
    
    125
    -  unless readAlreadyClosed $ do
    
    126
    -
    
    127
    -    -- releases handle lock taken by reading, and cleans up by replying to every
    
    128
    -    -- pending request with 'PipeReadClosedException'.
    
    129
    -    throwTo (asyncDrainTid pipeAsyncMngr) PipeReadClosedException
    
    130
    -
    
    131
    -  pipeCloseWrite
    
    118
    +closePipe p@Pipe{..} = do
    
    119
    +  readAlreadyClosed <- atomically $ do
    
    120
    +    closed <- readTVar pipeReadClosed
    
    121
    +    failPendingReplies p (toException PipeReadClosedException)
    
    122
    +    pure closed
    
    123
    +  withLock p pipeCloseWrite `finally`
    
    124
    +    unless readAlreadyClosed
    
    125
    +      (throwTo (asyncDrainTid pipeAsyncMngr) PipeReadClosedException)
    
    132 126
     
    
    133 127
     -- | We've closed the pipe read end on this process
    
    134 128
     data PipeReadClosedException = PipeReadClosedException deriving Show
    
    ... ... @@ -419,15 +413,18 @@ drainPipe p unmask = catch (unmask $ loop `finally` pipeCloseRead p) replyToPend
    419 413
             replyToPendings (toException PipeReadClosedException)
    
    420 414
             pure ()
    
    421 415
     
    
    422
    -  replyToPendings e = atomically $ do
    
    423
    -    -- reqs are only added if pipe not closed;
    
    424
    -    -- thus we close and take all pending atomically.
    
    425
    -    writeTVar (pipeReadClosed p) True
    
    426
    -    pendings <- swapTVar (pendingReply am) IntMap.empty -- take pending
    
    427
    -    mapM_ (\p -> putTMVar p (Left e {-host exception-})) pendings
    
    416
    +  replyToPendings e = atomically $ failPendingReplies p e
    
    428 417
     
    
    429 418
       am = pipeAsyncMngr p
    
    430 419
     
    
    420
    +failPendingReplies :: Pipe -> SomeException -> STM ()
    
    421
    +failPendingReplies p e = do
    
    422
    +  -- reqs are only added if pipe not closed;
    
    423
    +  -- thus we close and take all pending atomically.
    
    424
    +  writeTVar (pipeReadClosed p) True
    
    425
    +  pendings <- swapTVar (pendingReply (pipeAsyncMngr p)) IntMap.empty
    
    426
    +  mapM_ (\wv -> putTMVar wv (Left e)) pendings
    
    427
    +
    
    431 428
     -- | An exception that can be thrown on the host process representing an
    
    432 429
     -- expection that happened on the remote one.
    
    433 430
     newtype RemoteProcessException = RemoteProcessException SerializableException deriving Show
    

  • libraries/ghci/GHCi/Server.hs
    ... ... @@ -78,9 +78,6 @@ servWithCustom verbose hook pipe restore customHandler = newIORef False >>= loop
    78 78
             RunModFinalizers ii st qrefs -> wrapRunTH ii $ runModFinalizerRefs pipe ii st qrefs
    
    79 79
             _other -> run msg >>= reply
    
    80 80
     
    
    81
    -    -- closePipe here will wait until every active pipeWrite pipe exits, but
    
    82
    -    -- they should never block for long, as the host process is either reading
    
    83
    -    -- or has called pipeCloseRead.
    
    84 81
         isDone <- readIORef doneRef
    
    85 82
         if isDone then closePipe pipe
    
    86 83
                   else loop doneRef
    
    ... ... @@ -167,8 +164,8 @@ defaultServerWithCustom customHandler = do
    167 164
       (outh, inh, rest) <-
    
    168 165
           case args of
    
    169 166
             arg0:arg1:rest -> do
    
    170
    -            inh  <- readGhcHandle arg1
    
    171
    -            outh <- readGhcHandle arg0
    
    167
    +            inh  <- readGhcHandle ReadMode arg1
    
    168
    +            outh <- readGhcHandle WriteMode arg0
    
    172 169
                 return (outh, inh, rest)
    
    173 170
             _ -> dieWithUsage
    
    174 171
     #endif
    

  • 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
    ... ... @@ -108,6 +108,8 @@ library
    108 108
     
    
    109 109
         if !os(windows)
    
    110 110
             Build-Depends: unix >= 2.7 && < 2.9
    
    111
    +    else
    
    112
    +        Build-Depends: process >= 1.6.20 && < 1.7
    
    111 113
     
    
    112 114
         if arch(wasm32)
    
    113 115
             build-depends: ghc-experimental == @ProjectVersionForLib@.0