[Git][ghc/ghc][wip/romes/async-rpc] interp: fix external interpreter shutdown deadlock on Windows
Zubin pushed to branch wip/romes/async-rpc at Glasgow Haskell Compiler / GHC Commits: 66d87bca by Zubin Duggal at 2026-10-16T15:18:00+05:30 interp: fix external interpreter shutdown deadlock on Windows On shutdown both GHC and iserv call `closePipe`, which stopped the drain thread before closing the write end. On Windows the drain thread reads with a safe foreign call to _read, which `throwTo' can't interrupt, so the two sides were stuck in the deadlock deadlock. Closing the write end first is not enough on its own. On Windows a read on a pipe only unblocks once all write handles to it are closed [1]. However, all all four pipe ends were inheritable and we didn't close the child's ends in the parent. This means a close on any one side wouldn't unblock any reads, becuase the other side had another copy open. * runWithPipes now uses System.Process.CommunicationHandle [2], which is built for passing pipes to a child process: only the child's ends are inheritable, and it simplifies our mess of CPP. Additionally, it is important to close our copy of the endpoints passed to the child so each side only keeps open copies it needs to allow a close to unblock reads. * readGhcHandle makes the handle non-inheritable so we don't have additional references floating around that stop reads from unblocking on close. * In closePipe the ordering is subtle and important. We first fail any requests still waiting for a reply, then close the write end so the other side sees EOF. We take the write lock to close it, so we never close under a concurrent write [3]. Only then do we stop the drain thread, which closes the read end itself once its read has finished. Bump bound to process >= 1.6.20 as this is the version with System.Process.CommunicationHandle [1] https://learn.microsoft.com/en-us/windows/win32/ipc/anonymous-pipe-operation... [2] https://github.com/haskell/process/pull/308 [3] https://gitlab.haskell.org/ghc/ghc/-/work_items/27801 - - - - - 6 changed files: - compiler/GHC/Runtime/Utils.hs - compiler/ghc.cabal.in - libraries/ghci/GHCi/Message/Async.hs - libraries/ghci/GHCi/Server.hs - libraries/ghci/GHCi/Utils.hsc - libraries/ghci/ghci.cabal.in Changes: ===================================== compiler/GHC/Runtime/Utils.hs ===================================== @@ -1,84 +1,30 @@ -{-# LANGUAGE CPP #-} - module GHC.Runtime.Utils ( runWithPipes ) where import GHC.Prelude +import GHC.Utils.Exception (onException) -#if defined(mingw32_HOST_OS) -import Foreign.C -import GHC.IO.Handle.FD (fdToHandle) -import GHC.Utils.Exception as Ex -# if defined(__IO_MANAGER_WINIO__) -import GHC.IO.SubSystem ((<!>)) -import GHC.IO.Handle.Windows (handleToHANDLE) -import GHC.Event.Windows (associateHandle') -# endif -#else -import System.Posix as Posix -#endif import System.Process +import System.Process.CommunicationHandle import System.IO +-- | Spawn a process connected to us by two pipes. Important to close our copy +-- of the endpoints passed to the child so each side only keeps open copies it +-- needs. If we don't do that, then closing one end wouldn't interrupt reads on +-- the other. runWithPipes :: (CreateProcess -> IO ProcessHandle) -> FilePath -> [String] -> [String] -> IO (ProcessHandle, Handle, Handle) -#if defined(mingw32_HOST_OS) -foreign import ccall "io.h _close" - c__close :: CInt -> IO CInt - -foreign import ccall unsafe "io.h _get_osfhandle" - _get_osfhandle :: CInt -> IO CIntPtr - -runWithPipesPOSIX :: (CreateProcess -> IO ProcessHandle) - -> FilePath -> [String] -> [String] -> IO (ProcessHandle, Handle, Handle) -runWithPipesPOSIX createProc prog pre_opts opts = do - (rfd1, wfd1) <- createPipeFd -- we read on rfd1 - (rfd2, wfd2) <- createPipeFd -- we write on wfd2 - wh_client <- _get_osfhandle wfd1 - rh_client <- _get_osfhandle rfd2 - let args = pre_opts ++ (show wh_client : show rh_client : opts) - ph <- createProc (proc prog args) - rh <- mkHandle rfd1 - wh <- mkHandle wfd2 - return (ph, rh, wh) - where mkHandle :: CInt -> IO Handle - mkHandle fd = (fdToHandle fd) `Ex.onException` (c__close fd) - -# if defined (__IO_MANAGER_WINIO__) -runWithPipesNative :: (CreateProcess -> IO ProcessHandle) - -> FilePath -> [String] -> [String] -> IO (ProcessHandle, Handle, Handle) -runWithPipesNative createProc prog pre_opts opts = do - (rh, wfd1) <- createPipe -- we read on rfd1 - (rfd2, wh) <- createPipe -- we write on wfd2 - wh_client <- handleToHANDLE wfd1 - rh_client <- handleToHANDLE rfd2 - -- Associate the handle with the current manager - -- but don't touch the ones we're passing to the child - -- since it needs to register the handle with its own manager. - associateHandle' =<< handleToHANDLE rh - associateHandle' =<< handleToHANDLE wh - let args = pre_opts ++ (show wh_client : show rh_client : opts) - ph <- createProc (proc prog args) - return (ph, rh, wh) - -runWithPipes = runWithPipesPOSIX <!> runWithPipesNative -# else -runWithPipes = runWithPipesPOSIX -# endif -#else runWithPipes createProc prog pre_opts opts = do - (rfd1, wfd1) <- Posix.createPipe -- we read on rfd1 - (rfd2, wfd2) <- Posix.createPipe -- we write on wfd2 - setFdOption rfd1 CloseOnExec True - setFdOption wfd2 CloseOnExec True - let args = pre_opts ++ (show wfd1 : show rfd2 : opts) + (rh, chTheyWrite) <- createWeReadTheyWritePipe + (chTheyRead, wh) <- createTheyReadWeWritePipe + let args = pre_opts ++ (show chTheyWrite : show chTheyRead : opts) + closeTheirs = do + closeCommunicationHandle chTheyWrite + closeCommunicationHandle chTheyRead + -- These handles have no finalizers, so close them ourselves if the spawn fails. ph <- createProc (proc prog args) - closeFd wfd1 - closeFd rfd2 - rh <- fdToHandle rfd1 - wh <- fdToHandle wfd2 + `onException` (closeTheirs >> hClose rh >> hClose wh) + closeTheirs return (ph, rh, wh) -#endif - ===================================== compiler/ghc.cabal.in ===================================== @@ -117,7 +117,7 @@ Library Build-Depends: base >= 4.11 && < 4.24, deepseq >= 1.4 && < 1.6, directory >= 1 && < 1.4, - process >= 1 && < 1.7, + process >= 1.6.20 && < 1.7, bytestring >= 0.11 && < 0.13, binary == 0.8.*, text >= 2.0 && < 2.2, ===================================== libraries/ghci/GHCi/Message/Async.hs ===================================== @@ -106,29 +106,23 @@ mkPipeFromContinuations getSome putAll pipeCloseRead pipeCloseWrite = mdo let p = Pipe { .. } pure p --- | Cleanly close our ends of the Pipe, terminating the async manager's --- drain-read-end thread. Also calls 'pipeCloseRead' and 'pipeCloseWrite', in that order. +-- | Close our ends of the pipe. -- --- If the pipe was created using 'mkPipeFromHandles', the handles are also closed. --- --- The requests we've sent that are pending are all canceled with --- 'PipeReadClosedException' by ourselves. --- --- No special notification is sent to the remote process, a message to that --- effect should be sent ahead of time if one is needed (see 'Shutdown'). +-- The ordering is subtle and important. We first fail any requests still +-- waiting for a reply, then close the write end so the other side sees EOF. +-- We take the write lock to close it, so we never close under a concurrent +-- write. Only then do we stop the drain thread, which closes the read end +-- itself once its read has finished. On Windows its read can't be +-- interrupted, so this waits for the other side to close its write end too. closePipe :: Pipe -> IO () -closePipe Pipe{..} = do - - readAlreadyClosed <- atomically $ swapTVar pipeReadClosed True - - -- this triggers pipeCloseRead - unless readAlreadyClosed $ do - - -- releases handle lock taken by reading, and cleans up by replying to every - -- pending request with 'PipeReadClosedException'. - throwTo (asyncDrainTid pipeAsyncMngr) PipeReadClosedException - - pipeCloseWrite +closePipe p@Pipe{..} = do + readAlreadyClosed <- atomically $ do + closed <- readTVar pipeReadClosed + failPendingReplies p (toException PipeReadClosedException) + pure closed + withLock p pipeCloseWrite `finally` + unless readAlreadyClosed + (throwTo (asyncDrainTid pipeAsyncMngr) PipeReadClosedException) -- | We've closed the pipe read end on this process data PipeReadClosedException = PipeReadClosedException deriving Show @@ -419,15 +413,18 @@ drainPipe p unmask = catch (unmask $ loop `finally` pipeCloseRead p) replyToPend replyToPendings (toException PipeReadClosedException) pure () - replyToPendings e = atomically $ do - -- reqs are only added if pipe not closed; - -- thus we close and take all pending atomically. - writeTVar (pipeReadClosed p) True - pendings <- swapTVar (pendingReply am) IntMap.empty -- take pending - mapM_ (\p -> putTMVar p (Left e {-host exception-})) pendings + replyToPendings e = atomically $ failPendingReplies p e am = pipeAsyncMngr p +failPendingReplies :: Pipe -> SomeException -> STM () +failPendingReplies p e = do + -- reqs are only added if pipe not closed; + -- thus we close and take all pending atomically. + writeTVar (pipeReadClosed p) True + pendings <- swapTVar (pendingReply (pipeAsyncMngr p)) IntMap.empty + mapM_ (\wv -> putTMVar wv (Left e)) pendings + -- | An exception that can be thrown on the host process representing an -- expection that happened on the remote one. newtype RemoteProcessException = RemoteProcessException SerializableException deriving Show ===================================== libraries/ghci/GHCi/Server.hs ===================================== @@ -78,9 +78,6 @@ servWithCustom verbose hook pipe restore customHandler = newIORef False >>= loop RunModFinalizers ii st qrefs -> wrapRunTH ii $ runModFinalizerRefs pipe ii st qrefs _other -> run msg >>= reply - -- closePipe here will wait until every active pipeWrite pipe exits, but - -- they should never block for long, as the host process is either reading - -- or has called pipeCloseRead. isDone <- readIORef doneRef if isDone then closePipe pipe else loop doneRef @@ -167,8 +164,8 @@ defaultServerWithCustom customHandler = do (outh, inh, rest) <- case args of arg0:arg1:rest -> do - inh <- readGhcHandle arg1 - outh <- readGhcHandle arg0 + inh <- readGhcHandle ReadMode arg1 + outh <- readGhcHandle WriteMode arg0 return (outh, inh, rest) _ -> dieWithUsage #endif ===================================== libraries/ghci/GHCi/Utils.hsc ===================================== @@ -8,19 +8,22 @@ where import Prelude import Foreign.C import GHC.IO.Handle (Handle()) +import GHC.IO.IOMode #if defined(mingw32_HOST_OS) -import Foreign.Ptr (ptrToIntPtr,wordPtrToPtr) +import Foreign.Ptr (ptrToIntPtr) import GHC.IO (onException) import GHC.IO.Handle.FD (fdToHandle) -import GHC.Windows (HANDLE) +import GHC.Windows (BOOL, DWORD, HANDLE, failIfFalse_) import GHC.IO.SubSystem ((<!>)) import GHC.IO.Handle.Windows (mkHandleFromHANDLE) import GHC.IO.Device as IODevice import GHC.IO.Encoding (getLocaleEncoding) -import GHC.IO.IOMode import GHC.IO.Windows.Handle (fromHANDLE, Io(), NativeHandle()) +import System.Process.CommunicationHandle.Internal + ( CommunicationHandle(..), useCommunicationHandle ) #include <fcntl.h> /* for _O_BINARY */ +#include <windows.h> #else import System.Posix @@ -54,18 +57,23 @@ getGhcHandle fd = fdToHandle $ Fd fd #endif -- | Read a handle passed on the command-line and prepare it to be used with the IO manager -readGhcHandle :: String -> IO Handle -readGhcHandle s = do +-- also make the handle non-inheritable so we don't have additional references floating +-- around that stop reads from unblocking on close +readGhcHandle :: IOMode -> String -> IO Handle #if defined(mingw32_HOST_OS) - let fd = wordPtrToPtr (Prelude.read s) -# if defined(__IO_MANAGER_WINIO__) - -- register the handles we received with - -- our I/O manager otherwise we can't use - -- them correctly. - return () <!> associateHandle' fd -# endif +readGhcHandle mode s = do + let ch@(CommunicationHandle h) = Prelude.read s + failIfFalse_ "SetHandleInformation" $ + c_SetHandleInformation h (#const HANDLE_FLAG_INHERIT) 0 + useCommunicationHandle (mode == ReadMode) ch + +foreign import ccall unsafe "windows.h SetHandleInformation" + c_SetHandleInformation :: HANDLE -> DWORD -> DWORD -> IO BOOL #else +readGhcHandle _ s = do let fd = Prelude.read s -#endif +# if !defined(javascript_HOST_ARCH) + setFdOption (Fd fd) CloseOnExec True +# endif getGhcHandle fd - +#endif ===================================== libraries/ghci/ghci.cabal.in ===================================== @@ -108,6 +108,8 @@ library if !os(windows) Build-Depends: unix >= 2.7 && < 2.9 + else + Build-Depends: process >= 1.6.20 && < 1.7 if arch(wasm32) build-depends: ghc-experimental == @ProjectVersionForLib@.0 View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/66d87bcad518937a6d50ff2fe692bd3e... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/66d87bcad518937a6d50ff2fe692bd3e... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
participants (1)
-
Zubin (@wz1000)